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表示 ...
随机推荐
- swift 赋值判断
. var name:String? = "Wing" var geting = "Hello!" if let thisname = name{ geting ...
- ios专题 - socket(1)
二,BSD socket API 简介 BSD socket API 和 winsock API 接口大体差不多,下面将列出比较常用的 API: API接口 讲解 int socket(int add ...
- Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an enclosing instance of type caiquan (e.g. x.new A() where x is an instance of caiquan).
package test;import java.util.Scanner;import java.util.Random;public class caiquan { public static v ...
- C# 绘制窗体客户非客户区要用WM_PAINT和WM_NCPAINT
窗体分为两部分:客户区(Client area)和非客户区(Non-Client area) WM_PAINT消息.OnPaint()方法.GetDC()API函数都是处理窗体客户区绘制的 而标题 ...
- 分享一个自己写的基于TP的关系模型(2)
1.增加多对多关系的处理 /** * 定义关系 * @return array */ public function test4(){ //参数说明 //关联的模型 //主表关联字段 //关联中间表 ...
- HttpRequest 和HttpWebRequest的区别
[1]问题: asp.NET C# 中HttpRequest 和HttpWebRequest的区别 HttpRequest 与HttpWebRequest 有什么区别? 网上中文的帖子很多,但是答案 ...
- http与https差异
HTTPS和HTTP的区别: https协议需要到ca申请证书,一般免费证书很少,需要交费. http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议 http的连接很 ...
- js实现中文简繁切换效果
html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- bootstrap 下 标签页跳转总结
最近遇到一个问题,是关于bootstrap中的标签页实现上的一些功能实现,现总结一下. 问题描述:点击其他标签页后,如何在点击搜索按钮后自动跳转到第一个标签页.如下图 通过对bootstrap框架里的 ...
- iOS: 学习笔记, Swift运算符定义
Swift操作符可以自行定义, 只需要加上简单的标志符即可. @infix 中置运算. 如+,-,*,/运算 @prefix 前置运算. 如- @postfix 后置运算. a++, a-- @ass ...