"Algorithmic Learning note" 44. And check the star of SJTU OJ 3015.

Source: Internet
Author: User

"Title description"
已经深夜了,露子仍然在公园里仰望星空。你走近后,她对你说:“呜—,你看夜空中的星星。它们本来都是孤独地毫无联系,但人们赋予了它们各种地联想,在它们之间连上了线,便形成了夜空中无数的星座。”你回答:“是啊。为什么我们不自己创造一个美丽的星空呢?”假设夜空中一共有n颗星星,它们初始时都没有连线。接下来,露子会给你m条指令。一共有以下三种指令:1.在某两颗星星间连线。(可能会重复连接两颗星星)2.询问你当前夜空中一共有多少个星座。3.某两颗星星当前是否属于同一个星座。其中星座是指:至少由两颗星星组成的,连通的一群星星。现在就请你实现露子的愿望吧。
"Input Format"
第一行包含两个整数n和m,分别表示星星总数和指令条数。接下来有m行,每一行的形式是以下三种之一:1.“a x y”(不包含引号),表示连接编号为x和y的星星(1<=x,y<=n, x!=y)。2.“b”(不包含引号),表示询问当前夜空中一共有多少个星座。3.“c x y”,表示询问x和y当前是否属于同一个星座(1<=x,y<=n, x!=y)。
"Output Format"
按指令给出的顺序回答,每个回答各占一行。对于每一条b类指令,输出一个整数表示当前夜空中有多少个星座。对于每一条c类指令,输出一行YES或者NO。YES代表这两颗星星是同一个星座的,NO代表他们不是同一个星座的。
"Sample Input"
4 10bc 1 2a 1 2ba 3 4bc 1 3a 2 3bc 1 3
"Sample Output"
0NO12NO1YES
"Data Size"
对于30%的数据,n<=100, m<=100。对于100%的数据,n<=100000, m<=100000。时间限制:1s。
Prompted
使用并查集。



And the collection has been familiar with the size of the data is large, there are a few more details.
1. Be sure to use scanf, the trouble is that each line will also have a newline character processing
2. Path compression is necessary to reduce the number of loops in find
3. Merge (depth) by rank the lighter set is added to the deeper set at each join//maintenance depth only increases when merging two trees of equal depth can effectively reduce
4. The online technique for calculating the number of constellations (don't go through it all the time) but decide on the current isolation of the combined two numbers

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespacestd;//The stand-alone block here is the constellation, which refers to a number greater than or equal to 2.BOOLiso[100010]={0};intprenode[100010];//record I's superiorsintrank[100010]={0};//record the depth of a constellationintn,m;intGroupCount =0;intFindintx) {//find the root node of x    intRoot =x;  while(Root! =Prenode[root]) {Root=Prenode[root]; }    //compress paths to reduce the number of queries     while(X! =root) {        intTMP =Prenode[x]; PRENODE[X]=Root; X=tmp; }    returnRoot;}//try to add a small constellation to the larger constellation .voidJoinintXinty) {    introot_x =find (x); intRoot_y =find (y); if(root_x = = root_y)//if it's the same constellation, no need to continue.        return; //0 means none of the outliers 1 means that one is an outlier, 2 means two are outliers.    intTMP = iso[x]+Iso[y]; if(tmp==0) GroupCount--; Else if(tmp==2) GroupCount++; //prenode[root_y] = root_x; //merge lighter sets into deeper collections    if(rank[root_x] > Rank[root_y]) {//the depth of the root_x is deeper, so add y to it.Prenode[root_y] = root_x;//rank is the same}Else{prenode[root_x]=root_y; if(rank[root_x] = =Rank[root_y]) Rank[root_y]++;//added a layer of    }}//inline int GetGroupCount () {//int num = 0;//for (int i = 1; I <= n; ++i)//if (!iso[i] and prenode[i]==i)//is not isolated and is the root node//num++;//return num;// }intMainintargcChar Const*argv[]) {scanf ("%d%d",&n,&m); GetChar ();//get the Terminator of each row     for(inti =1; I <= N; ++i) {Prenode[i]=i; Iso[i]=true; }    intCur =0;  while(1)    {        //cout<<cur<<endl;        if(cur==m) Break; CharFlag; scanf ("%c",&flag); if(Flag = ='\ n')            Continue; if(Flag = =EOF) Break; intb; Cur++; Switch(flag) { Case 'a': scanf ("%d%d",&a,&b);                                Join (A, b); //connections are definitely not isolated.Iso[a] =false; ISO[B]=false;  Break;  Case 'b': printf ("%d\n", GroupCount);  Break;  Case 'C': scanf ("%d%d",&a,&b); if(Find (a) = =find (b)) printf ("yes\n"); Elseprintf ("no\n");  Break; } getchar ();//get the Terminator of each row    }        return 0;}/*Number of constellations updated: mostly at the time of the join if two points are outliers then the number of constellations plus 1 if one of them is an outlier, the constellation will be mathematically unchanged if two are not outliers, the constellation will be reduced by 1.*/





Algorithm Learning note 44. And check the star of OJ 3015 SJTU

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.