对没错我就是在水博

 

P3366 【模板】最小生成树

  kruskal:

 View Code

 

P3367 【模板】并查集

  

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=210000;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 int n,m;
16 int fa[mxn];
17 void init(){
18     for(int i=1;i<=n;i++){
19         fa[i]=i;
20     }
21 }
22 int find(int x){
23     if(fa[x]==x)return x;
24     return fa[x]=find(fa[x]);
25 }
26 int main(){
27     n=read();m=read();
28     init();
29     int x,y,z;
30     while(m--){
31         z=read();x=read();y=read();
32         x=find(x);
33         y=find(y);
34         if(z==1){
35             fa[x]=y;
36         }
37         else{
38             if(x==y){
39                 printf("Y\n");
40             }
41             else printf("N\n");
42         }
43     }
44     return 0;
45 }
View Code

相关文章:

  • 2021-10-30
  • 2021-12-26
  • 2022-01-30
  • 2021-10-28
  • 2021-06-28
  • 2021-04-29
  • 2022-12-23
  • 2022-02-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2022-01-24
  • 2022-02-24
  • 2021-09-13
  • 2022-12-23
相关资源
相似解决方案