HDU——3342 Legal or Not
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9125 Accepted Submission(s): 4231
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.
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".
0 1
1 2
2 2
0 1
1 0
0 0
NO
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 1010
using namespace std;
queue<int>q;
int n,m,x,y,in[N],tot,sum,head[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int from,to,next;
}edge[N];
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int main()
{
)
{
n=read(),m=read();
&&m==) break;
sum=,tot=;
memset(,sizeof(in));
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
;i<=m;i++)
{
x=read(),y=read();
add(x+,y+),]++;
}
;i<=n;i++)
) q.push(i);
while(!q.empty())
{
x=q.front(),q.pop();sum++;
for(int i=head[x];i;i=edge[i].next)
{
int t=edge[i].to;
in[t]--;
) q.push(t);
}
}
if(sum!=n) printf("NO\n");
else printf("YES\n");
}
;
}
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(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
- 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)Tota ...
- 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(判断环)
Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...
随机推荐
- AJPFX总结FileWriter类的write方法
FileWriter从类OutputStreamWriter继承的 1.public void write(int c) throws IOException写入单个字符. ...
- Thinkphp删除缓存
控制器代码 public function delcache(){ //当找到有Runtime的文件夹时,进入if if(is_dir(RUNTIME_PATH)){ delDir(RUNTIME ...
- [转]在ubuntu上安装chrome浏览器
原文链接: https://www.linuxidc.com/Linux/2013-10/91857.htm --------------------------------------------- ...
- git创建分支及日常使用
克隆代码 git clone https://github.com/master-dev.git 查看所有分支 git branch --all # 默认只有master分支,所以会看到如下两个分支 ...
- C# 移动开发 MasterDetailPage 侧滑
先上结果图: 虽然是跨平台的安卓和ios都可以运行,由于目前只配置了安卓的,ios的先不理. 我们先新建一个项目,跨平台应用: 可移植类库: 可移植项目右键添加新建项 选 Forms MasterDe ...
- vba,excel,身份证,照片
Sub 插入图片() '调整单元格大小,以适应图片大小 功能 插入身份证照片打印 - 正面在单元格d6 反面单元格d10 ActiveSheet.Pictures.Delete '清理过期 ...
- 如何选安卓android|linux系统开发板,简化学习难度,缩短开发进程
平台一:iTOP-4412精英版 系统支持:Android 4.0.3系统 / Android 4.4系统 / Linux + Qt系统 / Ubuntu12.04系统 开发板特点:Cortex-A ...
- hibernate fetch属性
fetch的属性值有:select(默认值).join.subselect 1)当fetch=”select”时,程序会先查询返回要查询的主体对象,然后根据lazy属性看是否懒加载. 2)当fetch ...
- 浅谈JS之text/javascript和application/javascript
问题描述: JS在IE8以下浏览器运行异常 代码: <script>标签是这样子写的: <script type="application/javascript" ...
- ios远程推送和python版push server相关笔记
今天研究了下ios的远程推送,网上的相关教程很多,做了一遍下来记录一下遇到的问题和注意事项(转载请注明) 1.证书及乱七八糟的配置 公钥:app id管理那儿的“Development Push SS ...