結果

問題 No.277 根掘り葉掘り
ユーザー bal4ubal4u
提出日時 2019-05-10 14:29:10
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 39 ms / 3,000 ms
コード長 1,383 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 30,008 KB
実行使用メモリ 14,140 KB
最終ジャッジ日時 2023-09-14 17:54:08
合計ジャッジ時間 3,076 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 32 ms
14,140 KB
testcase_10 AC 13 ms
7,592 KB
testcase_11 AC 22 ms
7,184 KB
testcase_12 AC 39 ms
11,796 KB
testcase_13 AC 22 ms
7,092 KB
testcase_14 AC 21 ms
7,668 KB
testcase_15 AC 24 ms
8,204 KB
testcase_16 AC 23 ms
7,676 KB
testcase_17 AC 22 ms
7,796 KB
testcase_18 AC 22 ms
7,360 KB
testcase_19 AC 21 ms
7,336 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:27:17: 備考: in expansion of macro ‘pc’
   27 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.277 根掘り葉掘り
// 2019.5.10 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in()
{
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(int n)
{
	int i;
	char ob[20];

	if (!n) pc('0');
	else {
		i = 0; while (n) ob[i++] = n%10 + '0', n/=10;
		while (i--) pc(ob[i]);
	}
	pc('\n');
}

#define MAX 100003
#define INF 0x33
int N;
int *to[MAX], hi[MAX];
int ans[MAX];

void dfs(int node, int par, int h) {
	int i;
	if (ans[node] > h) ans[node] = h;
	if (node && hi[node] == 1) {
		ans[node] = 0;
		if (ans[par] > 1) dfs(par, node, 1);
	} else {
		for (i = 0; i < hi[node]; i++) {
			if (ans[to[node][i]] > h+1) dfs(to[node][i], node, h+1);
		}
	}
}

int main()
{
	int i, x, y;
	int *memo, sz;
	
	N = in();
	memo = malloc(sizeof(int)*2*N);
	sz = 0; for (i = 1; i < N; i++) {
		memo[sz++] = x = in()-1, memo[sz++] = y = in()-1;
		hi[x]++, hi[y]++;
	}
	for (i = 0; i < N; i++) if (hi[i]) {
		to[i] = malloc(sizeof(int)*hi[i]);
	}
	memset(hi, 0, sizeof(int)*N);
	i = 0; while (i < sz) {
		x = memo[i++], y = memo[i++];
		to[x][hi[x]++] = y, to[y][hi[y]++] = x;
	}
	memset(ans, INF, sizeof(int)*N);
	dfs(0, -1, 0);
	for (i = 0; i < N; i++) out(ans[i]);
	return 0;
}
0