結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-03 16:09:11 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,132 bytes |
| コンパイル時間 | 602 ms |
| コンパイル使用メモリ | 69,056 KB |
| 実行使用メモリ | 18,560 KB |
| 最終ジャッジ日時 | 2024-11-21 15:18:26 |
| 合計ジャッジ時間 | 41,838 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 TLE * 10 |
ソースコード
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct treenode {
int num;
int depth;
int parent;
treenode(int num, int depth, int parent) : num(num), depth(depth), parent(parent) {}
};
int N, x, y;
vector<vector<int>> rinsetsu;
vector<int> leaves;
int ans[100001] = {};
void dfs(int start, bool happa) {
stack<treenode> st;
st.emplace(start, 0, -1);
while (!st.empty()) {
treenode n = st.top();
st.pop();
if (ans[n.num] > n.depth)
ans[n.num] = n.depth;
if (happa && (int)rinsetsu[n.num].size() == 1 && n.parent > -1) {
leaves.push_back(n.num);
continue;
}
for (auto &nn:rinsetsu[n.num]) {
if (nn != n.parent)
st.emplace(nn, n.depth+1, n.num);
}
}
}
int main() {
cin >> N;
rinsetsu = vector<vector<int>>(N);
for (int i = 0; i < N-1; i++) {
cin >> x >> y;
rinsetsu[x-1].push_back(y-1);
rinsetsu[y-1].push_back(x-1);
}
for (int i = 0; i < N; i++) {
ans[i] = 200000;
}
dfs(0, true);
for (auto &leaf:leaves)
dfs(leaf, false);
for (int i = 0; i < N; i++)
cout << ans[i] << endl;
return 0;
}