結果
問題 |
No.277 根掘り葉掘り
|
ユーザー |
![]() |
提出日時 | 2015-09-05 03:23:59 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 985 bytes |
コンパイル時間 | 119 ms |
コンパイル使用メモリ | 23,168 KB |
実行使用メモリ | 12,416 KB |
最終ジャッジ日時 | 2024-07-19 03:40:11 |
合計ジャッジ時間 | 1,494 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 10 WA * 8 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:11:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 11 | 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 100000 int calc(int, int); int last[MAX], first[MAX], vertex[MAX * 2], next[MAX * 2], ptr, visited[MAX], ans[MAX]; int main(void) { int n, i; scanf("%d", &n); for(i = 0; i < n; i++) { first[i] = -1; } 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; vertex[ptr] = y; next[ptr] = -1; ptr++; if(first[y] == -1) { first[y] = ptr; } else { next[ last[y] ] = ptr; } last[y] = ptr; vertex[ptr] = x; next[ptr] = -1; ptr++; } calc(0, 0); for(i = 0; i < n; i++) { printf("%d\n", ans[i]); } return 0; } int calc(int v, int ne) { visited[v] = 1; int ha = MAX; int p = first[v]; while(p != -1) { if(visited[ vertex[p] ] == 0) { int temp = calc(vertex[p], ne + 1) + 1; ha = (temp < ha ? temp : ha); } p = next[p]; } ha = (ha == MAX ? 0 : ha); ans[v] = (ne < ha ? ne : ha); return ha; }