結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 00:50:10
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 918 bytes
コンパイル時間 477 ms
コンパイル使用メモリ 25,780 KB
実行使用メモリ 10,016 KB
最終ジャッジ日時 2023-09-26 07:49:13
合計ジャッジ時間 3,183 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 RE -
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 39 ms
10,016 KB
testcase_10 AC 26 ms
4,376 KB
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 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[i] ][x] = y; count[x]++;
		lr[ count[i] ][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 d = (memo[ lr[0][i] ] == 0) + (memo[ lr[1][i] ] == 0) + (memo[ lr[2][i] ] == 0);
	if(d == 0) { return 0; }
	
	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); }
	ha[i] = m;
	return m;
}
0