[Discrete template] [disjoint-set] [Luo Gu 1955] automated analysis procedures

Begins

 

This is done long before the topic, but also my first contact with the discrete title. Down before been forgotten, wrote today make up a discrete blog.

 

topic

 

Original link: https: //www.luogu.com.cn/problem/P1955

 

(Too lazy to change the format and then copy and paste, direct it out)

 

Comment

 

Initially seen over a number of simple questions this thing ah, disjoint-set board it. First deal with the equality, equal thrown into a house set, and then determine unequal relationship, the two numbers each ranging relationship, if not set up in a house set, again down judgment on the establishment and nothing had happened. But the water had a question ha ha ha!

 

Start writing a smile on his face gradually solidified: Wait a minute size of this number is less than or equal 1e9, my father opened an array of no less than ah [Tuosai] [Tuosai]. Most will not start discrete and think of a few open int array, with each array to store the size of the number%, are stored separately. 10 1e8 open such an array, the second array 100 000 005 there fifth, tenth 10 there is a first such array. Later, a man came up with the title certainly does not want us to be like this, a great card may be a memory of what, in fact, this method of memory is not changed, it is estimated that the card will be lost, but extremely troublesome.

 

A closer look at the title n <= 1e6, each relationship described involves two numbers, then a maximum number 2,000,000 relates to, the storage space is equivalent to a majority of 1e9 is empty, simply a waste of space, optimize it? Obviously you can, in this question, we just need to distinguish between each number, without the need for specific size of each number, then we can not put some big numbers with a little bit of a small number instead? Like sign, anyway, last as long as each number has its exclusive signal, it can be distinguished with the. This is discrete .

 

So specifically how to do it? After all, before no contact discrete so at this time I should be forgiven search online meal

 

So, discrete includes a total of three. A sort. Second, the de-emphasis. Third, mapping. The following step by step explanation.

 

Sequence

 

This is a very simple step. While all the while reading the figures relate to the existence of all the array, and then sort sort on the line (PS may be the way here about a special sentence ranging relations are not directly YES then continue).

 1 cin>>n;
 2 memset(all,0,sizeof(all));
 3 int judge=0;
 4 for (int i=1;i<=n;i++){
 5     cin>>cun[i].a>>cun[i].b>>cun[i].inf;
 6     if(!cun[i].inf) judge=1;
 7         all[tot]=cun[i].a; tot++;
 8     all[tot]=cun[i].b; tot++;
 9 }
10 if(!judge){cout<<"YES"<<endl;continue;}
11 sort(all+1,all+tot);

 

And to re-map

 

This is not the reason before, this section refers to the function lower_bound no contact before the two () and unique () (belong algorithm library). unique role is to "remove" the container repeating elements (necessarily requiring an orderly array), it will repeat element to the end of the container (so the size of the array does not change), and the return value is the last address after the de-emphasis ( so we can be considered equivalent to array size becomes ), and the function lower_bound () before and after the first and last opening in the closed interval binary search, a return element is greater than or equal to the first position val. If all the elements are less than val, last position is returned (note that it is the return of position), by lower_bound () we can quickly find the location of a number in all the array, and then stored it in the index.unique(all+1,all+tot)-all-1,后面的元素就都相当于被抹掉了,那么我们就得到了一个本题涉及的所有数的列表,这样的话我们就可以把一个数的下标直接当成它的“暗号”

1 int cnt=unique(all+1,all+tot)-all-1;
2 for (int i=1;i<=n;i++){
3     cun[i].a=lower_bound(all+1,all+cnt+1,cun[i].a)-all;
4     cun[i].b=lower_bound(all+1,all+cnt+1,cun[i].b)-all;
5 }

 

At last

 

The last run disjoint-set directly on the line. This is not to say I think of it.

 

The complete code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int all[2000002],fa[20000002];
 4 struct u{
 5     int a,b,inf;
 6 }cun[20000002];
 7 bool cmp(u a,u b){
 8     return a.inf>b.inf;
 9 }
10 int find(int x){
11     if(fa[x]==x) return x;
12     return fa[x]=find(fa[x]);
13 }
14 int main(){
15     ios::sync_with_stdio(false);
16     int t;
17     cin>>t;
18     while(t--){
19         int n,tot=1;
20         cin>>n;
21         memset(all,0,sizeof(all));
22         int judge=0;
23         for (int i=1;i<=n;i++){
24             cin>>cun[i].a>>cun[i].b>>cun[i].inf;
25             if(!cun[i].inf) judge=1;
26             all[tot]=cun[i].a; tot++;
27             all[tot]=cun[i].b; tot++;
28         }
29         if(!judge){cout<<"YES"<<endl;continue;}
30         sort(all+1,all+tot);
31         int cnt=unique(all+1,all+tot)-all-1;
32         for (int i=1;i<=n;i++){
33             cun[i].a=lower_bound(all+1,all+cnt+1,cun[i].a)-all;
34             cun[i].b=lower_bound(all+1,all+cnt+1,cun[i].b)-all;
35         }
36         for (int i=1;i<=cnt+1;i++) fa[i]=i;
37         int js=0;
38         sort(cun+1,cun+1+n,cmp);
39         while(1){
40             js++;
41             if(!cun[js].inf) break;
42             int xx=find(cun[js].a),yy=find(cun[js].b);
43             if(xx!=yy) fa[xx]=yy;
44         }
45         int ans=1;
46         for (int i=js;i<=n;i++){
47             int xx=find(cun[i].a),yy=find(cun[i].b);
48             /*cout<<xx<<' '<<yy<<endl;*/
49             if(xx==yy){
50                 ans=0;
51                 break;
52             }
53         }
54         if(ans) cout<<"YES"<<endl;
55         else cout<<"NO"<<endl;
56     }
57     return 0;
58 } 
View Code

 

Epilogue

 

This is probably the best I taught for a while in the.

 

unique () and lower_bound () are thoroughly checked the usage, it can be said discrete learn clearly. Perhaps this is the so-called "beyond trainer" a necessary step in it.

 

Fortunately, even holy, songs to chant blog.

Guess you like

Origin www.cnblogs.com/DarthVictor/p/12449064.html