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. JQuery中点击超链接动态修改url连接地址无效

    这篇随笔的标题真是好拗口,想表达的意思是,当点击超链接后,才去修改超链接的地址,此时超链接仍然链接的是是修改之前的页面,而不是修改之后的页面. 超链接代码如下: <a id="chao ...

  2. pytion3--用户定义的迭代器

    1.迭代器基于下面两个个方法: (1)__next__ 返回容器的下一个项目(2)__iter__ 返回迭代器本身 2.当序列遍历完时,将抛出StopIteration异常,所以通过捕获这个异常来停止 ...

  3. web文件上传大小限制

    最近在项目中遇到上传文件,对上传文件的大小需要进行限制,这里学习和整理了一下一些常规的文件大小限制的方法. 一般分为两种方式,一种是服务器端判断文件大小进行限制,这种方法的存在明显的缺陷,当用户过多后 ...

  4. 刷题总结——子串(NOIP2015提高组)

    题目: 题目背景 NOIP2015 提高组 Day2 T2 题目描述 有两个仅包含小写英文字母的字符串 A 和 B .现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在 ...

  5. scrapy介绍及源码分析

    一 简介 Scrapy基于事件驱动网络框架 Twisted 编写.因此,Scrapy基于并发性考虑由非阻塞(即异步)的实现. 官方文档 :https://docs.scrapy.org/en/late ...

  6. Jackson工具类(各种转换)

    首先要在项目中引入jackson的jar包(在此不做说明) 下面直接上代码 public class JacksonUtils { private final static ObjectMapper ...

  7. MySQL 游戏排行榜

    今天在坛子上看到了,顺便写下来. 有两种方法: 1.效率不高,因为有子查询.但是简洁.而且我对SOCRES表做了INDEX.所以性能上也差不了多少. mysql> show create tab ...

  8. Linux上安装使用SSH

    參考博客:http://blog.csdn.net/xqhrs232/article/details/50960520 Ubuntu安装使用SSH ubuntu默认并没有安装ssh服务,如果通过ssh ...

  9. SQL Server 命令行操作

    连接sqlcmd -S localhost -U SA -P '123456'; 执行脚本 sqlcmd -S localhost -U SA -P '123456' -i /root/dbo.sql ...

  10. 接口自动化测试之HTTP协议详解

    协议 简单理解,计算机与计算机之间的通讯语言就叫做协议,不同的计算机之间只有使用相同的协议才能通信.所以网络协议就是为计算机网络中进行数据交换而建立的规则,标准或约定的集合. OSI模型 1978年国 ...