結果
問題 | No.556 仁義なきサルたち |
ユーザー |
![]() |
提出日時 | 2018-06-28 22:01:10 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 21 ms / 2,000 ms |
コード長 | 779 bytes |
コンパイル時間 | 1,472 ms |
コンパイル使用メモリ | 164,508 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-30 23:34:38 |
合計ジャッジ時間 | 2,315 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++) struct UF{ vector<int> node; UF(int sz){ node.resize(sz,-1); } int root(int x){ return (node[x] < 0 ? x : node[x] = root(node[x])); } int size(int x){ return -node[root(x)]; } void unite(int x,int y){ x = root(x); y = root(y); if(x == y) return; if(size(x) < size(y)){ swap(x,y); } else if(size(x) > size(y)){ } else if(x > y){ swap(x,y); } node[x] += node[y]; node[y] = x; } }; int main(){ int N,M; cin >> N >> M; UF uf(N + 1); rep(i,0,M - 1){ int a,b; cin >> a >> b; uf.unite(a,b); } rep(i,1,N){ cout << uf.root(i) << endl; } }