結果

問題 No.277 根掘り葉掘り
ユーザー tekitouktekitouk
提出日時 2015-09-05 03:21:20
言語 C90
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,158 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 24,824 KB
実行使用メモリ 6,892 KB
最終ジャッジ日時 2023-09-26 08:36:53
合計ジャッジ時間 6,058 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <stdio.h>

int x[100000] = { 0 };
int y[100000] = { 0 };
int R[100000] = { 0 };
int L[100000] = { 0 };

int N;

void root_search(int depth,int now);
void leaf_search(int depth, int now);
int min(int a, int b);

int MAX_DEPTH = 0;;
int DEPTH;

int main(){

	scanf("%d",&N);

	int i;
	for (i = 0; i < N - 1; i++){
		scanf("%d %d", &x[i], &y[i]);
		x[i]--;
		y[i]--;
	}

	root_search(0, 0);

	for (i = 0; i < N; i++){
		DEPTH = 9999999;
		leaf_search(0, i);
		L[i] = DEPTH;
	}

	for (i = 0; i < N; i++){
		printf("%d\n",min(R[i],L[i]));
	}

	return 0;
}
void root_search(int depth,int now){

	if (MAX_DEPTH < depth){ MAX_DEPTH = depth; }

	R[now] = depth;

	int i;

	for (i = 0; i < N - 1; i++){
		if (x[i] == now){
			root_search(depth + 1, y[i]);
		}
	}


}
void leaf_search(int depth, int now){

	if (depth>MAX_DEPTH){ return; }

	int i;

	int cnt = 0;
	for (i = 0; i < N - 1; i++){
		if (x[i] == now){
			cnt++;
		}
	}
	if (cnt == 0){ DEPTH = min(DEPTH, depth); return; }
	else{
		for (i = 0; i < N - 1; i++){
			if (x[i] == now){
				leaf_search(depth + 1, y[i]);
			}
		}
	}
}
int min(int a, int b){
	if (a >= b){ return b; }
	else{ return a; }
}
0