結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
hanorver
|
| 提出日時 | 2015-09-04 23:48:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,233 bytes |
| コンパイル時間 | 550 ms |
| コンパイル使用メモリ | 70,412 KB |
| 実行使用メモリ | 9,088 KB |
| 最終ジャッジ日時 | 2024-07-19 02:30:23 |
| 合計ジャッジ時間 | 3,566 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 6 WA * 12 |
ソースコード
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
struct tree {
std::vector<int> child;
int dist_root;
int dist_leaf;
int parent;
};
int main() {
int n;
std::cin >> n;
std::vector<tree> v(n);
for (int i = 0; i < n - 1; i++) {
int from, to;
std::cin >> from >> to;
v[from - 1].child.push_back(to - 1);
}
std::stack<int> s, leafpoint;
s.push(0);
v[0].dist_root = 0;
int dist = 1;
while (!s.empty()) {
int node = s.top();
s.pop();
for (int i = 0; i < v[node].child.size(); i++) {
v[v[node].child[i]].dist_root = v[node].dist_root + 1;
v[v[node].child[i]].parent = node;
s.push(v[node].child[i]);
}
v[node].dist_leaf = 99999;
if (v[node].child.size() == 0)leafpoint.push(node);
}
std::stack<int> parent;
while (!leafpoint.empty()) {
int node = leafpoint.top();
leafpoint.pop();
v[node].dist_leaf = 0;
parent.push(v[node].parent);
int dist = 1;
while (!parent.empty()) {
int pa = parent.top();
parent.pop();
v[pa].dist_leaf = std::min(v[pa].dist_leaf, dist);
if (pa == 0)break;
parent.push(v[pa].parent);
dist++;
}
}
for (int i = 0; i < n; i++) {
std::cout << std::min(v[i].dist_root, v[i].dist_leaf) << std::endl;
}
return 0;
}
hanorver