結果
問題 | No.277 根掘り葉掘り |
ユーザー |
![]() |
提出日時 | 2020-11-26 08:15:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 221 ms / 3,000 ms |
コード長 | 607 bytes |
コンパイル時間 | 753 ms |
コンパイル使用メモリ | 78,900 KB |
実行使用メモリ | 9,848 KB |
最終ジャッジ日時 | 2024-07-23 20:42:30 |
合計ジャッジ時間 | 4,217 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <iostream> #include <vector> #include <queue> using namespace std; int main(){ int N; cin >> N; vector<vector<int>> E(N); for (int i = 0; i < N - 1; i++){ int x, y; cin >> x >> y; x--; y--; E[x].push_back(y); E[y].push_back(x); } vector<int> d(N, -1); queue<int> Q; d[0] = 0; Q.push(0); for (int i = 1; i < N; i++){ if (E[i].size() == 1){ d[i] = 0; Q.push(i); } } while (!Q.empty()){ int v = Q.front(); Q.pop(); for (int w : E[v]){ if (d[w] == -1){ d[w] = d[v] + 1; Q.push(w); } } } for (int i = 0; i < N; i++){ cout << d[i] << endl; } }