HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11382 Accepted Submission(s): 5346
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.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
If it is legal, output "YES", otherwise "NO".
0 1
1 2
2 2
0 1
1 0
0 0
NO
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 105
int indgree[max_v];
vector<int> vv[max_v];
int n,m;
queue<int> q;
int tpsort()
{
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
if(indgree[i]==)
q.push(i);
int c=;
int temp;
while(!q.empty())
{
temp=q.front();
q.pop();
c++;
for(int i=;i<vv[temp].size();i++)
{
indgree[vv[temp][i]]--;
if(indgree[vv[temp][i]]==)
q.push(vv[temp][i]);
}
}
if(c!=n)//判环 拓扑完之后,如果存在点没有入队,那么这个点一定是环上的
return ;
else
return ;
}
int main()
{
/*
有向图判环 拓扑排序
无向图判环 并查集
*/
int x,y;
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)
break;
memset(indgree,,sizeof(indgree));
for(int i=;i<=n;i++)
vv[i].clear();
int flag=;
for(int i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
x++,y++;
if(x==y)
continue;
if(count(vv[x].begin(),vv[x].end(),y)==)//防重边
{
vv[x].push_back(y);
indgree[y]++;
}
if(count(vv[y].begin(),vv[y].end(),x)!=)//环的一种
{
flag=;
}
}
flag=tpsort();
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
HDU 3342 Legal or Not(有向图判环 拓扑排序)的更多相关文章
- HDU 3342 Legal or Not(判断环)
Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...
- HDU 5154 Harry and Magical Computer 有向图判环
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...
- Dwarves (有向图判环)
Dwarves 时间限制: 1 Sec 内存限制: 64 MB提交: 14 解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...
- COJ 3012 LZJ的问题 (有向图判环)
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如 ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 3342 Legal or Not (图是否有环)【拓扑排序】
<题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...
- 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 ...
随机推荐
- element-ui Collapse 折叠面板源码分析整理笔记(十)
Collapse 折叠面板源码: collapse.vue <template> <!--一组折叠面板最外层包裹div--> <div class="el-co ...
- 对HTML的理解及常用标签使用介绍--来自我的百度前端技术学院的笔记
HTML是什么,HTML5是什么? ——HTML:超文本标记语言,一种用于创建网页的标准标记语言: ——HTML5:目前最新的HTML标准,包含新的元素.属性.行为,基于它们的功能特征将他们分成不同的 ...
- 网页CSS中*{margin:0; padding:0;}有什么用
* 这东西叫“通配符”用来匹配页面上所有元素.*{margin:0; padding:0;} 像 2L 所说,body ,ul, li ,p,h1~h6,dd,dt 等……都有默认的margin 或p ...
- Loadrunner 脚本开发-利用web_custom_request函数进行接口测试
脚本开发-利用web_custom_request函数进行接口测试 by:授客 QQ:1033553122 一.POST + JSON格式参数 例: web_custom_request(" ...
- ionic左滑删除
<html ng-app="ionicApp"> <head> <meta charset="utf-8"> <met ...
- 用sparkR, 分析上亿条订单数据的脚本。
上周我们这个10人的小团队开发的推荐拉新系统, 日拉新人数已接近4万人.过去几个月这个系统从无到有, 拉新从日增几千稳步增长到日增几万, 同事们几个月来,每天工作13个小时以上,洗澡时间都没有, 有时 ...
- idea常规设置
java开发目前很多都是使用IntelliJ IDEA这款神器.废话不多说,官网地址:https://www.jetbrains.com/idea/ 我一直都是保持最新版本,安装之后注册. 首先需要安 ...
- Java中的StringTokenizer类的使用方法
StringTokenizer是字符串分隔解析类型,属于:java.util包. 1.StringTokenizer的构造函数 StringTokenizer(String str):构造一个用来解析 ...
- 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...
- Spring Cloud 子项目介绍
Spring Cloud由以下子项目组成. Spring Cloud Config 配置中心——利用git来集中管理程序的配置. 项目地址:https://spring.io/projects/spr ...