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. 后台返回数据判断是http还是后台本地图片 indexOf

    今天的笔记呢,记录一下 其实这个应该后台去判断的,但是因为某种原因,今天我们前台做一下判断 事情是这样的,后台返回我一个url  这个url有的http开头的 也有他后台本地的例如:/img/1.pn ...

  2. hdu 3832 Earth Hour bfs

    Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  3. JaveWeb 公司项目(5)----- Java获取当前时间的年月日以及同Thrift格式的转化

    随着项目进度的逐步完成,数据传输和界面基本上已经搭建完成,下面就是一些细节部分的修改 今天博文的主要内容说的是获取当前的时间和同Thrift类型的转化 和C#类似,java也有一个时间类Date,加载 ...

  4. chrome driver 下载

    https://npm.taobao.org/mirrors/chromedriver/ 解压后,把路径放到系统环境变量中即可 dirver = webdriver.Chrome()

  5. android状态栏和NavigationBar的动态控制显示

    项目在开发阅读器,阅读器对阅读界面的要求就是在工具栏不显示的状态下,ActionBar和NavigationBar都是不显示的,当工具栏显示时它们都出来,这就需要动态控制它们的显示与隐藏. 第一阶段: ...

  6. bat 命令 常用配置及其用法

    1.初衷: bat 批处理文件:当我懒得一个个操作的时候,可以把若干东西放到一个文件里面,开机运行或者需要的时候手动运行.节省时间. 2.命令集说明 2.1 常用命令 2.1.0 help 命令 /? ...

  7. MYSQL常用函数(时间和日期函数)

    CURDATE()或CURRENT_DATE() 返回当前的日期 CURTIME()或CURRENT_TIME() 返回当前的时间 DATE_ADD(date,INTERVAL int keyword ...

  8. Go使用protobuf

    WIN7 + Go1.9.2+protobuf3.5.1 1.首先定义一个用于测试的proto文件test.proto,内容如下: syntax = "proto3"; packa ...

  9. angular2 学习笔记 (Typescript - Attribute & reflection & decorator)

    更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...

  10. seo中的竞价排名是什么

    seo中的竞价排名是什么 一.总结 一句话总结:竞价排名的基本特点是按点击付费,推广信息出现在搜索结果中(一般是靠前的位置),如果没有被用户点击,则不收取推广费. 搜索引擎的一种推广广告的方式 1.竞 ...