結果

問題 No.277 根掘り葉掘り
ユーザー FF256grhyFF256grhy
提出日時 2015-09-05 05:06:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 37 ms / 3,000 ms
コード長 1,045 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 26,272 KB
実行使用メモリ 6,100 KB
最終ジャッジ日時 2023-09-26 08:42:15
合計ジャッジ時間 2,482 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,508 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 35 ms
6,088 KB
testcase_10 AC 29 ms
5,980 KB
testcase_11 AC 35 ms
6,036 KB
testcase_12 AC 35 ms
6,012 KB
testcase_13 AC 37 ms
6,012 KB
testcase_14 AC 35 ms
6,052 KB
testcase_15 AC 35 ms
6,012 KB
testcase_16 AC 36 ms
6,100 KB
testcase_17 AC 36 ms
6,072 KB
testcase_18 AC 36 ms
6,012 KB
testcase_19 AC 36 ms
6,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define MAX 100000

int last[MAX], first[MAX], ans[MAX], queue[MAX], get, set;
int value[MAX * 2], next[MAX * 2], ptr;

int main(void) {
	int n, i;
	scanf("%d", &n);
	
	for(i = 0; i < n; i++) {
		first[i] = -1;
		ans[i] = MAX;
	}
	
	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; value[ptr] = y; next[ptr] = -1; ptr++;
		
		if(first[y] == -1) { first[y] = ptr; } else { next[ last[y] ] = ptr; }
		last[y] = ptr; value[ptr] = x; next[ptr] = -1; ptr++;
	}
	
	for(i = 1; i < n; i++) {
		if(first[i] == last[i]) {
			ans[i] = 0;
			queue[set] = i;
			set++;
		}
	}
	ans[0] = 0;
	queue[set] = 0;
	set++;
	
	while(get < set) {
		int v = queue[get];
		int p = first[v];
		while(p != -1) {
			if(ans[v] + 1 < ans[ value[p] ]) {
				ans[ value[p] ] = ans[v] + 1;
				queue[set] = value[p];
				set++;
			}
			p = next[p];
		}
		get++;
	}
	
	for(i = 0; i < n; i++) {
		printf("%d\n", ans[i]);
	}
	
	return 0;
}
0