哈理工OJ 2307 Find Your Teacher(Floyed 求传递闭包)

题目链接: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值