[SSL] 1224 & [Logu] P2024 Food Chain

[SSL] 1224 & [Logu] P2024 Food Chain

Time Limit:1000MS
Memory Limit:65536K

Description

There are three types of animals in the animal kingdom, A, B, and C. The food chain of these three types of animals forms an interesting ring. A eats B, B eats C, and C eats A.
There are currently N animals, numbered from 1 to N. Every animal is one of A, B, and C, but we don't know which one it is.
Some people describe the food chain relationship formed by these N animals in two ways: the
first one is "1 XY", which means that X and Y are of the same kind.
The second argument is "2 XY", which means X eats Y.
This person uses the above two statements to say K sentences to N animals. Some of these K sentences are true and some are false. When a sentence satisfies one of the following three items, the sentence is false, otherwise it is true.
1) The current words conflict with some previous true words, which is a lie;
2) The current words X or Y is greater than N, which is a lie;
3) The current words mean that X eats X, which is a lie.
Your task is to output the total number of lies based on the given N (1<=N<=50,000) and K sentences (0<=K<=100,000).

Input

The first line is two integers N and K, separated by a space.
Each of the following K lines contains three positive integers D, X, Y, separated by a space between the two numbers, where D represents the type of argument.
If D=1, it means that X and Y are of the same kind.
If D=2, it means X eats Y.

Output

There is only one integer, which represents the number of lies.

Sample Input

Input file analysis of 7 sentences
100 7
1 101 1 lie
2 1 2 truth
2 2 3 truth
2 3 3 lie
1 1 3 lie
2 3 1 truth
1 5 5 truth

Sample Output

3

Hint

NOI2001

Ideas

Obviously, the processing of false conditions 2 and 3 is very simple, as long as the two conditions are judged when reading in the data, the main task of the problem is to deal with condition 1.
On the surface, it seems that the treatment of condition 1 is not difficult: an animal is nothing more than three types of A, B, and C, and the food chain relationship between A, B, and C is one-to-one one-way ring, that is to say If the category of animal X and the food chain relationship between X and Y are known, the category of animal Y can be determined. At the same time, which category an animal belongs to does not affect the result of this question, but only requires it to be related to other animals The relative position of the relationship is correct.
Therefore, we might as well open 3 arrays A, B, and C to record the three types of members. First, suppose that the animal X in the first valid sentence is of type A, and put it into array A. If Y is the same as X, Then put Y into A; if Y is eaten by X, put Y into B. Repeatedly operating all the valid words, you can determine the type of each animal and easily count the number of lies.
The problem seems to have been solved satisfactorily, but if you think about it carefully, you will find that the above algorithm has major errors and is very one-sided.
For a creature with unknown attributes, we all adopt the definition of type A, which is obviously wrong.
It can be seen that this algorithm can only be used when every sentence can directly establish a clear relationship with the previously known food chain.
With the above relationship clarified, it is not difficult for us to extend another algorithm from the previous algorithm: For animal X whose relationship is currently unknown, we will open up a new food chain A2, B2, C2 for him. Obviously, in this new group , The type of animal X is also arbitrary, so it is assumed that it is in group A2. In this way, all relationships with X can be added to this group in the same way as Algorithm 1, and this group is the same as the original group A1, B1, C1 The relationship is uncertain. Repeating this way, we can also get Group 3, Group 4, Group 5... Once there is a sentence involving the food chain relationship between the members of certain two groups, we will combine these two combinations according to certain conversion rules To ensure the integrity of the network of relationships.
Through the above analysis, and the application of the search set in this question is ready to come out.
There are three types of elements in a set. When merging sets, the three types of elements need to be merged.

Code

#include<iostream>
#include<cstdio>
using namespace std;
int f[300010];//s本身,s+n s的食物,s+n+n s的天敌 
int find(int x)//找代表值 
{
    
    
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
void merge(int x,int y)//合并
{
    
    
	f[find(x)]=find(y);
	return;
}
int main()
{
    
    
	int n,k,i,t,x,y,ans=0;
	scanf("%d%d",&n,&k);
	for(i=1;i<=3*n;f[i]=i,i++);//初始化
	for(i=1;i<=k;i++)
	{
    
    
		scanf("%d%d%d",&t,&x,&y);
		if(x>n||y>n||(t==1&&(find(x+n)==find(y)||find(x)==find(y+n)))||(t==2&&(find(x)==find(y)||find(x)==find(y+n))))
			ans++;
		else
			if(t==1)
			{
    
    
				merge(x,y);//合并 
				merge(x+n,y+n);
				merge(x+n+n,y+n+n);
			}
			else
			{
    
    
				merge(x,y+n+n);
				merge(x+n,y);
				merge(x+n+n,y+n);
			}
	}
	printf("%d",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/113003603