热身题。
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
思路:
把输入的路线集合直接并起来,如果要求的两个站台属于这个集合,那输出yes。
直接用一个二维数组存起来输入的station,立两个flag,如果start station和end station皆与输入station有相同的,cout yes.
#include<iostream>
using namespace std;
int a[105][105];
bool flag1=0,flag2=0;
int main()
{
int t;
cin>>t;
int n,m;
while(t--)
{
flag1=0,flag2=0;
int kstart,kend;
cin>>kstart>>kend;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>m;
for(int j=0;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]==kstart)
{
flag1=1;
}
if(a[i][j]==kend) flag2=1;
}
}
if(flag1&&flag2)
{
cout<<"Yes"<<endl;
}
else cout<<"No"<<endl;
}
}
/**********************************************************************
Problem: 1004
User: 0902170512
Language: C++
Result: AC
Time:4 ms
Memory:2064 kb
**********************************************************************/