⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pku1182.cpp

📁 这是ACM 方面的资料 是PKU的 北京大学的出来的
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50010

int nd[SIZE], st[SIZE];
int D, X, Y, cnt, hdX, hdY, N, K;
void init()
{
	int i;
	cnt = 0;
	for (i = 0; i <= N; i++)
	{
		nd[i] = st[i] = 0;
	}
}

int get_id(int x)
{
	int root;
	if (nd[x] == 0)
	{
		st[x] = 0;
		return x;
	}

	root = get_id(nd[x]);
	st[x] = (st[x] + st[nd[x]]) % 3;
	nd[x] = root;
	return root;
}

int main()
{
	int i;
	scanf("%d%d", &N, &K);
	init();
	for (i = 0; i < K && scanf("%d%d%d", &D, &X, &Y) != -1; i++)
	{
		if (X > N || Y > N)
		{
			cnt++;
			continue;
		}
		if (X == Y && D == 2)
		{
			cnt++;
			continue;
		}
		hdX = get_id(X);
		hdY = get_id(Y);
		if (hdX != hdY)
		{
			nd[hdY] = hdX;
			st[hdY] = (st[X] + D + 2 - st[Y]) % 3;
		}
		else
		{
			if ((st[Y] - st[X] + 3) % 3 != D - 1)
			{
				cnt++;
			}
		}
	}
	printf("%d\n", cnt);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -