結果
問題 |
No.277 根掘り葉掘り
|
ユーザー |
![]() |
提出日時 | 2015-09-05 02:38:15 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 859 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 23,168 KB |
実行使用メモリ | 12,416 KB |
最終ジャッジ日時 | 2024-07-19 03:35:40 |
合計ジャッジ時間 | 1,500 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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:20:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 20 | scanf("%d%d", &x, &y); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <stdio.h> #define MAX 100000 int calc(int, int); int last[MAX], vertex[MAX * 3], next[MAX * 3], ptr, visited[MAX], ans[MAX]; int main(void) { int n, i; scanf("%d", &n); for(i = 0; i < n; i++) { last[i] = i; ptr++; } for(i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; vertex[ptr] = y; next[ptr] = -1; next[ last[x] ] = ptr; last[x] = ptr; ptr++; vertex[ptr] = x; next[ptr] = -1; next[ last[y] ] = ptr; last[y] = ptr; ptr++; } calc(0, 0); for(i = 0; i < n; i++) { printf("%d\n", ans[i]); } return 0; } int calc(int v, int c) { visited[v] = 1; int m = MAX; int p = next[v]; while(p != -1) { if(visited[ vertex[p] ] == 0) { int temp = calc(vertex[p], c + 1) + 1; m = (temp < m ? temp : m); } p = next[p]; } m = (m == MAX ? 0 : m); ans[v] = (c < m ? c : m); return m; }