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

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

HDOJ Monthly Contest – 2010.03.06 
#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 (判断是否存在环)的更多相关文章

  1. HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...

  2. POJ-3259 Wormholes---SPFA判断有无负环

    题目链接: https://vjudge.net/problem/POJ-3259 题目大意: 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的 ...

  3. Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1003 题意是有m个关系格式是a b:表示想要和b必须喝a,问一个人是否喝醉就看一个人是 ...

  4. poj3259,简单判断有无负环,spfa

    英语能力差!百度的题意才读懂!就是一个判断有无负环的题.SPFA即可.,注意重边情况!! #include<iostream> //判断有无负环,spfa #include<queu ...

  5. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  6. hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  8. HDU 3342 Legal or Not (图是否有环)【拓扑排序】

    <题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...

  9. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. WebGIS前端地图显示之根据地理范围换算出瓦片行列号的原理(核心)

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或 ...

  2. 新建DataTable添加列添加行

    新建空Table添加行和列 DataTable dt = new DataTable(); //创建空DataTable 1.添加列 dt.Columns.Add("序号", ty ...

  3. C#在高分屏上让窗体程序忽略系统的显示缩放

    [STAThread] static void Main() { ) SetProcessDPIAware(); Application.EnableVisualStyles(); Applicati ...

  4. 哈密顿绕行世界问题 HDU 2181

    题意让你先输20行数表示20个城市及所相邻的三个城市(行数就是该城市),然后给你一个数,从这个(给的数就表示城市)城市出发走遍所有城市一次回到出发的城市:看着复杂,仔细想想是个不算太难的深搜题,主要你 ...

  5. confirm("确定要删除吗?") _详解

    具体代码如下: html代码: <div class="deletes">删除</div> js代码: <script type="text ...

  6. Http请求处理流程 管道流程 MVC扩展HttpModule

    HttpApplication  封装了管道处理请求的所有事件 HttpModule 对HttpApplication中事件的扩展 HttpHandler   处理程序  每个请求都要经过Handle ...

  7. x1c 6th usb-c 接口灯常亮,电源无法充电

    症状: 左侧能充电的那个usb-c 红灯始终常亮.拔下电源线也亮着. 系统里电源是始终显示正在充电,即使拔掉外接电源也显示连着外接电源正在充电! ——最吓人的是,用U盘启动进PE,想重装win10,结 ...

  8. lombok插件:Data自动get/set方法, Slf4j实现Logger的调用

    lombok插件:Data自动get/set方法, Slf4j实现Logger的调用 lombok.Data import lombok.Data; import org.hibernate.anno ...

  9. LeetCode--292--Nim游戏

    问题描述: 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函 ...

  10. sgu 203 Hyperhuffman

    题意:给出字符出现的次数,问替换成哈夫曼编码后的文本长度. 实际上观察发现就等于树的所有节点的和.用nlogn超时.用O(n),用两个队列,一个放原始数组,一个放新生成的节点. #include &l ...