結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 00:58:50
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 831 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 25,560 KB
実行使用メモリ 8,444 KB
最終ジャッジ日時 2023-09-26 07:51:27
合計ジャッジ時間 2,894 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 RE -
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 RE -
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 40 ms
8,444 KB
testcase_10 RE -
testcase_11 AC 37 ms
5,712 KB
testcase_12 AC 37 ms
7,312 KB
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 100000

int calc(int, int);

int lr[3][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 i, int c) {
	ne[i] = c;
	memo[i] = 1;
	
	int m = MAX, temp;
	if(memo[ lr[0][i] ] == 0) { temp = calc(lr[0][i], c + 1) + 1; m = (temp < m ? temp : m); }
	if(memo[ lr[1][i] ] == 0) { temp = calc(lr[1][i], c + 1) + 1; m = (temp < m ? temp : m); }
	if(memo[ lr[2][i] ] == 0) { temp = calc(lr[2][i], c + 1) + 1; m = (temp < m ? temp : m); }
	m = (m == MAX ? 0 : m);
	ha[i] = m;
	return m;
}
0