結果

問題 No.1582 Vertexes vs Edges
ユーザー pengin_2000pengin_2000
提出日時 2021-07-03 10:13:18
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,863 bytes
コンパイル時間 1,266 ms
コンパイル使用メモリ 30,452 KB
実行使用メモリ 5,456 KB
最終ジャッジ日時 2023-09-12 15:05:55
合計ジャッジ時間 4,555 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 0 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
int u[200005], v[200005];
int h[200005], l;
int comp_h(int a, int b)
{
	if (u[h[a]] > u[h[b]])
		return 1;
	else
		return -1;
}
void swap_h(int a, int b)
{
	int f = h[a];
	h[a] = h[b];
	h[b] = f;
	return;
}
void push(int ne)
{
	h[l] = ne;
	int p = l;
	l++;
	for (; p > 0; p = (p - 1) / 2)
		if (comp_h((p - 1) / 2, p) > 0)
			swap_h((p - 1) / 2, p);
	return;
}
int pop()
{
	l--;
	swap_h(0, l);
	int p = 0;
	for (;;)
	{
		if (2 * p + 2 < l)
		{
			if (comp_h(2 * p + 1, 2 * p + 2) > 0)
			{
				if (comp_h(p, 2 * p + 2) > 0)
					swap_h(p, 2 * p + 2);
				p = 2 * p + 2;
			}
			else
			{
				if (comp_h(p, 2 * p + 1) > 0)
					swap_h(p, 2 * p + 1);
				p = 2 * p + 1;
			}
		}
		else if (2 * p + 1 < l)
		{
			if (comp_h(p, 2 * p + 1) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[l];
}
int main()
{
	int n;
	scanf("%d", &n);
	int i;
	for (i = 0; i < n - 1; i++)
	{
		i *= 2;
		scanf("%d %d", &u[i], &v[i]);
		u[i]--;
		v[i]--;
		u[i + 1] = v[i];
		v[i + 1] = u[i];
		i /= 2;
	}
	l = 0;
	for (i = 0; i < 2 * n - 2; i++)
		push(i);
	int c[200005];
	for (i = 0; i < 2 * n - 2; i++)
		c[i] = pop();
	int depth[100005];
	for (i = 0; i < n; i++)
		depth[i] = -1;
	int stack[100005];
	l = 0;
	depth[0] = 0;
	stack[0] = 0;
	l++;
	int min, mid, max;
	c[2 * n - 2] = 2 * n - 2;
	u[2 * n - 2] = -1;
	while (l > 0)
	{
		l--;
		i = stack[l];
		min = -1;
		max = 2 * n - 2;
		while (max - min > 1)
		{
			mid = (max + min) / 2;
			if (u[c[mid]] < i)
				min = mid;
			else
				max = mid;
		}
		for (; u[c[max]] == i; max++)
		{
			if (depth[v[c[max]]] < 0)
			{
				depth[v[c[max]]] = depth[i] + 1;
				stack[l] = v[c[max]];
				l++;
			}
		}
	}
	int cnt[2];
	cnt[0] = cnt[1] = 0;
	for (i = 0; i < n; i++)
		cnt[depth[i] % 2]++;
	if (cnt[0] < cnt[1])
		printf("%d\n", cnt[0]);
	else
		printf("%d\n", cnt[1]);
	return 0;
}
0