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 <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
vector<int>v[];
int x,y,degree[];
int vis[],n,m;
void toposort()
{
set<int>s;
priority_queue<int,vector<int>,greater<int> >q;
for(int i=;i<n;i++)
{
if(!degree[i])
{
q.push(i);
vis[i]=;
}
}
bool flag=;
while(!q.empty())
{
int u=q.top();
q.pop();
s.insert(u);
for(int i=;i<v[u].size();i++)
{
degree[v[u][i]]--;
if(!degree[v[u][i]]) q.push(v[u][i]);
}
}
puts(s.size()==n?"YES":"NO");
}
int main()
{
while(scanf("%d%d",&n,&m)&& n+m)
{
for(int i=;i<=n;i++)
v[i].clear();
memset(degree,,sizeof(degree));
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
degree[y]++;
}
toposort();
}
return ;
}

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(判断是否存在环)

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

  3. hdu 3342 Legal or Not

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Description ACM-DIY is a large QQ g ...

  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(拓扑排序判断成环)

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

  6. HDU——3342 Legal or Not

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

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

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

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

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

  9. 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 ...

随机推荐

  1. FriendlyARM交叉工具链以及编译第一个arm9应用

    不记录什么都会忘光!!!这两天又要用到开发板来做项目,可是好久没有碰了,最近一直在搞上层的东东,对rails和前端感兴趣,我这是不要毕业的节奏了吗?好吧,既然什么都忘光掉了,那就干脆来个痛快,重新装机 ...

  2. HD-ACM算法专攻系列(9)——大菲波数

    题目描述: 源码: 运用Java大数求解. import java.math.BigInteger; import java.util.*; public class Main { //主函数 pub ...

  3. javascipt入门

    一.javascript简介 javascript:(基于对象的编程语言:内部很多对象,我们只需要使用即可,几乎不需要自己创建对象) ECMAScript DOM BOM 存放位置: 建议代码放到ht ...

  4. Swift 闭包中 self? 的由来

    class UIViewSpringAnimator: SwipeAnimator { // 动画完成的闭包 var completion:((Bool) ->Void)? func addCo ...

  5. 搭建javawebxiangmu

    https://blog.csdn.net/qq_23994787/article/details/73612870#

  6. (一)React再学习

    新公司的技术栈是React,虽然之前对react大概过了一遍,但是自己没有实际落地过项目 再学习一遍react: 一.react全家桶 ·create-react-app ·组件化思维 ·JSX ·开 ...

  7. python 正则表达式与re模块

    一.正则表达式 用途 用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. #### 简单地说 就是用于字符串匹配的 字符组 在 ...

  8. 【codeforces 257D】Sum

    [题目链接]:http://codeforces.com/problemset/problem/257/D [题意] 给你n个数字; 这n个数字组成的数组满足: a[i-1]<=a[i]< ...

  9. 【codeforces 314C】Sereja and Subsequences

    [题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...

  10. Java多线程-基础知识

    一. 进程是执行中的程序,程序是静态的(我们写完以后不运行就一直放在那里),进程是执行中的程序,是动态概念的.一个进程可以有多个线程. 二. 多线程包含两个或两个以上并发运行的部分,把程序中每个这样并 ...