HDU 3342 Legal or Not (最短路 拓扑排序?)
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
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.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
If it is legal, output "YES", otherwise "NO".
0 1
1 2
2 2
0 1
1 0
0 0
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 (最短路 拓扑排序?)的更多相关文章
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- [NOIP2017]逛公园 最短路+拓扑排序+dp
题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...
- [Luogu P3953] 逛公园 (最短路+拓扑排序+DP)
题面 传送门:https://www.luogu.org/problemnew/show/P3953 Solution 这是一道神题 首先,我们不妨想一下K=0,即求最短路方案数的部分分. 我们很容易 ...
- HDU 3342 Legal or Not(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...
- HDU 3342 Legal or Not (图是否有环)【拓扑排序】
<题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...
- hdu 3342 Legal or Not (拓扑排序)
重边这样的东西 仅仅能呵呵 就是裸裸的拓扑排序 假设恩可以排出来就YES . else NO 仅仅须要所有搜一遍就好了 #include <cstdio> #include < ...
随机推荐
- bash中的算术运算
bash中的算术运算 +, -, *, /, % 实现算术运算: (1) let var=算术表达式 (2) var=$[算术表达式] ...
- google F12
谷歌浏览器(Google Chrome)开发调试详细介绍 博客分类: 前端 浏览器chromegoogle调试开发 很多Web前台开发者都喜欢这种浏览器自带的开发者工具,这对前台设计.代码调试很大帮 ...
- python中判断字符串是否为中文
判断字符串是否在中文编码范围内 for c in s: if not ('\u4e00' <= c <= '\u9fa5'): return False ...
- [Istio]Web应用出现upstream connect error or disconnect/reset before headers
在部署web应用之后,使用ingressway为入口,不能正常访问.服务返回 upstream connect error or disconnect/reset before headers 这种情 ...
- mysqlbinlog备份和mysqldump备份
-bash : mysqldump: command not found -bash : mysqlbinlog:command not found 首先得知道mysql命令或mysqldump命令的 ...
- Access denied for user ''@'localhost' to database 'mysql'
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql' 在centos下安装好了mysql,用r ...
- redis2.3.7安装时出现undefined reference to `clock_gettime'
(转自:http://blog.csdn.net/qq_28779503/article/details/54844988) undefined reference to `clock_gettime ...
- 计算几何 I. 极角
参考资料 hankcs.com: POJ 1981 Circle and Points 题解 aswmtjdsj: POJ 1981 Circle and Points [定长圆覆盖最多点问题] zx ...
- 刷题总结——随机图(ssoi)
题目: 随机图 (random.cpp/c/pas) [问题描述] BG 为了造数据,随机生成了一张�个点的无向图.他把顶点标号为1~�. 根据BG 的随机算法,对于一个点对�, �(1 ≤ � &l ...
- 【2018.4.5】Shoi2017题集
这三道题分别对应bzoj4868~4870,pdf没法往这放,因此放弃了. T1: 方法1(正解):三分法 考虑暴力枚举最晚公布的时间x,关注到2操作是没有负面影响的1操作,所以如果A大于B,那么只需 ...