結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2018-10-31 19:06:28 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,195 bytes |
コンパイル時間 | 801 ms |
コンパイル使用メモリ | 79,544 KB |
実行使用メモリ | 13,200 KB |
最終ジャッジ日時 | 2024-11-19 12:43:02 |
合計ジャッジ時間 | 3,011 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 6 WA * 12 |
ソースコード
#include<iostream> #include<vector> #include<stack> using namespace std; int main(){ int n; cin >> n; int INF = 100001; vector<vector<int>> str(n+1), rev(n+1); vector<int> r(n+1, INF), l(n+1, INF); bool notLeaf[INF] = {}; int x, y; for(int i = 0; i < n-1; i++){ cin >> x >> y; str[x].push_back(y); notLeaf[x] = true; rev[y].push_back(x); } stack<pair<int,int>> s; s.push({1, 0}); while(!s.empty()){ pair<int,int> p = s.top(); s.pop(); r[p.first] = p.second; for(int next : str[p.first]){ s.push({next, p.second+1}); } } for(int i = 1; i <= n; i++){ if(notLeaf[i]) continue; s.push({i, 0}); while(!s.empty()){ pair<int,int> now = s.top(); s.pop(); if(now.second >= l[now.first]) continue; l[now.first] = now.second; for(int next : rev[now.first]){ if(now.second + 1 < l[next]){ s.push({next, now.second + 1}); } } } } for(int i = 1; i <= n; i++) printf("%d\n", min(l[i], r[i])); return 0; }