【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 3Sample Output
No Yes Yes解題報(bào)告:
? ?直接并查集就好了。留給學(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(并查集,裸题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winaw32.exe - Winaw3
- 下一篇: 家里人越多中签率越高!北京超6成指标被无