結果
問題 |
No.277 根掘り葉掘り
|
ユーザー |
![]() |
提出日時 | 2015-07-14 02:45:39 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 675 bytes |
コンパイル時間 | 518 ms |
コンパイル使用メモリ | 62,428 KB |
実行使用メモリ | 18,304 KB |
最終ジャッジ日時 | 2024-07-08 07:06:01 |
合計ジャッジ時間 | 3,358 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 10 WA * 8 |
ソースコード
//木DPっぽい嘘解法 #include <iostream> #include <vector> using namespace std; const int INF = 1e8; int dfs(vector<vector<int>>& G, vector<int>& dist, int pos, int last, int d){ int x = INF; for(int i=0; i<G[pos].size(); i++){ if(G[pos][i] == last) continue; x = min(x, dfs(G, dist, G[pos][i], pos, d+1) + 1); } if(x == INF) x = 0; dist[pos] = min(x, d); return x; } int main(){ int n; cin >> n; vector<vector<int>> G(n); for(int i=0; i<n-1; i++){ int x,y; cin >> x >> y; x--; y--; G[x].push_back(y); G[y].push_back(x); } vector<int> dist(n, INF); dfs(G,dist, 0,-1,0); for(int i=0; i<n; i++){ cout << dist[i] << endl; } return 0; }