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. bzoj2464: 中山市选[2009]小明的游戏(最短路)

    2464: 中山市选[2009]小明的游戏 题目:传送门 题解: 最短路的裸题... 代码: #include<cstdio> #include<cstring> #inclu ...

  2. ionic2集成极光推送

    ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...

  3. (二)Ribbon(负载均衡的客户端)+Rest

    前面讲了服务的注册与发现,微服务项目会把项目的各个业务需求划分成几个模块来单独提供服务,各服务间的调用都是采用Http Restful来实现,但是在SpringClound中服务间的调用有两种方式:一 ...

  4. hihocoder 1407 重复旋律2

    思路: 二分一哈答案 height分个块 //By SiriusRen #include <cstdio> #include <cstring> #include <al ...

  5. 接口、索引器、Foreach的本质(学习笔记)

    接口 什么是接口? 接口代表一种能力,和抽象类类似但比抽象类的抽象程度更高! 接口的定义: public interface IEat//定义一个接口 { void Eat(string food); ...

  6. 准备把平台挪到linux

    在上午准备周末胡老师的课程考核的Ppt时,逐渐我觉得不得不把平台挪到linux了.很多并行的应用不只是在linux上效率更高,而且很多包都在linux上.另外如果不及早挪到Linux上,后面遇到的问题 ...

  7. Glide错误java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity

    解决办法 在使用Glide的那段代码加是否在主线程判断 if(Util.isOnMainThread()) { Glide.with(ClassifyItemDetailActivity.this). ...

  8. ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  9. Matplotlib 画廊

    https://matplotlib.org/gallery.html

  10. Python读取Matlab的.mat文件

    参考网站: https://blog.csdn.net/rumswell/article/details/8545087 数据: R 22*22 double 部分截图如下: 使用sicpy.io即可 ...