hdoj 3342 Legal or Not【拓扑排序】
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
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.
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.
the messy relationship.
If it is legal, output "YES", otherwise "NO".
题意:输入数据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【拓扑排序】的更多相关文章
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- 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) ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDOJ 1285 确定比赛名次(拓扑排序)
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- HDOJ 2647 Reward 【逆拓扑排序+分层】
题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. ...
- HDOJ 5098 Smart Software Installer 拓扑排序
拓扑排序: 两个队列,一个放不须要重新启动入度为0的,一个放须要重新启动入度为0的....从不须要重新启动的队列開始,每弹出一个数就更新下入度,遇到入读为0的就增加到对应队列里,当队列空时,记录重新启 ...
- hdoj 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...
随机推荐
- 24种设计模式--抽象工厂模式【Abstract Factory Pattern】
女娲造人,人是造出来了,世界是热闹了,可是低头一看,都是清一色的类型,缺少关爱.仇恨.喜怒哀乐等情绪,人类的生命太平淡了,女娲一想,猛然一拍脑袋,忘记给人类定义性别了,那怎么办?抹掉重来,然后就把人类 ...
- sublime 正则搜索日语字符
sublime 正则搜索日语字符 [\x{3041}-\x{3096}\x{30A0}-\x{30FF}\x{3400}-\x{4DB5}\x{4E00}-\x{9FCB}\x{F900}-\x{FA ...
- js 中的流程控制—while和do while
while语句: while(exp){ }如果为true ,执行代码块里的语句,如果为false,跳出循环 <script> var i =1 ; while (i<10){ / ...
- Shortcut Collapse project or projects in the Solution Explorer Microsoft Visual Studio 2008
The standard windows keyboard shortcuts for expanding and collapsing treeviews are: Numeric Keypad * ...
- WPF之 XAML集合项简单演示
我们直接通过xaml文件演示一个简单的xaml集合项: <Window x:Class="WPF_XAML集合项.MainWindow" xmlns="http:/ ...
- 一个c++给程序打log的单例模式类
开发过程中需要给程序打log. 所以照着网上写了个单例模式的log类 #ifndef MISCLOGWRITER_H_ #define MISCLOGWRITER_H_ #include <io ...
- Quartz1.8.5例子(二)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- 转载:用Dreamweave cs 5.5+PhoneGap+Jquery Mobile搭建移动开发
转载地址:http://blog.csdn.net/haha_mingg/article/details/7900221 移动设备应用开发有多难,只要学会HTML5+Javascript就可以.用Dr ...
- FLASH 存储学习-串行SPI NOR FLASH
1.1 SST25VF080B简介1.1.1 主要特性 关键点:容量.速度(时钟速度.读写速度).功耗. l 容量:8MBit: l 最高SPI时钟频率:50MHz: l 低功耗模式下电流消耗:5uA ...
- USB枚举的详细流程
附一个很好的枚举过程的详细流程: ◆ 用户将一个USB设备插入USB端口,主机为端口供电,设备此时处于上电状态.◆ 主机检测设备.◆ 集线器使用中断通道将事件报告给主机.◆ 主机发送Get_Port_ ...