結果
問題 |
No.994 ばらばらコイン
|
ユーザー |
![]() |
提出日時 | 2020-03-25 15:39:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,153 bytes |
コンパイル時間 | 1,552 ms |
コンパイル使用メモリ | 166,884 KB |
実行使用メモリ | 9,216 KB |
最終ジャッジ日時 | 2025-01-01 23:54:52 |
合計ジャッジ時間 | 3,223 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 WA * 13 |
ソースコード
#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; }