Luo Gu p3367 disjoint-set templates (called the father to find)

Find the father function template

 

int find (int x) defines a function still find

{

  if(fa[x]!=x)

    fa [x] = find (fa [x]); to find his father, he is to find the root cause

  return four [x];

}

#include<bits/stdc++.h>
using namespace std;
int mo[500005];
int n,m,zi,xi,yi;
int find(int x)
{
 if(mo[x]!=x)
  mo[x]=find(mo[x]);
 return mo[x];
}
int main()
{
 cin>>n>>m;
 for(int i=1;i<=n;i++)
  mo[i]=i;
 for(int i=1;i<=m;i++)
 {
  cin>>zi>>xi>>yi;
  if(zi==1)
  {
   mo[find(yi)]=find(xi);
  }
  else
   if(find(yi)==find(xi))
    cout<<"Y\n";
   else
    cout<<"N\n";
 }
 return 0;
}

 

Guess you like

Origin www.cnblogs.com/fanwentao/p/11621033.html