Legal or Not
Legal or Not
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
Output
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 思路:拓扑排序。若能完成拓扑排序说明此图是有向无环图(DAG),最后所有点的入度都为0,说明至少存在一种拓扑排序,即是关系合理,否则说明是此图有环,明显关系不合理。
#include<stdio.h>
#include<string.h>
int degree[],vis[],map[][];
int main()
{
int n,m,a,b,i,j,temp,flag;
while(~scanf("%d%d",&n,&m) && n)
{
memset(degree,,sizeof(degree));
memset(vis,,sizeof(vis));
memset(map,,sizeof(map));
flag = ;
while(m--)
{
scanf("%d%d",&a,&b);
if(!map[a][b]) //不计重边;
{
degree[b]++;
map[a][b] = ;
}
}
for(i = ;i < n;i ++)
{
for(j = ;j < n;j ++) //查找入度为0的点;
{
if(degree[j] == && vis[j] == )
temp = j;
}
vis[temp] = ; //找到即把此点销毁;
for(j = ;j < n;j ++)
{
if(j != temp && map[temp][j] == && vis[j] == ) //处理与此点相关的点和边;
{
degree[j]--;
map[temp][j] = ; //销毁边;
}
}
}
for(i = ;i < n;i ++) //验证是否还有入度不为0的点;
{
if(degree[i])
{
flag = ;
break ;
}
}
if(!flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
Legal or Not的更多相关文章
- iOS之App Store上架被拒Legal - 5.1.5问题
今天在看到App Store 上架过程中,苹果公司反馈的拒绝原因发现了这么一个问题: Legal - 5.1.5 Your app uses background location services ...
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- 苹果开发者账号申请时报错提示错误:Legal Entity Name
he information you entered did not match your profile in the D&B database. Before submitting you ...
- 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 ...
- TOJ3650 Legal or Not
Legal or Not Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte Total Submit: 41 ...
- The only legal comparisons are between two numbers, two strings, or two dates.
The only legal comparisons are between two numbers, two strings, or two dates. Left hand operand is ...
- hdoj 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/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- MFC设置对话框透明背景
在对话框初始化函数OnInitDialog函数中添加如下代码: SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(), ...
- IOS 学习笔记 2015-03-27 我理解的OC-代理模式
案例1 KCButton.h // // KCButton.h // Protocol&Block&Category // // Created by Kenshin Cui on 1 ...
- .net日志专用
一个简单的.net写日志方法 - 可能的改进点 昨天有个朋友在评论的时候问我如下代码在web开发中有什么问题 public void Log(string msg) { System.IO.File. ...
- centOS 6.4 vsftpd 500 illegal port command
原先配置好的vsftpd突然不行了,不知为啥,感觉跟网络有关,这个网络总是有dns拦截的现象,..小公司.真烦人,用联通线路就没问题, 但同事就是连不上,我的笔记本却可以连接上..我的ubuntn,同 ...
- discuz注册 内部错误
ucenter整合后,在论坛注册时出现 内部错误,无法显示,这是因为ucenter中,某个appid没写入! 可以把ucenter安装包下的utilities/checkappid.php,复制到uc ...
- libz.so库分析
from:http://blog.chinaunix.net/uid-12773189-id-84605.html 1.查看库文件是由哪个软件包提供的空闲时打开/usr/lib目录(因为我知道这个目录 ...
- Mock相关收集
MockMVC+Mockito http://www.cnblogs.com/syxchina/p/4150879.html Spring中使用Mockito http://www.cnblogs.c ...
- 你需要了解的z-index世界
本文摘自:飘零雾雨的博客 z-index的重要性 在我看来,z-index 给了我们日常工作中以极大的帮助,我们用它来定义元素的层叠级别(stack level).受益于它,你能做Popup, Dro ...
- 005 Python的数值类型
005 Python的数值类型 BIF 指的是内置函数,一般不作为变量命名.如 input,while,if,else,float,等等.整型:整数.(python3.0版本把整型和长整型结合在 ...
- 2016-10-31 reload
经历了创业公司的欠薪后,决定重新开始,从原本着力于如何快速入门与实现变更为巩固基础再出发. 2016-10-31