結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 01:21:09
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 703 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 25,472 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-26 07:55:28
合計ジャッジ時間 1,456 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
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 100000

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