結果
問題 | No.277 根掘り葉掘り |
ユーザー |
![]() |
提出日時 | 2015-09-05 05:06:23 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 35 ms / 3,000 ms |
コード長 | 1,045 bytes |
コンパイル時間 | 157 ms |
コンパイル使用メモリ | 23,808 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-19 03:43:40 |
合計ジャッジ時間 | 1,627 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:10:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 10 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:19:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 19 | scanf("%d%d", &x, &y); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <stdio.h>#define MAX 100000int 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;}