結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
FF256grhy
|
| 提出日時 | 2015-09-05 04:07:31 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 995 bytes |
| コンパイル時間 | 134 ms |
| コンパイル使用メモリ | 23,552 KB |
| 実行使用メモリ | 12,544 KB |
| 最終ジャッジ日時 | 2024-07-19 03:40:48 |
| 合計ジャッジ時間 | 1,406 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 10 WA * 8 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:12:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
12 | 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>
// test
#define MAX 10000000
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;
}
FF256grhy