結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 04:07:31
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 995 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 25,648 KB
実行使用メモリ 17,088 KB
最終ジャッジ日時 2023-09-26 08:39:01
合計ジャッジ時間 1,765 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,168 KB
testcase_01 AC 3 ms
11,148 KB
testcase_02 WA -
testcase_03 AC 3 ms
11,220 KB
testcase_04 AC 3 ms
11,152 KB
testcase_05 AC 3 ms
11,244 KB
testcase_06 AC 3 ms
11,228 KB
testcase_07 AC 3 ms
11,164 KB
testcase_08 AC 3 ms
11,224 KB
testcase_09 AC 40 ms
17,088 KB
testcase_10 AC 31 ms
12,388 KB
testcase_11 AC 37 ms
12,316 KB
testcase_12 AC 39 ms
14,744 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0