[Explanations] && disjoint-set food chain

Topic Portal

Subject description:

There are three types of animals in the animal kingdom A, B, C, three types of animal food chain constitute an interesting ring. A food B, B eat C, C eat A. Animals prior N, number to 1-N. Each animal is A, B, C in kind, but we do not know it in the end is what kind of. It was carried out with two versions of this trophic animals consisting of N Description:
The first argument is "1 XY", it represents the X and Y are similar.
The second argument is "2 XY", X represents eat Y.
This person for N animals, with the above two statements, one sentence by sentence to say K, K this sentence some true, some false. When one of the following three words, this sentence is a lie, the truth is otherwise.
1) if the current true, then some of the previous conflicts, is lie;
2), then the current X or Y is larger than N, is lie;
3) X represents eat, then the current X, is lie.
Your job is given according to N (1 <= N <= 50,000) and K words (0 <= K <= 100,000 ), the total number of output lies.


The input format
of the first two lines are integers N and K, separated by a space. K The following three lines each is a positive integer D, X, Y, separated by a space between the two numbers, where D indicates the type of argument. If D = 1, it indicates that X and Y are similar. If D = 2, then X represents eat Y.

Output formats
only one integer representing the number of lies.


analysis:

This question is transformed after the meaning of the title is actually a complex deal with the relationship.
Because this question need to deal with similar food predators relationship between.

Think of the relationship between processing, we might think of two ways: Map Map and and check Collection Disjoint-set
the problem in a graph is not can not do, but apparently with a disjoint-set than the trouble.
So this question is a subject of investigation and collection.

  • At this time, the introduction of what disjoint-set Spread exhibition area Extension field concepts.
    In fact,more disjoint-setjointly deal with the relationship

So we meaning of the questions, can be found given three disjoint-set, that is, extend the length of the disjoint-set to three times as long.
Note: It is best not to use to store multiple disjoint-set relationship, because there is no common ground between them, but also for the relationship between the processing error occurs (that is, so to miss the ............)

We are located:
f a [ i ] table Show With i for Edit number of father Dear FA [i] i is expressed in number of father
1 Office Reason i of with class of turn off system   ( 1 &lt; = i &lt; = n ) 1, the processing similar relationship i \ (1 & lt; = i & lt; = n)
2 i   ( n + 1 &lt; = i &lt; = 2 n ) 2, the relationship between the processing of natural enemies i \ (n + 1 & lt; = i & lt; = 2 * n)
3 i ( 2 n + 1 &lt; = i &lt; = 3 n ) 3, the relationship between food treated i (2 * n + 1 & lt; = i & lt; = 3 * n)

So the title is a lie to say before the conflict with the words, then how about the conflict with the previous count it?
There are four cases:
1 1, eat the same kind
2 2, similar eating predators
3 3, and predators are similar
4 4, and the food is similar

So long as our relationship can be processed in accordance with the above four.


Code

/*
1、同类吃同类
2、同类吃天敌
3、和天敌是同类
4、 和食物是同类 

1-n 同类
n+1-2*n 天敌
2*n+1-3*n 食物
*/
#include<bits/stdc++.h>
using namespace std;
int fa[1000001];
int n,m;
int ans=0;
int getfa(int k){
    return k==fa[k]?k:fa[k]=getfa(fa[k]);
}
int main(){
	freopen("eat.in","r",stdin);
	freopen("eat.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n*3;i++) fa[i]=i;
    for (int i=1,x,y,z;i<=m;i++){
		scanf("%d %d %d",&z,&x,&y);
		if (x>n||y>n) {ans++;continue;}
		if (z==2&&x==y) {ans++;continue;}
		if (z==1){
			if (getfa(x+n)==getfa(y)&&x!=y) {ans++;continue;}//3
			if (getfa(x+2*n)==getfa(y)&&x!=y) {ans++;continue;}//4
			fa[getfa(x)]=getfa(y);
			fa[getfa(x+n)]=getfa(y+n);
			fa[getfa(x+2*n)]=getfa(y+2*n);
		}
		if (z==2){
			if (getfa(x)==getfa(y)) {ans++;continue;}//1
			if (getfa(x+n)==getfa(y)) {ans++;continue;}//2
			fa[getfa(x)]=getfa(y+n);
			fa[getfa(x+n)]=getfa(y+2*n);
			fa[getfa(x+2*n)]=getfa(y);
		}
	}
	printf("%d",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}

Guess you like

Origin blog.csdn.net/huang_ke_hai/article/details/88864128