日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發(fā)布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CSU - 1004】Xi and Bo(并查集,裸题) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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

解題報告:

? ?直接并查集就好了。留給學(xué)弟做練習(xí)用。

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

?

總結(jié)

以上是生活随笔為你收集整理的【CSU - 1004】Xi and Bo(并查集,裸题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。