結果
| 問題 |
No.556 仁義なきサルたち
|
| コンテスト | |
| ユーザー |
okchan08
|
| 提出日時 | 2018-04-05 22:15:53 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 2,000 ms |
| コード長 | 1,288 bytes |
| コンパイル時間 | 586 ms |
| コンパイル使用メモリ | 62,456 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-26 10:39:04 |
| 合計ジャッジ時間 | 1,378 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
class UnionFind{
public:
UnionFind(){
UnionFind(0);
}
UnionFind(int N){
par.resize(N);
rnk.resize(N);
for(int i=0;i<N;i++){
par[i] = i;
rnk[i] = 1;
}
}
int root(int x){
return par[x] = par[x] == x ? x : root(par[x]);
}
bool same(int x, int y){
return root(x) == root(y);
}
void unite(int x, int y){
x = root(x); y = root(y);
if(x == y) return;
if(rnk[x] < rnk[y] || (rnk[x] == rnk[y] && y < x)){
par[x] = y;
rnk[y] += rnk[x];
}else{
par[y] = x;
rnk[x] += rnk[y];
}
}
int getrnk(int i){
return rnk[i];
}
private:
vector<int> par, rnk;
};
int main(){
int N; cin >> N;
UnionFind uf = UnionFind(N);
int M; cin >> M;
int A,B;
for(int i=0;i<M;i++){
cin >> A >> B;
A--; B--;
if(uf.same(A,B)) continue;
uf.unite(A,B);
}
for(int i=0;i<N;i++){
cout << uf.root(i) + 1 << endl;
}
return 0;
}
okchan08