結果
| 問題 | No.1390 Get together |
| コンテスト | |
| ユーザー |
m_tsubasa
|
| 提出日時 | 2021-02-12 21:27:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 133 ms / 2,000 ms |
| コード長 | 950 bytes |
| コンパイル時間 | 1,916 ms |
| コンパイル使用メモリ | 196,440 KB |
| 最終ジャッジ日時 | 2025-01-18 17:58:31 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct Unionfind {
// tree number
vector<int> par;
// constructor
Unionfind(int n = 1) : par(n, -1) {}
// search root
int root(int x) {
if (par[x] < 0) return x;
return par[x] = root(par[x]);
}
// is same?
bool issame(int x, int y) { return root(x) == root(y); }
// add
// already added, return 0
bool uni(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return 0;
if (par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
return 1;
}
int size(int x) { return -par[root(x)]; }
};
int n, m;
Unionfind uf;
vector<int> v;
int main() {
cin >> n >> m;
uf = Unionfind(m);
v.assign(n, -1);
int res = 0;
for (int i = 0; i < n; ++i) {
int b, c;
cin >> b >> c;
--b, --c;
if (v[c] >= 0) {
if (!uf.issame(b, v[c])) ++res, uf.uni(b, v[c]);
} else
v[c] = b;
}
cout << res << endl;
return 0;
}
m_tsubasa