Legal or Not

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 42   Accepted Submission(s) : 32

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
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int i,n,m;
int cnt[];
vector<int> s[]; bool toposort()
{
queue<int> Q;
int num=;
for(int i=;i<n;i++) if (cnt[i]==) Q.push(i);
if(Q.empty()) return ;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
num++;
for(int i=;i<s[u].size();i++)
{
cnt[s[u][i]]--;
if (cnt[s[u][i]]<) return ;
if (cnt[s[u][i]]==) Q.push(s[u][i]);
}
}
if (num<n) return ;
return ;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if (n== && m==) break;
memset(cnt,,sizeof(cnt));
for(i=;i<n;i++) s[i].clear();
for(i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
s[x].push_back(y);
cnt[y]++;
}
if (toposort()) printf("YES\n");
else printf("NO\n");
}
return ;
}

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

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

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

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

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

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

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

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

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

  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【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

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

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

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

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

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

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

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

随机推荐

  1. Gym 100553B Burrito King 无脑背包

    题意就是你有n和m两个上限 吃一个东西会同时增加两个东西 m的值不能超过给定的m 问最后的n m值和每个东西吃了多少 贪心一下就好了 算一下性价比 从最大的开始吃 直到吃满了m n也一定是最大了 只是 ...

  2. mvc路由参数注解

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //过滤掉禁止访问的路由 routes.MapRoute( name: &quo ...

  3. android 动态string

    android开发过程之中,动态的插入string内容时候使用, 例如, <string name="time">当前时间:<xliff:g id="p ...

  4. 网页 JavaScript

    今天,我主要学习了JavaScript的部分内容. JavaScript在html中的位置主要有三个地方:head里面.body里面.</html>之后,我们一般写在</html&g ...

  5. Hadoop作业优化

    mapper数量 reduce数量 combiner 中间值压缩 自定义序列 调整shuffle,减少溢出写 关闭推测执行 任务JVM重用 慢启动reduce

  6. 【CRC校验】学习笔记

    #include<stdio.h> unsigned ]= { 0x01,0x02,0x03,0x04,0x05,0x06 }; ] = { 0x0000, 0x1021, 0x2042, ...

  7. 使用cocoapods的两个大坑的修改方法

    1.报错内容: [!] The dependency `ReactiveCocoa (= 2.1.8)` is not used in any concrete target. The depende ...

  8. javaWEB总结(7):HttpServlet和HttpServletRequest

    前言:HttpServletRequest对象封装了客户端进行HTTP协议请求时的所有信息,HttpServletRequest继承了ServletRequest,所以和ServletRequest一 ...

  9. shell中的cat和文件分界符(<<EOF)

    在shell中,文件分界符(通常写成EOF,你也可以写成FOE或者其他任何字符串)紧跟在<<符号后,意思是分界符后的内容将被当做标准输入传给<<前面的命令,直到再次在独立的一行 ...

  10. leetcode409

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...