HDU 3342 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 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.
If it is legal, output "YES", otherwise "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(判断环)的更多相关文章
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- 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 ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3342 Legal or Not(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
- HDU——3342 Legal or Not
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdu 3342 Legal or Not(拓扑排序)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 3342 Legal or Not (最短路 拓扑排序?)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- 安卓WebView的使用,在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页
WebView 在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页. 1.定义一个WebView位置 <?xml version="1.0" encoding=" ...
- apiCloud中openFrameGroup传参
apiCloud中openFrameGroup传参 1.无效的 api.openFrameGroup({ // 打开 frame 组 name: 'group', scrollEnabled: fal ...
- 使用LSTM做电影评论负面检测——使用朴素贝叶斯才51%,但是使用LSTM可以达到99%准确度
基本思路: 每个评论取前200个单词.然后生成词汇表,利用词汇index标注评论(对 每条评论的前200个单词编号而已),然后使用LSTM做正负评论检测. 代码解读见[[[评论]]]!embeddin ...
- div在父集高度未知的情况下垂直居中的方法
父集高度未知,子集高度已知: 可以使用弹性盒来解决: justify-content属性定义了项目在主轴上的对齐方式. align-items属性定义项目在交叉轴上如何对齐.
- 前后端分离跨域 关于前后端分离开发环境下的跨域访问问题(angular proxy=>nginx )
前后端分离后遇到了跨域访问的问题: angular1中使用proxy很麻烦,最后还是失败结束:最后总结3种方法如下: 本人使用的第一种方法,只是开发环境下使用很方便! 1:禁掉谷歌的安全策略(Turn ...
- HD-ACM算法专攻系列(5)——N!
题目描述: 源码: #include"iostream" using namespace std; int main() { int n, digit, carry, tmp; i ...
- Git 环境安装
本文环境: 操作系统:Windows XP SP3 Git客户端:TortoiseGit-1.8.14.0-32bit 一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 msysgit: ...
- windows下mysql解压版安装及centos下mysql root密码忘记
windows安装 1. 下载zip版的解压后将bin添加到path. 2. 修改解压目录D:\mysql\mysql-5.7.12-winx64下的my.ini,设置路径: 还要添加 [client ...
- .NET Framework 3.5 无法安装以下功能 安装错误:0x800F0906(客户端加域后出现)
问题:安装错误:0x800F0906 系统安装并加域后,在安装用友软件时提示没有.net 3.5 系统为win10 但是,点击确定后,却出现了这样的错误.如下: 点击下载并安装此功能,出现了这样 ...
- [POJ2823][洛谷P1886]滑动窗口 Sliding Window
题目大意:有一列数,和一个窗口,一次能框连续的s个数,初始时窗口在左端,不断往右移动,移到最右端为止,求每次被框住的s个数中的最小数和最大数. 解题思路:这道题是一道区间查询问题,可以用线段树做.每个 ...