SSL_1896【Family】&&P1551【Relatives】&&ybtoj【Graph Theory】1 Chapter 1 Questions&&P3367【【Template】Combined check collection】

[Template] and check set

topic

Relatives (same family)
[template] combined check set (ybtoj [template] combined check set is the same)


Parsing

And check the introductory questions, pay attention to the scope, just
four times the experience

code (relatives && family):

#include<iostream>
using namespace std;
int n,m,a[50001],x,y,z;
int find(int who)
{
    
    
    if(a[who]==who)return who;
    else return a[who]=find(a[who]);
}
int main()
{
    
    
    cin>>n>>m>>z;
    for(int i=1;i<=n;i++)a[i]=i;
    for(int i=1;i<=m;i++)
    {
    
    
    	cin>>x>>y;
    	a[find(x)]=find(y);
	}
    for(int i=1;i<=z;i++)
    {
    
    
    	cin>>x>>y;
        if(find(x)==find(y))cout<<"Yes";
        else cout<<"No";
        cout<<endl;
    }
    return 0;
}

code([template] and check set):

#include<iostream>
using namespace std;
int n,m,a[10001],x,y,z;
int find(int who)
{
    
    
    if(a[who]==who)return who;
    else return a[who]=find(a[who]);
}
int main()
{
    
    
    cin>>n>>m;
    for(int i=1;i<=n;i++)a[i]=i;
    for(int i=1;i<=m;i++)
    {
    
    
        cin>>x>>y>>z;
        if(x==1)a[find(z)]=find(y);
        else
        {
    
    
            if(find(y)==find(z))cout<<'Y';
            else cout<<'N';
            cout<<endl;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/zhanglili1597895/article/details/112977791