結果
| 問題 |
No.994 ばらばらコイン
|
| コンテスト | |
| ユーザー |
forest3
|
| 提出日時 | 2020-03-25 15:47:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 66 ms / 2,000 ms |
| コード長 | 762 bytes |
| コンパイル時間 | 1,613 ms |
| コンパイル使用メモリ | 163,128 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2025-01-01 23:56:18 |
| 合計ジャッジ時間 | 3,309 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) { }
bool unionSet(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) {
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
int size(int x) {
return -data[root(x)];
}
};
int main()
{
int N, K;
cin >> N >> K;
UnionFind uf( N );
for( int i = 0; i < N - 1; i++ ) {
int a, b;
cin >> a >> b;
a--;
b--;
uf.unionSet( a, b );
}
int ans = -1;
if( K <= uf.size( 0 ) ) ans = K - 1;
cout << ans << endl;
}
forest3