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) ...
随机推荐
- Swift 一些环境配置
#if DEBUG 使用 如下图配置即可使用
- Sublime Text使用手记
1.Package Control 输入python 命令安装,打开控制台输入下方代码运行即可.控制台打开可使用快捷键Ctrl+~ 或菜单栏中View> Show Console,可访问Pack ...
- C++模板学习随笔
本文学习内容参考:http://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html C++模板 1.模板分为函数模板和类模板两种类型 函数模板针 ...
- Android中Matrix的pre post set方法理解(转载来源:Linux社区 作者:zjmdp)
虽说以前学习过线性代数和图形学原理,但是在实际中碰到matrix还是疑惑了好一阵子,今天通过向同事请教终于找到一点门路,特总结如下: Matrix主要用于对平面进行缩放,平移,旋转以及倾斜操作,为简化 ...
- C++类继承内存布局(一)
转自:http://blog.csdn.net/jiangyi711/article/details/4890889# 一 类布局 不同的继承方式将导致不同的内存布局 1)C结构 C++基于C,所以C ...
- 在.NET 应用程序设计中如何选择Class, Abstract Class and Interface
关键字: Type– 类型 Class - 类 Abstract - 抽象的 Interface - 接口 Member - 成员 Method - 方法 Property - 属性 预备知识:在阅读 ...
- C#控件命名规范
文档名称: C#控件命名规范 撰写作者: codefly 版本编号: V1.1 C#控件命名规范 一.Data Control 类型 前缀 示例 AccessDataSource ads adsPub ...
- Invoke()方法的使用
在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法是错误的做法,Invoke 和 BeginInvoke 就是为了解决这个问题而出现的,使你在多线程中安全的更新界 ...
- Boolean 布尔类型详解
这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE.两个都不区分大小写. 要明确地将一个值转换成 boolean,用 (bool)或者 (boolean) 来强制转换.但是 ...
- ODBC方式连接Informix数据库
公司某个报表系统使用Informix数据库,在谋划使用Perl语言写数据采集程序后,花费了很多时间建立Perl访问Informix连接.恰巧Windows下ActivePerl的CPAN中又没有DBD ...