結果
| 問題 |
No.556 仁義なきサルたち
|
| ユーザー |
Konton7
|
| 提出日時 | 2020-02-05 12:48:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 2,000 ms |
| コード長 | 1,415 bytes |
| コンパイル時間 | 1,871 ms |
| コンパイル使用メモリ | 196,080 KB |
| 最終ジャッジ日時 | 2025-01-08 22:20:01 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
class Unionfind
{
vector<int> p;
vector<int> q;
public:
int find(int x);
void unite(int x, int y);
Unionfind(int n);
};
Unionfind::Unionfind(int n)
{
for (int i = 0; i < n; i++)
{
p.push_back(i);
q.push_back(1);
}
}
int Unionfind::find(int x)
{
while (p[x] != x)
{
p[x] = p[p[x]];
x = p[x];
}
return x;
}
void Unionfind::unite(int x, int y)
{
x = Unionfind::find(x);
y = Unionfind::find(y);
if (x > y)
swap(x, y);
if (x != y)
{
if (q[x] >= q[y])
{
p[y] = x;
q[x] += q[y];
q[y] = 0;
}
else
{
p[x] = y;
q[y] += q[x];
q[x] = 0;
}
}
}
int main()
{
#ifdef DEBUG
cout << "DEBUG MODE" << endl;
ifstream in("input.txt"); //for debug
cin.rdbuf(in.rdbuf()); //for debug
#endif
int n, m, x, y;
cin >> n >> m;
Unionfind monkeys(n);
rep(i, m)
{
cin >> x >> y;
x--, y--;
monkeys.unite(x, y);
}
rep(i, n)
cout << monkeys.find(i) + 1 << endl;
return 0;
}
Konton7