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

Problem Description
ACM-DIY is a large QQ group where many excellent acmers 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.

 
Input
The input consists of several test cases. For each 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.
 
Output
For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".
 
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
 
Sample Output
YES
NO
 题意:给出一个不一定联通的图,判断图中是否有环
题解:典型的拓扑排序判环,我又想到了暴力的dfs但是因为dfs要扫描所有的路径,所以超时了,也可以用强联通分量做
下面是拓扑排序的ac代码
 #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(拓扑排序判环)的更多相关文章

  1. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  2. LightOJ1003---Drunk(拓扑排序判环)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  3. HDU1811 拓扑排序判环+并查集

    HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...

  4. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  5. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

  6. 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

    [题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...

  7. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  8. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】

    Exploration Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. mac上虚拟机安装旧版本的macosx 10.8

    前言 由于测试的需要,需要10.8的macosx,但又不想降级自己mac版本,所以还是装虚拟机,Parallels Desktop试验了安装不了osx,就换VMware Fusion,发现是可以的. ...

  2. Java I/O---IO流的规律小结

    IO流的规律总结:解决的问题,就是开发中具体要使用哪个流对象的问题. 1,明确数据源,数据汇(数据目的) 其实就是在明确要使用的IO体系:字节流 InputStream & OutputStr ...

  3. Wincc flexable的IO域组态

    1.题目 2.新建三个变量 3.组态画面,添加IO域1 1)常规设置 2)属性设置 4.组态IO域2 1)常规项 2)属性设置 5.组态第三个IO域 1)常规设置 2)属性设置 6.此外可以设置动画 ...

  4. Xamarin android SwipeRefreshLayout入门实例

    android SwipeRefreshLayout 是实现的效果就是上滑下拉刷新ListView 获取其他控件数据.基本上每个App都有这种效果.Google提供了一个官方的刷新控件SwipeRef ...

  5. Locust no-web 模式与参数详解

    读前参考:<性能测试工具Locust > 熟悉 Apache ab 工具的同学都知道,它是没有界面的,通过命令行执行. Locust 同样也提供的命令行运行,好处就是更节省客户端资源. 命 ...

  6. 浅谈JavaScript的面向对象程序设计(一)

    面向对象的语言有一个标志,他们都有类的概念,通过类可以创建多个具有相同属性和方法的对象.但是JavaScript中没有类的概念,因此JavaScript与其他的面向对象语言还是有一定区别的.JavaS ...

  7. 深入C#.NET数据类型

    深入C#数据类型 --不同类型的参数传递使用值传递,在方法中对参数的更改在调用后不能保留.使用ref方式传递,可以保留对参数值的更改. ---值方式参数传递和引用方式传递使用值方式(不用ref修饰)传 ...

  8. java多线程(二)-Runnable和Thread

    Java在顺序性语言的基础上提供了多线程的支持.Java的线程机制是抢占式的.这表示调度机制会周期的中断线程,将上下文切换到另一个线程,从而为每个线程都提供时间片.(与抢占式多线程对应的是 协作式多线 ...

  9. JDBC详解系列(三)之建立连接(DriverManager.getConnection)

      在JDBC详解系列(一)之流程中,我将数据库的连接分解成了六个步骤. JDBC流程: 第一步:加载Driver类,注册数据库驱动: 第二步:通过DriverManager,使用url,用户名和密码 ...

  10. 第十一章:Python の 网络编程基础(三)

    本課主題 多线程的创建和使用 消息队列的介绍 Python 操作 memached 和 redis 实战 本周作业 消息队列的介绍 对列是在内存中创建的,如果整个进程里的程序运行完毕之后会被清空,消息 ...