結果
| 問題 |
No.556 仁義なきサルたち
|
| コンテスト | |
| ユーザー |
ant2357
|
| 提出日時 | 2018-04-23 23:36:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 19 ms / 2,000 ms |
| コード長 | 948 bytes |
| コンパイル時間 | 2,153 ms |
| コンパイル使用メモリ | 195,496 KB |
| 最終ジャッジ日時 | 2025-01-05 10:20:45 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { -1, 0, 1, 0 };
struct UnionFind {
vector<int> data;
UnionFind(int n) : data(n, -1) {}
int find(int x) {
return data[x] < 0 ? x : data[x] = find(data[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
if (data[x] > data[y] || data[x] == data[y] && x > y) {
swap(x, y);
}
data[x] += data[y];
data[y] = x;
return true;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--; b--;
uf.unite(a, b);
}
for (int i = 0; i < n; i++) {
cout << uf.find(i) + 1 << endl;
}
return 0;
}
ant2357