結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2015-09-05 00:44:58 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 215 ms / 3,000 ms |
コード長 | 887 bytes |
コンパイル時間 | 763 ms |
コンパイル使用メモリ | 73,364 KB |
実行使用メモリ | 10,100 KB |
最終ジャッジ日時 | 2024-07-19 02:49:52 |
合計ジャッジ時間 | 4,188 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <utility> #include <queue> using namespace std; const int INF = 1e8; int main() { int n, x, y; cin >> n; vector< vector<int> > next(n+1, vector<int>()); for (int i = 1; i < n; i++) { cin >> x >> y; next[x].push_back(y); next[y].push_back(x); } queue<pair<int, int> > que; pair<int, int> p; vector<int> dist(n+1, INF); for (int i = 1; i <= n; i++) { if (i == 1 || next[i].size() == 1) { dist[i] = 0; que.push(make_pair(i, 0)); } } int z; while (!que.empty()) { p = que.front(); que.pop(); for (int i = 0; i < next[p.first].size(); i++) { z = next[p.first][i]; if (p.second + 1 < dist[z]) { dist[z] = p.second + 1; que.push(make_pair(z, p.second + 1)); } } } for (int i = 1; i <= n; i++) { cout << dist[i] << endl; } return 0; }