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. HDU 5724 Chess(SG函数+状态压缩)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...

  2. 集成算法——Ensemble learning

    目的:让机器学习效果更好,单个不行,群殴啊! Bagging:训练多个分类器取平均 Boosting:从弱学习器开始加强,通过加权来进行训练 (加入一棵树,比原来要强) Stacking:聚合多个分类 ...

  3. gitlab git

    git网站是进不去的需要加权限才能进去!!!!!!!! 登录进去后 ssh-keygen -t rsa -C "gitlab用户名一般是邮箱" 一路设置好 Use the code ...

  4. ASP.NET技术总结

    ASP.NET是ASP相关技术的最新版本,它如何工作: (经典)ASP的后缀名是".asp",通常用VB编写:ASP.NET是".aspx"—— ASP.NET ...

  5. IOS学习笔记一1

    //创建.h文件 界面的类文件(创建一个类) @interface MyClass:NSObject{ //类变量声明 int a; int b; } //类属性声明 (int) p2 //类方法声明 ...

  6. 基于 Python 和 Pandas 的数据分析(7) --- Pickling

    上一节我们介绍了几种合并数据的方法. 这一节, 我们将重新开始不动产的例子. 在第四节中我们写了如下代码: import Quandl import pandas as pd fiddy_states ...

  7. js,jq获取父,兄弟,子节点整理

    js获取节点 父: parentNode 获取已知节点的父节点. 子: childNodes; 得到全部子节点 children 得到全部子节点 firstChild 获得第一个子节点 lastChi ...

  8. String和常量池

    1.Java 会确保一个字符串常量只有一个拷贝 2.用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间 ...

  9. 创建SpringBoot项目pom.xml文件第一行报错:Non-parseable POM E:\maven\repository\org\springframework\securit

    在编辑pom.xml时,第一行有个刺眼红色×,然后在Problems看到这个问题 [ERROR] The build could not read 1 project -> [Help 1]E: ...

  10. Android 虹软免费人脸识别App

    人脸识别+本机Web后端 人脸sdk采用虹软sdk,本机web采用AndServer:上传姓名+人脸图片即可实现注册源码地址:https://github.com/joetang1989/ArcFac ...