【floyd】 HRBUST 2037 Find Your Teacher

本文介绍了一道关于寻找老师联系方式的算法题目,采用Floyd算法解决人际网络中的可达性问题,判断学生是否能通过同学间的联系找到老师的电话号码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两米长弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值