Hrbust 2307 Find your teacher(Floyed算法 | 传递闭包)
来源:互联网 发布:轮胎动平衡三个数据 编辑:程序博客网 时间:2024/06/08 19:16
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
题目大意:
总共有n个人,其中有m条关系,以下n行中 x拥有y的电话,通过关系传递,1会不会有n的电话。
题解
通过Floyed算法思想,我这里用j作为for最外层作为两个关系联系起来的 “桥”,
所谓的“桥”代表关系过度的那个中间人,必然说a有b的电话,b有c的电话,那就说明b就是“桥”,通过将桥放到最外层,可以将所有之间存在间接关系都连接起来,最后判断是否1能直接到达n即可
参考代码
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int n, m;int book[1005][1005];int main(){ int t; cin >> t; while (t--) { memset(book, 0, sizeof(book)); cin >> n >> m; for (int i = 0;i < m;i++) { int a, b; cin >> a >> b; book[a][b] = 1; } for (int j = 1;j <= n;j++)//过桥 { for (int i = 1;i <= n;i++) { for (int k = 1;k <= n;k++) { /*如果a与b有关系,且a与c有关系,则a与c有关系*/ if (book[i][j] && book[j][k]) book[i][k] = 1; } } } if (book[1][n]) cout << "^_^" << endl; else cout << "T_T" << endl; }}
- Hrbust 2307 Find your teacher(Floyed算法 | 传递闭包)
- 哈理工OJ 2307 Find Your Teacher(Floyed 求传递闭包)
- POJ 3660 Cow Contest(传递闭包floyed算法)
- poj 3660 Cow Contest(传递闭包floyed算法)
- POJ 3660 Cow Contest(floyed算法求传递闭包)
- POJ3660---Cow Contest(floyed传递闭包问题)
- Floyed求传递闭包 poj 3660
- 哈理工OJ 1352 中间的球 (floyed求传递闭包)
- HDU 6152 Friend-Graph(2017CCPC网络选拔赛)【Floyed求传递闭包】
- 2017 乌鲁木齐赛区网络赛 Banana(【Floyed求传递闭包】)
- hrbust 1748 Sort the Array(传递闭包)
- hrbust 1748Sort the Array【传递闭包+Floyd】
- BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)
- Arbitrage (floyed算法)
- UVA247[Calling Circles] Floyed求传递闭包 || tarjan求SCC
- Floyed算法
- floyed 算法
- FLOYED算法
- Windows常用技巧集锦
- Search in Rotated Sorted Array II
- 51nod1265 四点共面
- 安卓开发:去掉自带的软件名栏
- UTL
- Hrbust 2307 Find your teacher(Floyed算法 | 传递闭包)
- 表达式
- 在eclipse的java EE模式中导入别人的项目出现Multiple annotations found at this line
- 几种实现悬浮下拉菜单的方式
- Java三层架构
- Linux命令(9)——tcpdump命令
- error C2668: “pow”: 对重载函数的调用不明确
- 标准对象
- Javascript中prototype属性详解