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 ...
随机推荐
- Lintcode214-Max of Array-Naive
Given an array with couple of float numbers. Return the max value of them. Example Example 1: Input: ...
- Codeforces 617 E. XOR and Favorite Number
题目链接:http://codeforces.com/problemset/problem/617/E 一看这种区间查询的题目,考虑一下莫队. 如何${O(1)}$的修改和查询呢? 令${f(i,j) ...
- 函数indexOf()和lastIndexOf()
返回前面起第一个字符的位置indexOf(“字符”); 它是从前面开始数(从左边开始数),而且只找第一个,然后返回该字符的位置,索引号都是从0开始的.返回的是个数值. var txt = “abcde ...
- 添加删除tag
为某个分支打tag git tag -a v1. 9fceb02 删除tag git tag -d test_tag git push origin --delete tag test_tag 推送: ...
- Windows操作系统电脑的运行代码大全
CMD命令使用方法:开始->运行->键入cmd.或者win键+R->键入cmd gpedit.msc—–组策略 sndrec32——-录音机 Nslookup——-IP地址侦测器 e ...
- django Admin文档生成器
Django的admindocs应用可以从模型.视图.模板标签等地方获得文档内容. 一.概览 要激活admindocs,请按下面的步骤操作: 在INSTALLED_APPS内添加django.cont ...
- C.【转】C语言字符串与数字相互转换
1.gcvt 把浮点数转成字符串 - CSDN博客.html(https://blog.csdn.net/dxuehui/article/details/52791412) 1.1. 函数名: gcv ...
- [c][c++]按位操作
因为有时候需要大量的标志位来判断当前状态等.使用太多的int,bool等会使得程序不“漂亮” 这时候需要“位”操作来解决 建立一个标志位 unsigned ; 在定义一些宏,如 #define CON ...
- hive表的存储路径查找以及表的大小
1.在hive中知道一个表的存储路径可以通过hive命令 desc formatted table_name 显示表的详细信息; 2.然后找到该表的存储路径 "Location: ...
- Java == 和 equals 区别
先来看一段代码 1. String str1 = new String("hello");//堆中分配一块内存,存放"hello",str1 指向内存地址 2. ...