Legal or Not (判断是否存在环)
Legal or Not
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 61 Accepted Submission(s) : 44
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
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
Source
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int indegree[];
int map[][];
int n,m;
void topu()
{
int p=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(indegree[j]==)
{ p++;
indegree[j]--;
for(int k=;k<n;k++)
{
if(map[j][k]==)
{
map[j][k]=;
indegree[k]--;
}
}
break; }
} } if(p==n)
printf("YES\n");
else
printf("NO\n");
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
memset(map,,sizeof map);
memset(indegree,,sizeof indegree);
if(n==&&m==)
break;
for(int i=;i<m;i++)
{
int q,p;
scanf("%d%d",&q,&p);
if(map[q][p]==)
{
map[q][p]=;
indegree[p]++;
}
}
topu();
} return ;
}
Legal or Not (判断是否存在环)的更多相关文章
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- POJ-3259 Wormholes---SPFA判断有无负环
题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...
- Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...
- poj3259,简单判断有无负环,spfa
英语能力差!百度的题意才读懂!就是一个判断有无负环的题.SPFA即可.,注意重边情况!! #include<iostream> //判断有无负环,spfa #include<queu ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- HDU 3342 Legal or Not (图是否有环)【拓扑排序】
<题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- Mac python 2.X 升级到 3.X
Mac OS X10.9默认带了Python2.7,不过现在Python3.3.3出来了,如果想使用最新版本,赶紧升级下吧.基本步骤如下. 第1步:下载Python3.3 下载地址如下: Python ...
- JQGrid 在页面加载时展开SubGrid
1.jqgrid在加载完成之后展开SubGrid,用得如下办法: gridComplete: function () { var rowIds = $("#list2").getD ...
- [从零开始搭网站四]CentOS配置Tomcat
点击下面连接查看从零开始搭网站全系列 从零开始搭网站 上一章带大家配置了JDK,那么现在就要来配置Tomcat容器了. 1:去 http://tomcat.apache.org/download-90 ...
- git创建、删除分支
//创建分支 git branch branchname //创建并切换到新分支 git checkout -b branchname //远程分支 git push origin branchnam ...
- JavaSE习题 第四章 类与对象
问答题: 1.在声明类时,类名应该遵守哪些习惯? 1.与文件名相同2.首字母大写 2.类体内容中有那两类比较重要的成员? 1.成员变量2.方法 3.实例方法可以操作类变量吗?类方法可以操作实例变量吗? ...
- StringBuilderWriter 这个类需要commons.io.2.6这个包才可以使用, 在maven仓库中搜
- Java String常用的两个方法
- Codeforces 934D - A Determined Cleanup
934D - A Determined Cleanup 思路: 找规律,和k进制的求法差不多,答案的奇数位是p%k,偶数位如果p%k!=0,那么答案是k-p%k,否则为0. 代码: #include& ...
- [MySQL]典型的行列转换
列变成行 测试数据库数据样式: 应用的sql语句: SELECT TM,NAME,SUM(GE) AS 'GE',SUM(GD) AS 'GD',SUM(CT) AS 'CT',SUM(NUM) AS ...
- vue2总结
1.ref可跨页面取.vue文件的所有内容: <!--echart图自定义公用模板--> <echar-tem ref="echar"> </echa ...