Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6580    Accepted Submission(s): 3088

Problem Description
ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?

We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.

Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.

 
Input
The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
 
Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".
 
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
 
Sample Output
YES
NO
 
Author
QiuQiu@NJFU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1285 2647 3333 3339 3341
 
看题解有的说拓扑排序? 把我吓到了,  但是还是有一种简单的解法  Floyd 跑一遍就行了
如果一条路mat[i][j] 和 mat[j][i] 都是不是INF, 就说明NO了
#include<algorithm>
#include<stdio.h>
#include<iostream> using namespace std;
#define N 112345678
#define M 111
#define INF 0x3f3f3f3f int n,m,a,b,x,y,t;
int mat[M][M]; void init()
{
for(int i = ; i < M; i++)
for(int j = ; j < M; j++)
mat[i][j] = INF; }
int main()
{
while(cin>>n>>m && n)
{
init();
bool flag = true;
while(m--)
{
scanf("%d %d", &x, &y);
mat[x][y] = ;
}
for(int k = ; k < n; k++)
for(int i = ; i < n; i++)
if(mat[i][k] != INF) // 加这个此题 会大大节约时间 ! 加了93MS 不加748MS
for(int j = ; j < n; j++)
if(mat[i][j] > mat[i][k] + mat[k][j])
mat[i][j] = mat[i][k] + mat[k][j]; for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
if(i != j)
if(mat[i][j] != INF && mat[j][i] != INF)
{
flag = false;
break;
} if(!flag)
puts("NO"); else
puts("YES"); }
return ;
}

以后Floyd 超时可以试试加上这句代码

 for(int k = ; k < n; k++)
for(int i = ; i < n; i++)
if(mat[i][k] != INF) // 加这个此题 会大大节约时间 ! 加了93MS 不加748MS
for(int j = ; j < n; j++)

HDU 3342 Legal or Not (最短路 拓扑排序?)的更多相关文章

  1. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  3. [NOIP2017]逛公园 最短路+拓扑排序+dp

    题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...

  4. [Luogu P3953] 逛公园 (最短路+拓扑排序+DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3953 Solution 这是一道神题 首先,我们不妨想一下K=0,即求最短路方案数的部分分. 我们很容易 ...

  5. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  6. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06

    一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...

  8. HDU 3342 Legal or Not (图是否有环)【拓扑排序】

    <题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...

  9. hdu 3342 Legal or Not (拓扑排序)

    重边这样的东西   仅仅能呵呵 就是裸裸的拓扑排序 假设恩可以排出来就YES . else  NO 仅仅须要所有搜一遍就好了 #include <cstdio> #include < ...

随机推荐

  1. bash中的算术运算

    bash中的算术运算     +, -, *, /, %     实现算术运算:         (1) let var=算术表达式          (2) var=$[算术表达式]         ...

  2. google F12

    谷歌浏览器(Google Chrome)开发调试详细介绍 博客分类: 前端 浏览器chromegoogle调试开发  很多Web前台开发者都喜欢这种浏览器自带的开发者工具,这对前台设计.代码调试很大帮 ...

  3. python中判断字符串是否为中文

    判断字符串是否在中文编码范围内 for c in s:        if not ('\u4e00' <= c <= '\u9fa5'):            return False ...

  4. [Istio]Web应用出现upstream connect error or disconnect/reset before headers

    在部署web应用之后,使用ingressway为入口,不能正常访问.服务返回 upstream connect error or disconnect/reset before headers 这种情 ...

  5. mysqlbinlog备份和mysqldump备份

    -bash : mysqldump: command not found -bash : mysqlbinlog:command not found 首先得知道mysql命令或mysqldump命令的 ...

  6. Access denied for user ''@'localhost' to database 'mysql'

    ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'   在centos下安装好了mysql,用r ...

  7. redis2.3.7安装时出现undefined reference to `clock_gettime'

    (转自:http://blog.csdn.net/qq_28779503/article/details/54844988) undefined reference to `clock_gettime ...

  8. 计算几何 I. 极角

    参考资料 hankcs.com: POJ 1981 Circle and Points 题解 aswmtjdsj: POJ 1981 Circle and Points [定长圆覆盖最多点问题] zx ...

  9. 刷题总结——随机图(ssoi)

    题目: 随机图 (random.cpp/c/pas) [问题描述] BG 为了造数据,随机生成了一张�个点的无向图.他把顶点标号为1~�. 根据BG 的随机算法,对于一个点对�, �(1 ≤ � &l ...

  10. 【2018.4.5】Shoi2017题集

    这三道题分别对应bzoj4868~4870,pdf没法往这放,因此放弃了. T1: 方法1(正解):三分法 考虑暴力枚举最晚公布的时间x,关注到2操作是没有负面影响的1操作,所以如果A大于B,那么只需 ...