CSU 1004

1004: Xi and Bo

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 273  Solved: 93
[Submit][Status][Web Board]

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.

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

HINT

Source

中南大学第五届大学生程序设计竞赛-热身赛

一开始想到了并查集

后来发现不是两个两个的输入

又想到DFS

其实可以把他变成两个两个的就好

 1 #include <iostream>
 2
 3 using namespace std;
 4 int fa[101];
 5 void init()
 6 {
 7     for(int i =0;i<101;i++)
 8     {
 9         fa[i]=i;
10     }
11 }
12
13 int find(int x)
14 {
15     while(x!=fa[x])
16         x = fa[x];
17     return x;
18 }
19
20 void merge(int x,int y)
21 {
22     int a = find(x);
23     int b = find(y);
24     if(a!=b)
25     {
26         fa[a]=b;
27     }
28 }
29 int main()
30 {
31     int T,start,end,n,m;
32     cin.sync_with_stdio(false);
33     cin>>T;
34     while(T--)
35     {
36         init();
37         cin>>start>>end>>n;
38         for(int i=0;i<n;i++)
39         {
40             cin>>m;
41             int x,y;
42             cin>>x;
43             for(int i=1;i<m;i++)
44             {
45                 cin>>y;
46                 merge(x,y);
47             }
48         }
49         if(find(start)!=find(end))
50             cout<<"No"<<endl;
51         else
52             cout<<"Yes"<<endl;
53
54     }
55      return 0;
56 }

实现代码

CSU 1004

时间: 2024-09-29 23:24:14

CSU 1004的相关文章

CSU 1004并查集

试题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1004 题目分析: 讲述的主要是是否可以通过公交直接到达自己的目的地,如果最后将问题转换为并查集后就可以发现其实就是问在自己构造的这棵树中,他们的根节点是否是相同的. 主要的注意是在输入的时候要求两个两个的处理,简单并且节省空间. #include <iostream> #include <cstdio> using namespace std; int fa[101];

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>

CSU 1111: 三家人【有趣的思维题】

1111: 三家人 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 2241  Solved: 874 [Submit][Status][Web Board] Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请问这笔钱如何分给A.B 二位太太较为恰当?A 应得多少元?90/(5+4)*5=$50

CSU 1112: 机器人的指令【模拟题】

1112: 机器人的指令 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1858  Solved: 682 [Submit][Status][Web Board] Description 数轴原点有一个机器人.该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置. ·LEFT:往左移动一个单位 ·RIGHT: 往右移动一个单位 ·SAME AS i: 和第i 条执行相同的动作.输入保证i 是一个正整数,且不超过之前执行指令数 In

HDU 4864(多校)1004 Task

Problem Description Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task's level yi cannot complete this task. If the company comp

CSU 1416 Practical Number

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1416 结论题,具体判断方法请点击这个网址. 筛素数是肯定的,但一开始定的范围太大了,想当然要筛到10^9的质数,但仔细想想,只要到sqrt(10^9)就可以了,最后的那一个质数是最后一步的比较,不用筛出来. #include <stdio.h> #include <string.h> #include <iostream> using namespace st

CSU 1412 Line and Circles

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1412 题目要求判断是否有一条直线可以穿过所有的圆. 做法:把所有圆心做一次凸包,然后判断这个凸包是否能通过一个宽度为2*R的通道. 做法和求凸包直径差不多,只是判断的时候把点到两个端点的距离换成点到直线的距离. #include <stdio.h> #include <string.h> #include <math.h> #include <stdli

HDU 1004 Let the Balloon Rise【STL&lt;map&gt;】

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123800    Accepted Submission(s): 48826 Problem Description Contest time again! How excited it is to see balloons floating ar

51Nod - 1004 n^n的末位数字

51Nod - 1004 n^n的末位数字 给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字. Input 一个数N(1 <= N <= 10^9) Output 输出N^N的末位数字 Input示例 13 Output示例 3 题解: 末尾数字,所以在快速迭代幂的时候,只需要考虑末尾数字即可. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstri