結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 03:23:59
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 985 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 25,916 KB
実行使用メモリ 10,700 KB
最終ジャッジ日時 2023-09-26 08:38:10
合計ジャッジ時間 2,158 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>

#define MAX 100000

int calc(int, int);

int last[MAX], first[MAX], vertex[MAX * 2], next[MAX * 2], ptr, visited[MAX], ans[MAX];

int main(void) {
	int n, i;
	scanf("%d", &n);
	
	for(i = 0; i < n; i++) {
		first[i] = -1;
	}
	
	for(i = 0; i < n - 1; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		x--; y--;
		
		if(first[x] == -1) { first[x] = ptr; } else { next[ last[x] ] = ptr; }
		last[x] = ptr; vertex[ptr] = y; next[ptr] = -1; ptr++;
		
		if(first[y] == -1) { first[y] = ptr; } else { next[ last[y] ] = ptr; }
		last[y] = ptr; vertex[ptr] = x; next[ptr] = -1; ptr++;
	}
	
	calc(0, 0);
	
	for(i = 0; i < n; i++) {
		printf("%d\n", ans[i]);
	}
	
	return 0;
}

int calc(int v, int ne) {
	visited[v] = 1;
	
	int ha = MAX;
	int p = first[v];
	while(p != -1) {
		if(visited[ vertex[p] ] == 0) {
			int temp = calc(vertex[p], ne + 1) + 1;
			ha = (temp < ha ? temp : ha); 
		}
		p = next[p];
	}
	ha = (ha == MAX ? 0 : ha);
	ans[v] = (ne < ha ? ne : ha);
	return ha;
}
0