欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

【CSU - 1004】Xi and Bo(并查集,裸题)

发布时间:2023/12/10 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【CSU - 1004】Xi and Bo(并查集,裸题) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题干:

Bo has been in Changsha for four years. However he spends most of his time staying his small dormitory. One day he decides to get out of the dormitory and see the beautiful city. So he asks to Xi to know whether he can get to another bus station from a bus station. Xi is not a good man because he doesn't tell Bo directly. He tells to Bo about some buses' routes. Now Bo turns to you and he hopes you to tell him whether he can get to another bus station from a bus station directly according to the Xi's information.

Input

The first line of the input contains a single integer T (0<T<30) which is the number of test cases. For each test case, the first contains two different numbers representing the starting station and the ending station that Bo asks. The second line is the number n (0<n<=50) of buses' routes which Xi tells. For each of the following n lines, the first number m (2<=m<= 100) which stands for the number of bus station in the bus' route. The remaining m numbers represents the m bus station. All of the bus stations are represented by a number, which is between 0 and 100.So you can think that there are only 100 bus stations in Changsha.

Output

For each test case, output the "Yes" if Bo can get to the ending station from the starting station by taking some of buses which Xi tells. Otherwise output "No". One line per each case. Quotes should not be included.

Sample Input

3 0 3 3 3 1 2 3 3 4 5 6 3 1 5 6 0 4 2 3 0 2 3 2 2 4 3 2 1 4 2 1 0 3

Sample Output

No Yes Yes

解题报告:

   直接并查集就好了。留给学弟做练习用。

AC代码:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int f[MAX],q[MAX]; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1; } void init() {for(int i = 0; i<=100; i++) {f[i] = i;} } int main() {int t,m;cin>>t;while(t--) {int st,ed;scanf("%d%d",&st,&ed);scanf("%d",&m);init();for(int num,i = 1; i<=m; i++) {scanf("%d",&num);for(int j = 1; j<=num; j++) {scanf("%d",q+j);}for(int j = 2; j<=num; j++) {merge(q[j],q[j-1]);}}if(getf(st) == getf(ed)) puts("Yes");else puts("No");}return 0 ; } //12:33-12:40

 

总结

以上是生活随笔为你收集整理的【CSU - 1004】Xi and Bo(并查集,裸题)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。