結果
| 問題 | No.277 根掘り葉掘り |
| コンテスト | |
| ユーザー |
FF256grhy
|
| 提出日時 | 2015-09-05 05:06:23 |
| 言語 | C++11 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 24 ms / 3,000 ms |
| コード長 | 1,045 bytes |
| 記録 | |
| コンパイル時間 | 184 ms |
| コンパイル使用メモリ | 41,668 KB |
| 実行使用メモリ | 49,256 KB |
| 最終ジャッジ日時 | 2026-04-02 13:53:09 |
| 合計ジャッジ時間 | 1,322 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
#include <stdio.h>
#define MAX 100000
int last[MAX], first[MAX], ans[MAX], queue[MAX], get, set;
int value[MAX * 2], next[MAX * 2], ptr;
int main(void) {
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) {
first[i] = -1;
ans[i] = MAX;
}
for(i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
x--; y--;
if(first[x] == -1) { first[x] = ptr; } else { next[ last[x] ] = ptr; }
last[x] = ptr; value[ptr] = y; next[ptr] = -1; ptr++;
if(first[y] == -1) { first[y] = ptr; } else { next[ last[y] ] = ptr; }
last[y] = ptr; value[ptr] = x; next[ptr] = -1; ptr++;
}
for(i = 1; i < n; i++) {
if(first[i] == last[i]) {
ans[i] = 0;
queue[set] = i;
set++;
}
}
ans[0] = 0;
queue[set] = 0;
set++;
while(get < set) {
int v = queue[get];
int p = first[v];
while(p != -1) {
if(ans[v] + 1 < ans[ value[p] ]) {
ans[ value[p] ] = ans[v] + 1;
queue[set] = value[p];
set++;
}
p = next[p];
}
get++;
}
for(i = 0; i < n; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
FF256grhy