题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2307
Find Your Teacher
Time Limit: 1000 MS Memory Limit: 131072 K
Total Submit: 33(12 users) Total Accepted: 10(8 users) Rating: Special Judge: No
Description
You just finished your exam, you felt terrible because you might fail the exam. But your teacher was so kind that he might let you pass the lesson if you called him and begged for passing. Unfortunately, you didn’t have your teacher’s phone number! You began to ask whoever probably had the number, if they didn’t have, they would ask their classmates. Would you finally get the number?
Input
The first line contains an integer T (1 <= T <= 20), indicates the number of test cases.
For each test case, the first line is two positive integers, n (n <= 50), m (m <= 2000). n is the number of people who will appear in the problem, you are indexed as 1 and your teacher is indexed as n.
Then m lines follows, each line contains two integers x, y (1 <= x, y <= n), means x have the phone number of y.
Output
For each test case, if you could finally got your teacher’s number, output “^_^”, “T_T” the otherwise.
Sample Input
2
5 5
1 2
1 3
2 3
2 4
4 5
4 3
1 2
3 4
4 1
Sample Output
^_^
T_T
Source
“尚学堂杯”哈尔滨理工大学第六届程序设计竞赛
【思路分析】本题算是一道传递闭包的裸题吧,直接Floyed搞一下就可以了。
【AC代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int a[55][55];
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
int u,v;
for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v);
a[u][v]=1;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
if(a[j][i]&&a[i][k])
{
a[j][k]=1;
}
}
}
}
if(a[1][n]==1)
{
printf("^_^\n");
}
else
{
printf("T_T\n");
}
}
return 0;
}