Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 10898    Accepted Submission(s): 5111

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

题意

给出n个人(编号0~n-1)和m对关系关系,判断有没有出现环

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e3+10;
using namespace std;
int n,m;
int a[maxn][maxn];
int vis[maxn];
void toposort()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(!vis[j])
{
vis[j]--;
for(int k=0;k<n;k++)
{
if(a[j][k])
{
a[j][k]--;
vis[k]--;
}
}
break;
}
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>n>>m&&n&&m)
{
ms(a);
ms(vis);
int x,y;
for(int i=0;i<m;i++)
{
cin>>x>>y;
if(!a[x][y])
{
a[x][y]=1;
vis[y]++;
}
}
int flag=0;
toposort();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
flag+=a[i][j];
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}

HDU 3342:Legal or Not(拓扑排序)的更多相关文章

  1. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  2. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  3. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  4. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

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

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

  6. HDU 3342 Legal or Not (最短路 拓扑排序?)

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

  7. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

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

  8. hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06

    一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...

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

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

  10. hdu 3342 Legal or Not (拓扑排序)

    重边这样的东西   仅仅能呵呵 就是裸裸的拓扑排序 假设恩可以排出来就YES . else  NO 仅仅须要所有搜一遍就好了 #include <cstdio> #include < ...

随机推荐

  1. Oracle学习笔记(一)——并发与锁

    1 并发 多用户数据库管理系统的一个主要任务是对 并发(concurrency)进行控制,即对多个用户同时访问同一数据进行控制.当缺乏有效的并发控制时,修改数据的操作就不能保证正常,从而危害数据完整性 ...

  2. Codeforces 821C - Okabe and Boxes

    821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...

  3. Java选择结构和数组

    Java选择结构和数组 一.Switch语句 二.if和switch区别 推荐使用if 三.函数 Java中的函数和方法是同一个词 四.数组 4.1.数组常见错误 五.内存机制 六.转换成十六进制 移 ...

  4. linux编译安装mysql5.1.x

    安装mysql,安装前准备 如果mysql用户不存在,那么添加mysql用户 groupadd mysql useradd -g mysql mysql mysql编译安装 make时间特别长 wge ...

  5. 『Scrapy』爬虫框架入门

    框架结构 引擎:处于中央位置协调工作的模块 spiders:生成需求url直接处理响应的单元 调度器:生成url队列(包括去重等) 下载器:直接和互联网打交道的单元 管道:持久化存储的单元 框架安装 ...

  6. python-day37--协程

    一. 协程介绍 单线程下实现并发,提升运行效率, 1.自己控制切换,保存状态 2.遇到I/O切         (单纯的CPU切没意义,只有在遇到I/O的时候切才有效率) 一句话说明什么是线程:协程是 ...

  7. 检测Linux glibc幽灵漏洞和修补漏洞

    1.首先安装rpm : sudo apt-get install rpm   wget -OGHOST-test.sh http://www.antian365.com/lab/linux0day/G ...

  8. linux单用户模式

    linux单用户模式 2014年11月11日 17:18 在grub上相应要启动的内核上按“e”. 进入下一界面,继续按“e”. 在进入文本界面后,输入“single”回车. 进入grub界面后,按“ ...

  9. 小议常被忽略的a标签:visited属性的特殊用法

    CSS1/CSS2对于a定义了4个伪类, :link  a标签未访问时的样式 :active  a标签mousedown时的样式 :hover  a标签mouseover时的样式 :visited  ...

  10. DB2 的事务日志

    1.     DB2事务日志:DB2的日志分主日志和次日志,主日志是在数据库第一次被连接和激活时创建的,而次日志是当写满所有的主日志后,才动态分配次日志,主日志和次日志受设置个数的制约,当配置的所有主 ...