結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-17 13:24:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 241 ms / 3,000 ms |
| コード長 | 743 bytes |
| コンパイル時間 | 3,144 ms |
| コンパイル使用メモリ | 197,804 KB |
| 最終ジャッジ日時 | 2025-01-19 17:53:58 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int INF = 1<<28;
int n;
vector<int> g[100000];
int dist[100000];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> 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);
}
fill(dist, dist + n, INF);
queue<int> q;
for (int i = 0; i < n; i++) {
if (i == 0 || g[i].size() == 1) {
q.push(i);
dist[i] = 0;
}
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (int w: g[v]) {
if (dist[w] == INF) {
dist[w] = dist[v] + 1;
q.push(w);
}
}
}
for (int i = 0; i < n; i++)
cout << dist[i] << endl;
return 0;
}