[NOI2001 food chain] (a NOI information through 1390)

Description [title]

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.

Some people describe this relationship N animal food chain formed by two different ways:

The first argument is "1 X Y", represents the X and Y are similar.

The second argument is "2 X Y", 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 and previous conflicts, then some really, that is a lie;

2) if the current X or Y greater than N, is lie;

3) currently eat as saying X X, is a lie.

Your task is the total number (1≤ N ≤50,000) and K words (0≤K≤100,000), output lies in the given N.

[Enter]

The first line of two 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]

Only one integer representing the number of lies.

[Sample input]

100 7
1 101 1 
2 1 2
2 2 3 
2 3 3 
1 1 3 
2 3 1 
1 5 5

[Sample Output]

3


Read a lot of problem solutions and concluded that this approach is best understood by the following:

Unlike most methods, we disjoint-set open space three times as large. But the key is used twice after what representation. Here I introduce two concepts, predators and prey! Let x then y y eating predators is x, x prey is y. For the above three times the space, which we denote respectively: 1-n: represents who and who is the same. n + 1-2 * n: represents the set of natural enemies. 2 * n + 1-3 * n: represents the set of prey.

So this is our go back and look at both instructions:

1. Enter the x and y, x and y represent the same kind: for x and y, if they already have a relationship with food being eaten, then to lie. So we check the x and y + n, y + 2 * n is in the same set (y whether prey or predators of x) if so, ans ++, if not the merger. NATURAL must merge the three sets were combined, i.e. combined: - x and y - x + n and y + n - x + 2 * n and y + 2 * n

2. Enter the x and y, x represents eat y: At this point if it is a lie, then there are two cases: - 1.x and y is the same class - 2.y eat x 1, we only need to check the situation with respect to x y to whether in a collection. In the case of 2, we need to check and x + n y are in the same set (x y is not prey). If the two conditions are not met, then it would even edge up. Need to connect three sides: - 1.y + n and x (y is the prey x) - 2.x + 2 * n and y (y predators is x) - 3. This is the most ingenious point (!): - x + n with y + 2 * n (in the x and the y prey to predators) - ease of understanding herein, may be introduced into an animal t - t prey animal is x, the annular structure, it must be a natural enemies y right - and so x + n y + 2 * n the essentially belongs to a class, to merge! - This is the soul of this question, we must understand, do not know how to take paper painting it!

To understand the relationship between these (I admit that was a bit around), this problem is not difficult to implement. Specific implementation code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int fa[20000001],ans;
 4 int read()
 5 {
 6     int f=1,x=0;char s=getchar();
 7     while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
 8     while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
 9     x*=f;
10     return x;
11 }
12 void write(int x)
13 {
14     if(x<0)
15     {
16         putchar('-');
17         x=-x;
18     }
19     if(x>9)write(x/10);
20     putchar(x%10+'0');
21 }
22 int find(int x)
23 {
24     if(fa[x]!=x)fa[x]=find(fa[x]);
25     return fa[x];
26 }
27 int main()
28 {
29     int n,m,w,x,y,k,d;
30     n=read();k=read();
31     for(int i=1;i<=3*n;i++)
32         fa[i]=i;
33     for(int i=1;i<=k;i++)
34     {
35         d=read();x=read();y=read();
36         if(x==y&&d==2)
37         {
38             ans++;continue;
39         }
40         if(x>n||y>n)
41         {
42             ans++;continue;
43         }
44         if(d==1)
45         {
46             if(find(x)==find(y+n)||find(x)==find(y+2*n))ans++;
47             else
48             {
49                 fa[find(y)]=find(x);
50                 fa[find(y+n)]=find(x+n);
51                 fa[find(y+2*n)]=find(x+2*n);
52             }
53         }
54         if(d==2)
55         {
56             if(find(x)==find(y)||find(x+n)==find(y)||find(y+2*n)==find(x))ans++;
57             else
58             {
59                 fa[find(y+n)]=find(x);
60                 fa[find(y)]=find(x+2*n);
61                 fa[find(y+2*n)]=find(x+n);
62             }
63         }
64     }
65     write(ans);
66     return 0;
67 }

// Reference: Luo Gu · P2024 food chain (multi-criteria disjoint-set) - Guess_Ha's blog - CSDN blog
https://blog.csdn.net/qq_39553725/article/details/76474124

Guess you like

Origin www.cnblogs.com/ljy-endl/p/11277606.html