【floyd】 HRBUST 2037 Find Your Teacher

版权声明:新人还请多多指教wˋ( ° ▽、° ) https://blog.csdn.net/peng0614/article/details/80272569

Find Your Teacher

Time Limit: 1000 MS Memory Limit: 131072 K

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

“尚学堂杯”哈尔滨理工大学第六届程序设计竞赛

题意

找老师。
有n个人,编号从1~n,其中你的编号是1,老师的编号是n,现在给你一系列编号,意思是a同学可以联系上b同学,问你是否可以根据这些关系联系到你的老师。

思路

数据很水,直接floyd裸一遍过。

坑点

模板题,毫无坑点。

AC代码

#include<bits/stdc++.h>
using namespace std;

bool ans[55][55];

void solve(void)
{
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        memset(ans,false,sizeof ans);
        int n,m;
        cin>>n>>m;
        for(int i = 1 ; i <= m ; i++)
        {
            int f,t;
            cin>>f>>t;
            ans[f][t] = true;
        }
        for(int k = 1 ; k <= n ; k++)
        {
            for(int i = 1 ; i <= n ; i++)
            {
                for(int j = 1 ; j <=n ; j++)
                {
                    if(ans[i][k]==true&&ans[k][j]==true)
                        ans[i][j] = true;
                }
            }
        }
        if(ans[1][n]==true) cout<<"^_^"<<endl;
        else cout<<"T_T"<<endl;
    }
}

int main(void)
{
    solve();
    return 0;
}
阅读更多

没有更多推荐了,返回首页