結果
| 問題 |
No.330 Eigenvalue Decomposition
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-17 16:06:31 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 196 ms / 5,000 ms |
| コード長 | 1,164 bytes |
| コンパイル時間 | 583 ms |
| コンパイル使用メモリ | 60,788 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-14 04:22:58 |
| 合計ジャッジ時間 | 4,563 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 31 |
ソースコード
#include <iostream>
#include <vector>
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
/*
* Union-Find tree
* header requirement: vector
*/
class UnionFind {
private:
std::vector<int> disj;
std::vector<int> rank;
public:
UnionFind(int n) : disj(n), rank(n) {
for (int i = 0; i < n; ++i) {
disj[i] = i;
rank[i] = 0;
}
}
int root(int x) {
if (disj[x] == x) {
return x;
}
return disj[x] = root(disj[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (rank[x] < rank[y]) {
disj[x] = y;
} else {
disj[y] = x;
if (rank[x] == rank[y]) {
++rank[x];
}
}
}
bool is_same_set(int x, int y) {
return root(x) == root(y);
}
};
// Solution in the editoral.
int main(void){
int n, m;
cin >> n >> m;
int conn = n;
UnionFind uf(n);
REP(i, 0, m) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
if (not uf.is_same_set(a, b)) {
uf.unite(a, b);
conn--;
}
}
cout << conn << endl;
}