結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 02:38:15
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 859 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 25,760 KB
実行使用メモリ 10,836 KB
最終ジャッジ日時 2023-09-26 08:32:13
合計ジャッジ時間 1,871 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 38 ms
10,836 KB
testcase_10 AC 29 ms
6,016 KB
testcase_11 AC 35 ms
6,036 KB
testcase_12 AC 36 ms
8,356 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>

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