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 ...
随机推荐
- JS中深浅拷贝 函数封装代码
一.了解 基本数据类型保存在栈内存中,按值访问,引用数据类型保存在堆内存中,按址访问. 二.浅拷贝 浅拷贝只是复制了指向某个对象的指针,而不是复制对象本身,新旧对象其实是同一内存地址的数据,修改其中一 ...
- 常见Java问题二
1.什么是B/S架构?什么是C/S架构? B/S browser/server Web应用程序 C/S Client/Server 桌面应用程序 2.String str="www" ...
- [SDOI2010]猪国杀
题面 (这个做题面的大佬太赞啦) 无聊啊~~~然后就写大模拟,然后就从早上写到下午,生活得到了极大的充实 注意事项: 牌库为空之后再抽牌,会重复抽最后一张被抽走牌 无论在任何过程中,游戏结束(主公死或 ...
- BZOJ4827: [Hnoi2017]礼物(FFT 二次函数)
题意 题目链接 Sol 越来越菜了..裸的FFT写了1h.. 思路比较简单,直接把 \(\sum (x_i - y_i + c)^2\) 拆开 发现能提出一坨东西,然后与c有关的部分是关于C的二次函数 ...
- element-ui 中的table的列隐藏问题
element-ui 中的table和bootstrap中的table的某些设置还是有一定的差别的.之前用bootstrap做的表格,想要实现简短列和详细列的切换.因为详细列实在有太多列了,拉动滚动条 ...
- React之设置元素的滚动条
在React中,解耦了对DOM元素的操作,但有时我们确实需要对DOM操作,比如设置元素的滚动条,这时ref就满足了我们的需求 在低版本的react中,ref可以是一个string类型的属性,通过thi ...
- iOS 开发多线程 —— NSOperation
本文是根据文顶顶老师的博客学习而来,转载地址:http://www.cnblogs.com/wendingding/p/3809042.html 一.NSOperation简介 1.简单说明 NSOp ...
- 关于网站开发中div标签中设置宽度后其中文本溢出的原因和解决方法
一.问题产生的原因 当我们为div标签声明了宽度,但是仍然会出现文本越界的情况,不知道大家有没有发现,只有文本内容为单词或者纯数字的时候才会出现这种情况为此我特意测试了两种情况,结果如下: ①当文本内 ...
- (Stanford CS224d) Deep Learning and NLP课程笔记(二):word2vec
本节课将开始学习Deep NLP的基础--词向量模型. 背景 word vector是一种在计算机中表达word meaning的方式.在Webster词典中,关于meaning有三种定义: the ...
- 异常System.BadImageFormatException
[问题描述] Server Error in '/' Application. Could not load file or assembly 'WebDemo' or one of its depe ...