[OJ]1004:Xi and Bo
Description
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.
#include <stdio.h> int uset[100]; void u_init(){ int i; for(i=0; i<100; i++){ uset[i] = -1; } } int u_find(int x){ int i = x; while(uset[i] >= 0){ i = uset[i]; } return i; } void u_cat(int r1, int r2){ if(r1 == r2){ return; } uset[r1] += uset[r2]; uset[r2] = r1; } void main(){ int t,from,to,lines, vertices; int m,v,rm,rv; scanf(" %d", &t); while(t--){ scanf(" %d %d", &from, &to); scanf(" %d", &lines); u_init(); while(lines--){ scanf(" %d", &vertices); v = -1; while(vertices--){ m = v; scanf(" %d", &v); if(m >= 0){ rm = u_find(m); rv = u_find(v); u_cat(rm, rv); } } } rm = u_find(from); rv = u_find(to); if(rm == rv){ printf("Yes\n"); }else{ printf("No\n"); } } }
标签: C
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: