Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5885    Accepted Submission(s):
2726

Problem Description
ACM-DIY is a large QQ group where many excellent acmers
get together. It is so harmonious that just like a big family. Every day,many
"holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to
exchange their ideas. When someone has questions, many warm-hearted cows like
Lost will come to help. Then the one being helped will call Lost "master", and
Lost will have a nice "prentice". By and by, there are many pairs of "master and
prentice". But then problem occurs: there are too many masters and too many
prentices, how can we know whether it is 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
The 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.
 
Output
For each test case, print in one line the judgement of
the messy relationship.
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
 
Author
QiuQiu@NJFU
 

题意:输入数据n,m,表示有n个人接下来m行,每行输入x,y表示x是y的师父;

如果A是B的师父B是C的师父,则A是C的师父

如果A是B的师父,B又是A的师父则不合法输出No,如果合法输出YES

题解:1、如果输入的点中无不依赖定点的点(成环)输出no

2、最后结果中不依赖顶点的节点个数少于n不符合题意

  

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m;
int map[110][110];
int vis[110];
void getmap()
{
int i,j,a,b;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(!map[a][b])
{
vis[b]++;
map[a][b]=1;
}
}
}
void tuopu()
{
int i,j,sum=0;
int ok=0;
queue<int>q;
while(!q.empty())
q.pop();
for(i=0;i<n;i++)
{
if(vis[i]==0)
{
sum++;
q.push(i);
}
}
if(sum==0) ok=1;//开始图中就不存在不依赖顶点的节点(成环)
else
{
int u,ans=0;
while(!q.empty())
{
u=q.front();
ans++;
q.pop();
for(i=0;i<n;i++)
{
if(map[u][i])
{
vis[i]--;
if(vis[i]==0)
q.push(i);
}
}
}
if(ans<n)//最后排序完成后不依赖顶点的节点个数小于n
ok=1;//即存在环不符合题意
}
if(ok==0)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),n|m)
{
getmap();
tuopu();
}
return 0;
}

  

hdoj 3342 Legal or Not【拓扑排序】的更多相关文章

  1. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  2. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  3. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  4. hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  5. HDOJ 1285 确定比赛名次(拓扑排序)

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...

  6. HDOJ 2647 Reward 【逆拓扑排序+分层】

    题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. ...

  7. HDOJ 5098 Smart Software Installer 拓扑排序

    拓扑排序: 两个队列,一个放不须要重新启动入度为0的,一个放须要重新启动入度为0的....从不须要重新启动的队列開始,每弹出一个数就更新下入度,遇到入读为0的就增加到对应队列里,当队列空时,记录重新启 ...

  8. hdoj 2647 Reward【反向拓扑排序】

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06

    一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...

随机推荐

  1. 5 DML语言

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  2. 交换机access与trunk口

    理论知识: 以太网端口二种链路类型:Access 和Trunk. Access 类型的端口:只能属于1 个VLAN,一般用于连接计算机的端口: Trunk 类型的端口:可以允许多个VLAN 通过,可以 ...

  3. 输出不大于N的素数的个数

    输出不大于N的素数的个数 Sieve of Eratosthenes 方法  素数的性质: 非素数可以分解为素数乘积. 证明 (1)n = 2 成立,n = 3 成立: (2)若 n = k 时成立, ...

  4. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大? public class Gambler { public static void main(String[] args) { // Run T experiments that ...

  5. 网络基础---OSI 模型与TCP/IP

    一.网络的演进: 1.简单的联接:1960's ------------ 1970's    Host Network 六十至七十年代,网络的概念主要是主机架构的低速串行联接,提供应用程序执行.远程打 ...

  6. 接触.net5年了,感觉自己的知识面很狭隘。

    08年毕业找工作期间开始接触网页开发,由于在学校了混了4年时间,我只能从html标记语言开始学习,后来应聘到一个网站建设公司,开始学习ps.Dreamweaver和asp.由于基础薄弱,一个月后离开了 ...

  7. (转)log4j日志级别设置成DEBUG时输出Html代码等问题:

    log4j日志级别设置成DEBUG时输出Html代码等问题: 问题: log4j日志级别设置成DEBUG时会输出很多信息,包括一些Html代码 解决方案: log4j的控制是树形,所以在log4j.p ...

  8. javascript 学习笔记之JQuery中的Deferred对象

    Deffered是Jquery中的一个非常重要的对象,从1.5版本之后,Jquery中的ajax操作都基于Deffered进行了重构,这个对象的处理模式就像其他Javascript框中的Promise ...

  9. 学习笔记-记ActiveMQ学习摘录与心得(一)

    这两天在看开源的MQ技术,趁着晚上安静,把这两天学的东西摘录下.在公司学东西效率真心捉鸡,心里总觉得别扭,拿了公司的钱不干活还在那学习,表示心情不淡定,效率不行啊...晚上时间是我的,下班还是蛮开心的 ...

  10. javascript 一串DIV跟随鼠标移动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...