B - 电影节

2022/5/5 23:18:52

本文主要是介绍B - 电影节,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目链接 https://acm.sdut.edu.cn/onlinejudge3/contests/3989/problems/B

并查集模板

唯一值得做的地方是循环输入(一开始真的忘记了)


 

放AC代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m,a,b,c,d;
 4 int p[100010];
 5 
 6 void init(int n)
 7 {
 8     for(int o=1; o<=n; o++)
 9         p[o]=o;
10 }
11 
12 int getp(int o)
13 {
14     return p[o]==o?o:(p[o]=getp(p[o]));
15 }
16 
17 void Union(int x,int y)
18 {
19     int x_root=getp(x);
20     int y_root=getp(y);
21     if(x_root!=y_root)
22         p[y_root]=x_root;
23     return;
24 }
25 
26 int main()
27 {
28     while(cin>>n>>m)
29     {
30         init(n);
31         for(int i=1; i<=m; i++)
32         {
33             cin>>a>>b;
34             Union(a,b);
35         }
36         cin>>c>>d;
37         if(getp(c)==getp(d)) cout<<"same"<<endl;
38         else cout<<"not sure"<<endl;
39     }
40     return 0;
41 }

 



这篇关于B - 电影节的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程