P2024 [NOI2001] food chain [disjoint-set extension field] P1525 held criminals [disjoint-set extension field]

Topic Source: Luo Gu

Title 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, 1 - id N. Each animal is A, B, C in kind, but we do not know

Which in the end is which.

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

The first argument is "1 XY", 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 which some true sentence

, Some false. When one of the following three words, this sentence is a lie, the truth is otherwise.

• The current case with some true words of earlier conflicts, is a lie

• if the current X or Y greater than N, that is a lie

• Current eat as saying X X, is a lie

Your task is given according to the total number of N and K words, the output of lies.

Input and output formats

Input formats:

 

The input data from eat.in

The first line of two integers, N, K, N expressed animal, K words.

Each word line the second line (in accordance with the requirements of the subject, see examples)

 

Output formats:

 

Output in the eat.out

Line, an integer representing the total number of lies.

 

Sample input and output

Input Sample # 1: 
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Output Sample # 1: 
3

Explanation

1 ≤ N ≤ 5 ∗ 10^4

1 ≤ K ≤ 10^5

 

Resolution:

Good question ah good topic ah (Pa Pa Pa)! I did the day finally came out.

Beginning with the right side and want to check Ji Jie found that this could not think of konjac, or use the extended domain.

 

This problem can be seen as an extension of P1525 imprisoned criminals expand the topic, specifically the idea is the same.


If you have questions about an extension field, you can read my previous one solution to a problem  P1525 imprisoned criminals [extended domain disjoint-set] , which mentioned the domain extension.

Ideas:

 Extension field disjoint-set maintains three domains: x_self similar, x_enemy predators, x_eat prey.

The relationship between them:

Suppose x, y two animals,

 

If the topic given x and y are the same, then merge x_self, y_self and x_enemy, y_enemy and x_eat and y_eat.

Provided that: x_self and y_eat not in the same collection, x_eat and y_self not in the same collection.

 

If the topic given x y eat, where we note that, because the relationship between the food chain is a ring given topic, so we can by the "x eat y" inferred "x is y predators prey" .

Whereby combined x_eat, y_self and x_self, y_enemy and x_enemy, y_eat.

Provided that: x_self and y_self not in a collection, x_self and y_eat not in a collection.

 

Reference Code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<cstring>
 5 #include<iostream>
 6 #define N 50010
 7 using namespace std;
 8 struct node{
 9     int x,y,flag;
10 }g[N<<4];
11 int n,k,fa[N<<4];
12 int get(int x)
13 {
14     if(fa[x]==x) return x;
15     return fa[x]=get(fa[x]);
16 }
17 void merge(int x,int y)
18 {
19     x=get(x),y=get(y);
20     fa[x]=y;
21 }
22 int main()
23 {
24     //freopen("fuc.in","r",stdin);
25     //freopen("fuc.out","w",stdout);
26     int cnt=0;
27     scanf("%d%d",&n,&k);
28     for(int i=1;i<=n*3;i++) fa[i]=i;
29     for(int i=1;i<=k;i++)
30         scanf("%d%d%d",&g[i].flag,&g[i].x,&g[i].y);
31     for(int i=1;i<=k;i++){
32         if(g[i].x>n||g[i].y>n){
33             cnt++;continue;
34         }
35         int x_self=g[i].x,x_enemy=g[i].x+n,x_eat=g[i].x+n+n;
36         int y_self=g[i].y,y_enemy=g[i].y+n,y_eat=g[i].y+n+n;
37         if(g[i].flag==1){
38             if(get(x_eat)==get(y_self)||get(x_self)==get(y_eat)){
39                 cnt++;
40             }
41             else{
42                 merge(x_self,y_self);
43                 merge(x_eat,y_eat);
44                 merge(x_enemy,y_enemy);
45             }
46         }
47         else{
48             if(get(x_self)==get(y_self)||get(y_eat)==get(x_self)){
49                 cnt++;
50             }
51             else{
52                 merge(x_self,y_enemy);
53                 merge(x_eat,y_self);
54                 merge(x_enemy,y_eat);
55             }
56         }
57     }
58     cout<<cnt<<endl;
59     return 0;
60 }

 

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/10979331.html