結果
問題 |
No.556 仁義なきサルたち
|
ユーザー |
![]() |
提出日時 | 2018-03-16 22:14:58 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 22 ms / 2,000 ms |
コード長 | 871 bytes |
コンパイル時間 | 526 ms |
コンパイル使用メモリ | 55,772 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-21 14:44:52 |
合計ジャッジ時間 | 1,849 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#include<iostream> using namespace std; int parent[10001]; int data[10001]; int Find(int x){ if(parent[x] == x) return x; else{ return parent[x] = Find(parent[x]); } } void Union(int x,int y){ int xroot = Find(x); int yroot = Find(y); if(data[xroot] < data[yroot] ||( (data[xroot] == data[yroot]) && xroot > yroot )){ parent[xroot] = yroot; data[yroot] += data[xroot]; } else{ parent[yroot] = xroot; data[xroot] += data[yroot]; } } bool isUnion(int x,int y){ return Find(x) == Find(y); } int main(){ int N,M,A,B; cin >> N >> M; for(int i=1;i<=N;i++){ parent[i] = i; data[i] = 1; } for(int i=0;i<M;i++){ cin >> A >> B; if(isUnion(A,B)) continue; Union(A,B); } for(int i=1;i<=N;i++) cout << Find(i) << endl; return 0; }