Luo Valley P2024 [NOI2001] food chain (type disjoint-set weighting disjoint-set) groups

Portal


Problem-solving ideas

Weighting disjoint-set:

What is the weighted disjoint-set?

It is a record of each node to its parent's information (weight, etc.).

Difficulties: When compression path node and the present node to the combined weights of the father's conversion to the root weights

How it transformed?

Each question is not the same QAQ

We look at this question r [x] = 0 is represented by x and F [x] is the same species, eating equal to 1 x F [x], x is equal to 2 represents F [x] food.

From point x to F [x] is a method of updating weights to the point i of the weight of the ancestors:

Since the compression path is recursive, so in fact return f [x] = find (f [x]) when, f [f [x]] is ancestors.

So this is actually a map:

This is then placed on the problem, not difficult to find r [x] = (r [x] + r [f [x]])% 3.

Bold conjecture without proof! !

Then is the merger:

Figure it the first place! F1 is the ancestor of A, F2 are ancestors of B.

The A and B are merged (ancestors of ancestors A father as B) is essentially required r [f1].

And x is the know - when A and B is the same, x is 0, when A eat B, x is 1.

So obviously, r [f1] = (r [b] + xr [a] +3)% 3. (Because there may be negative, then it is +3% 3)

Bold conjecture without proof! !


Kind of disjoint-set:

The kind of disjoint-set do not understand can look down this road a relatively simple problem - gang .

Know the kind of the disjoint-set, look at this question: three with the same disjoint-set are maintained separately, food, predators (three times the size of the opening f the array --1 ~ n, n + 1 ~ 2 * n, n * 2 + 1 ~ 3 * n).

Each time data for -

  • When 1: Determine if a natural enemy b or b is a natural enemy to ans ++, or merger (a, b is the same kind of similar, a food is food b, a natural enemy is the natural enemy of b)
  • When 2:00: determine if a and b are the same or a natural enemy b on ans ++, or merger (a food is b, a b is similar natural enemies, a natural enemy of food b)

The answer to the final output.

// write relatively simple, think simple , no difficult challenge --by ckw

AC Code

Weighting disjoint-set:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn=50005;
 5 int n,k,f[maxn],r[maxn],ans;
 6 int find(int x){
 7     if(f[x]==x) return x;
 8     int fa=find(f[x]);
 9     r[x]=(r[x]+r[f[x]])%3;
10     f[x]=fa;
11     return fa;
12 }
13 int main()
14 {
15     cin>>n>>k;
16     for(int i=1;i<=n;i++){
17         f[i]=i;
18     }
19     while(k--){
20         int a,b,c;
21         scanf("%d%d%d",&c,&a,&b);
22         if((c==2&&a==b)||a>n||b>n){
23             ans++;
24             continue;
25         }
26         int fx=find(a);
27         int fy=find(b);
28         if(c==1){
29             if(fx==fy&&r[a]!=r[b]){
30                 ans++;
31                 continue;
32             }
33             if(fx!=fy){
34                 f[fx]=fy;
35                 r[fx]=(3+r[b]-r[a])%3;
36             }
37             continue;
38         }
39         if(c==2){
40             if(fx==fy&&(r[a]+3-r[b])%3!=1){
41                 ans++;
42                 continue;
43             }
44             if(fx!=fy){
45                 f[fx]=fy;
46                 r[fx]=(3+r[b]-r[a]+1)%3;
47             }
48         }
49     }
50     cout<<ans;
51     return 0;
52 }
Weighting disjoint-set

Kind of disjoint-set (press row Dafa is good!):

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn=50005;
 5 int n,k,f[maxn*3],ans;
 6 int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
 7 int main()
 8 {
 9     cin>>n>>k;
10     for(int i=1;i<=3*n;i++){
11         f[i]=i;
12     }
13     while(k--){
14         int a,b,c;
15         scanf("%d%d%d",&c,&a,&b);
16         if(a>n||b>n){ans++;continue;}
17         if(c==1) (find(a+n)==find(b)||find(b+n)==find(a))?(ans++):(f[find(a)]=find(b),f[find(a+n)]=find(b+n),f[find(a+n*2)]=find(b+n*2));
18         else (a==b||find(a)==find(b)||find(a)==find(b+n))?(ans++):(f[find(a)]=find(b+2*n),f[find(a+n)]=find(b),f[find(a+2*n)]=find(b+not));
19      }
 20      cout << years;
21      return  0 ;
22 }
Kind of disjoint-set

//NOI2001 Day1 t1

Guess you like

Origin www.cnblogs.com/yinyuqin/p/11774582.html