結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 01:21:34
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 702 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 25,536 KB
実行使用メモリ 191,984 KB
最終ジャッジ日時 2023-09-26 07:55:53
合計ジャッジ時間 13,441 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
5,120 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define MAX 10000

int calc(int, int);

int lr[MAX][MAX], count[MAX], memo[MAX], ne[MAX], ha[MAX];

int main(void) {
	int n, i;
	scanf("%d", &n);
	for(i = 0; i < n - 1; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--; y--;
		lr[ count[x] ][x] = y; count[x]++;
		lr[ count[y] ][y] = x; count[y]++;
	}
	
	calc(0, 0);
	
	for(i = 0; i < n; i++) {
		printf("%d\n", ne[i] < ha[i] ? ne[i] : ha[i]);
	}
	
	return 0;
}

int calc(int v, int c) {
	ne[v] = c;
	memo[v] = 1;
	
	int m = MAX, temp;
	int i;
	for(i = 0; i < count[v]; i++) {
		if(memo[ lr[i][v] ] == 0) {
			temp = calc(lr[i][v], c + 1) + 1;
			m = (temp < m ? temp : m); 
		}
	}
	m = (m == MAX ? 0 : m);
	ha[v] = m;
	return m;
}
0