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) ...
随机推荐
- IOS之沙盒(Sandbox)机制
IOS中每个App应用程序都有一个单独封闭的文件夹,这个文件夹称为沙盒,并且苹果规定,任何App都无权访问其他App的沙盒 沙盒目录通过 FOUNDATION_EXPORT NSString *NSH ...
- WPF动画之线性插值动画(1)
XAML代码: <Window x:Class="线性插值动画.MainWindow" xmlns="http://schemas.microsoft.com/wi ...
- java新手笔记15 多态
1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println(" ...
- 04_过滤器Filter_05_Filter解决全站中文乱码问题(POST方式)
[工程截图] [web.xml] <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: ...
- 12_复杂查询01_Mapper代理实现
[工程截图] [代码实现] [user.java] package com.Higgin.Mybatis.po; import java.util.Date; public class User { ...
- Codeforces Round #80 Div.1 D
思路:考虑离线操作,以y为关键字排序,对于y相同的一起操作,然后考虑y的范围,当y<=sqrt(n)时,直接O(n)预处理出f[x]表示f[x]+f[x+y]+f[x+2*y]+..+f[x+k ...
- 《APUE》第三章笔记(3)
文件共享 UNIX系统支持在不同进程中共享打开的文件,首先先用一幅apue的图来介绍一下内核用于I/O文件的数据结构: 如图所见,一个进程都会有一个记录项,记录项中包含有一张打开文件描述符表,每个描述 ...
- svn-主副分支使用
主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在主切换回副分支) 奇葩的实现了需求 主改bug 副加功能, :主合并到副(在副中切换主分支),副调试成功,合并回主(在 ...
- 修改phpmyadmin上传文件大小限制
当你想将SQL语句,导入phpmyadmin 时,发现自己的sql语句文本大小 大于 phpmyadmin的课上传的文本大小. 默认phpmyadmin上传文件大小为2M,如果想要phpmyadmin ...
- MDK建立STM32F103*开发模板
一.整体流程 1.获取ST库--STM32F10x_StdPeriph_Lib_V3.5.0 2.新建文件夹并加载文件 3.新建工程 4.给工程添加组 5.设置"Target Option& ...