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 < ...
随机推荐
- python模块--random
random主要用于生成随机字符串等,例如登录页面上随机字符串验证. random常用方法: import random print(random.randrange(1, 10)) # 返回1-10 ...
- Eclipse截取android报错log
Eclipse截取android报错log: 1.前提条件:已安装eclipse 2. LogCat界面设置: Logcat是Android 编程中一个命令行工具,可以用于得到程序的 log 信息,可 ...
- D. Frequent values
D. Frequent values Time Limit: 3000ms Case Time Limit: 3000ms Memory Limit: 131072KB 64-bit intege ...
- 九度oj 题目1207:质因数的个数
题目描述: 求正整数N(N>1)的质因数的个数. 相同的质因数需要重复计算.如120=2*2*2*3*5,共有5个质因数. 输入: 可能有多组测试数据,每组测试数据的输入是一个正整数N,(1&l ...
- Scala基础知识[一]
摘要:在Scala 是 Scalable Language 的简写,是一门多范式(multi-paradigm)的编程语言.设计初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行在Jav ...
- 【Luogu】P3865ST表模板(ST表)
题目链接 本来准备自己yy一个倍增来着,然而一看要求O1查询就怂了. ST表模板.放上代码. #include<cstdio> #include<cstdlib> #inclu ...
- BZOJ 2242 [SDOI2011]计算器 ——EXGCD/快速幂/BSGS
三合一的题目. exgcd不解释,快速幂不解释. BSGS采用了一种不用写EXGCD的方法,写起来感觉好了很多. 比较坑,没给BSGS的样例(LAJI) #include <map> #i ...
- Codeforces633G - Yash And Trees
Portal Description 给出一个\(n(n\leq10^5)\)个点的带点权树,以\(1\)为根:以及正整数\(m(m\leq10^3)\).进行\(q(q\leq10^5)\)次操作: ...
- Snmp的学习总结(一)
摘自:http://blog.csdn.net/shanzhizi/article/details/11606767 目录(?)[-] SNMP的5种协议数据单元 SNMP的运行过程 11 Get ...
- 服务器内部转发forward,action到action
如果request.getRequestDispatcher();中不是页面而是传action的话,参考以下内容修改: web.xml 2.4版本里,默认的filter只拦截request. 如果使用 ...