結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
puru
|
| 提出日時 | 2016-04-23 02:25:49 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 3,000 ms |
| コード長 | 1,865 bytes |
| コンパイル時間 | 1,165 ms |
| コンパイル使用メモリ | 166,376 KB |
| 実行使用メモリ | 10,900 KB |
| 最終ジャッジ日時 | 2024-10-04 15:15:31 |
| 合計ジャッジ時間 | 2,724 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:42:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
42 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:46:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
46 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
template<class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
struct prepare {
prepare() {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(8);
ios_base::sync_with_stdio(false);
}
} _prepare;
const int MAX_N = (int) (1e5) + 1;
vi edges[MAX_N];
int R[MAX_N];
int L[MAX_N];
int main() {
int N;
scanf("%d", &N);
rep(i, N - 1) {
int x, y;
scanf("%d%d", &x, &y);
--x, --y;
edges[x].push_back(y);
edges[y].push_back(x);
}
memset(R, -1, sizeof(R));
fill(L, L + MAX_N, INF);
queue<pii> que, leaf_que;
que.push(mp(0, 0));
while (!que.empty()) {
pii qp = que.front();
que.pop();
int v = qp.first;
int d = qp.second;
R[v] = d;
bool has_child = false;
tr(it, edges[v]) {
if (R[*it] == -1) {
que.push(mp(*it, d + 1));
has_child = true;
}
}
if (!has_child)
leaf_que.push(mp(v, 0));
}
while (!leaf_que.empty()) {
pii qp = leaf_que.front();
leaf_que.pop();
const int v = qp.first;
const int d = qp.second;
if (!chmin(L[v], d))
continue;
tr(it, edges[v]) {
if (L[*it] > d + 1) {
leaf_que.push(mp(*it, d + 1));
}
}
}
rep(i, N)
printf("%d\n", min(R[i], L[i]));
return 0;
}
puru