Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5788 Accepted Submission(s): 2678
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<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 150
struct Edge{
int to;
int next;
}edge[N];
int head[N];
int Enct;
int in[N];
void init()
{
Enct = ;
memset(head,-,sizeof(head));
memset(in,,sizeof(in));
}
void add(int from , int to )
{
edge[Enct].to = to;
edge[Enct].next = head[from];
head[from]= Enct++;
}
int que[N];
int n;
bool ph()
{
int c = ;
for(int i = ; i < n ;i++)
{
if(in[i]==) que[c++] = i;
}
for(int i = ; i < c; i++)
{
for(int j = head[que[i]] ; j!=-; j= edge[j].next)
{
Edge e = edge[j];
in[e.to]--;
if(in[e.to]==)
que[c++] = e.to;
}
}
//printf("c = %d\n",c);
if(c<n-) return false ;
else return true;
}
int main()
{
int m ;
while(~scanf("%d%d",&n,&m)&&(n!=||m!=))
{
init();
for(int i = ;i < m ;i++)
{
int a , b;
scanf("%d%d",&a,&b);
add(a,b);
in[b]++;
}
if(ph()) printf("YES\n");
else printf("NO\n");
} return ;
}
下面是dfs超时的代码
//这种遍历所有路径的方法一般会超时,真的超时了,嘎嘎
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 150
int vis[N];
int n ;
struct Edge{
int to ;
int next;
}edge[N];
int head[N];
int Enct;
void init()
{
Enct = ;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));//标记0为未访问
}
void add(int from , int to )
{
edge[Enct].to = to;
edge[Enct].next = head[from];
head[from] = Enct++;
}
/*bool dfs(int i )
{
if(vis[i]) return false;
vis[i] = 1;
printf("vis[%d] = %d\n",i,vis[i]);
for(int j = head[i] ; j!=-1; j = edge[j].next)
{
Edge e = edge[j];
dfs(e.to);
}
return true;
}*/
bool tm = true;
bool dfs(int i )
{
vis[i]=;
for(int j = head[i] ; j!=- ;j = edge[j].next)
{
Edge e = edge[j];
if(vis[e.to]==) tm = false;
else
{
dfs(e.to);
vis[e.to]=;//保证dfs走的是一条链,每次回溯的时候相当于走反向所以标记成未访问
}
}
return tm;
}
int main()
{
int m ;
while(~scanf("%d%d",&n,&m)&&(n!=||m!=))
{
init();
tm = true;
for(int i = ;i < m ;i++)
{
int a ,b;
scanf("%d%d",&a,&b);
add(a,b);
}
bool flag = true;
for(int i= ; i < n ;i++)
{
if(flag == false) break;
if(vis[i]==)
flag = dfs(i);
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
Legal or Not(拓扑排序判环)的更多相关文章
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- LightOJ1003---Drunk(拓扑排序判环)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- HDU1811 拓扑排序判环+并查集
HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...
- [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)
题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- 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/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】
Exploration Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
随机推荐
- three.js实现3D模型展示
由于项目需要展示3d模型,所以对three做了点研究,分享出来 希望能帮到大家 先看看效果: three.js整体来说 不是很难 只要你静下心来研究研究 很快就会上手的 首先我们在页面上需要创建一个能 ...
- boost::format(字符串格式化库)
这段时间学习boost库的使用,撰文一方面留以备用,另一方面就是shared精神. format主要是用来格式化std::string字符串以及配合std::cout代替C语言printf() 使用f ...
- StreamCQL编写jstorm拓扑任务入门
一,什么是 StreamCQL StreamCQL(Stream Continuous Query Language)是一个类似SQL的声明式语言, 目的是在流计算平台(目前也就是jstrom)的基础 ...
- js知识点图解
- Python学习_08_函数式编程
在python中,函数名也是一个变量,代表对一个函数内容的引用,意味着可以作为参数传入到其他函数中,根据这个特性,发散出装饰器.闭包等概念,并涉及到变量作用域等问题. 函数 python中函数操作符为 ...
- HTML5本地存储应用sessionStorage和localStorage
在html5之前,浏览器要实现数据的存储,一般都是用cookie,但是cookie有域名和大小限定. html5流行之后,可以通过localStorage和sessionStorage实现浏览器端的数 ...
- vue2.0 样式表引入的方法 css sass less
在引入样式之前,首先要了解static.assets两个文件夹的区别. 从字面上可以看出,static用来存放静态文件,assets用来存放资源文件: static存放的文件不会被编译,打包后直接赋值 ...
- Java学习笔记23(Calendar类)
Calendar意味日历,对Date类中的很多方法做了改进 Calendar类是一个抽象类,不可以见对象,需要子类完成实现 不过这个类有特殊之处,不需要创建子类对象,而是使用它的静态方法直接获取: 示 ...
- sqoop数据迁移(基于Hadoop和关系数据库服务器之间传送数据)
1:sqoop的概述: (1):sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具.(2):导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HIV ...
- 【转】adb shell dumpsys 命令
adb shell dumpsys,默认打印出当前系统所有service信息,在后面可加上具体的服务名 需要列出当前运行的服务,可运行: adb shell dumpsys | findstr DUM ...