結果

問題 No.994 ばらばらコイン
ユーザー forest3forest3
提出日時 2020-03-25 15:39:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,153 bytes
コンパイル時間 2,522 ms
コンパイル使用メモリ 171,524 KB
実行使用メモリ 9,352 KB
最終ジャッジ日時 2023-08-30 11:40:51
合計ジャッジ時間 4,014 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 67 ms
8,864 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 50 ms
7,208 KB
testcase_09 WA -
testcase_10 AC 54 ms
7,364 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

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)];
  }
};

vector<vector<int>> g;
int ans = -1;

int dfs( int n, int p, int m, int d )
{
//cout << n << " " << p << " " << m << " " << d << endl;
	if( m == 1 ) return d;
	for( int e : g[n] ) {
		if( e == p ) continue;
		int res = dfs( e, n, m - 1, d + 1 );
		ans = min( ans, res );
	}
	return ans;
}

int main()
{
	int N, K;
	cin >> N >> K;
	UnionFind uf( N );
	g = vector<vector<int>>( N );
	for( int i = 0; i < N - 1; i++ ) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		uf.unionSet( a, b );
		g[a].push_back( b );
		g[b].push_back( a );
	}

	if( K <= uf.size( 0 ) ) {
		ans = INT32_MAX;
		ans = dfs( 0, -1, K, 0 );
	}

	cout << ans << endl;
}
0