結果

問題 No.994 ばらばらコイン
ユーザー forest3forest3
提出日時 2020-03-25 15:47:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 762 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 166,812 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-30 11:41:27
合計ジャッジ時間 3,635 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 52 ms
4,380 KB
testcase_03 AC 53 ms
4,376 KB
testcase_04 AC 52 ms
4,376 KB
testcase_05 AC 26 ms
4,380 KB
testcase_06 AC 53 ms
4,376 KB
testcase_07 AC 52 ms
4,376 KB
testcase_08 AC 37 ms
4,380 KB
testcase_09 AC 49 ms
4,376 KB
testcase_10 AC 36 ms
4,376 KB
testcase_11 AC 41 ms
4,380 KB
testcase_12 AC 44 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

 #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;
}
0