为什么这种写法过不了
查看原帖
为什么这种写法过不了
569692
Broken_Light楼主2023/6/26 13:02

RT

#include <cstdio>

using namespace std;

const int Maxn = 1e5 + 20;

int N, K, ans;
int father[Maxn], d[Maxn];

inline int find(int x)
{
	if (x == father[x]) return x;
	int root = find(father[x]);
	
	d[x] = (d[x] + d[father[x]]) % 3;
	return father[x] = root;
}

int main()
{
	scanf("%d %d", &N, &K);
	for (int i = 1; i <= N; ++i) father[i] = i;
	for (int i = 1, u, x, y; i <= K; ++i)
	{
		scanf("%d %d %d", &u, &x, &y);
		if (x > N || y > N || (u == 2 && x == y)) {ans++; continue;}
		if (u == 1)
		{
			if (find(x) == find(y) && d[x] != d[y]) {ans++; continue;}
			else if (find(x) != find(y))
			{
				d[find(x)] = (d[y] - d[x] + 3) % 3;
				father[find(x)] = find(y);
			}
		}
		else
		{
			if (find(x) != find(y))
			{
				d[find(x)] = (d[y] - d[x] + 4) % 3;
				father[find(x)] = find(y);	
			}
			else if ((d[x] - d[y] + 3) % 3 != 1) {ans++; continue;}
		}
	}
	printf("%d\n", ans);
	return 0;
}

上面这种写法只有20分 但是下面这种就能AC

#include <cstdio>

using namespace std;

const int Maxn = 1e5 + 20;

int N, K, ans;
int father[Maxn], d[Maxn];

inline int find(int x)
{
	if (x == father[x]) return x;
	int root = find(father[x]);
	
	d[x] = (d[x] + d[father[x]]) % 3;
	return father[x] = root;
}

int main()
{
	scanf("%d %d", &N, &K);
	for (int i = 1; i <= N; ++i) father[i] = i;
	for (int i = 1, u, x, y; i <= K; ++i)
	{
		scanf("%d %d %d", &u, &x, &y);
		if (x > N || y > N) {ans++; continue;}
		
		int s = find(x), t = find(y);
		if (u == 1)
		{
			if (s == t && d[x] != d[y]) {ans++; continue;}
			else if (s != t)
			{
				d[s] = (d[y] - d[x] + 3) % 3;
				father[s] = t;
			}
		}
		else
		{
			if (s != t)
			{
				d[s] = (d[y] - d[x] + 4) % 3;
				father[s] = t;	
			}
			else if ((d[x] - d[y] + 3) % 3 != 1) {ans++; continue;}
		}
	}
	printf("%d\n", ans);
	return 0;
}

蒟蒻求问,百思不得其解

2023/6/26 13:02
加载中...