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. ecshop中的$user对象

    ecshop的程序中,有个对象:$user,它是用来处理用户信息的.比如登录.注册,还有就是用来和第三方管理通讯和共享资源的.在user.php中,有一条$user->login($userna ...

  2. 某次送温暖考试的 c题

    题目大意: 给定n个点的无根树,树上每个点都有一个非负的点权. 树上的路径的价值定义为树上路径的点权和-树上路径的点权最大值; 现在给定一个参数P询问有多少条路径的价值是P的倍数(注意单点也算路径,路 ...

  3. C#常用单词

    C#语言需要的一些英语注释 About -----关于 abstract -----抽象的 Abstract ------抽象的 Accept -----接受 activat -----活跃 add  ...

  4. ES6 Proxy和Reflect (上)

    Proxy概述 Proxy用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种"元编程"(meta programming),即对编程语言进行编程. Proxy可以理 ...

  5. CSS3 使用选择器在页面插入内容

    使用选择器来插入文字 h2:before{ content:'COLUMN'; color:white: background-color:orange: padding:1px 5px; } 注意点 ...

  6. alias 命令详解

    alias 命令 作用:  设置命令别名,可以将较长的命令进行简化,使用alias 时,用户必须使用单引号将原来的命令引起来,防止特殊字符导致错误. 如要永久生效则将alias 命令存放到bash 的 ...

  7. c#加密解密源码,md5、des、rsa

    从网上找来的代码,顺手改改,用起来更方便. 配置文件 using System; using System.Collections.Generic; using System.Text; using ...

  8. SQL语句 我喜欢上海

    select * from [user] wherer name like '上海%'

  9. 如何将外部的obj模型导入OpenGL

    1.关于obj的说明. obj中存放的是顶点坐标信息(v),面的信息(f),法线(vn),纹理坐标(vt),以及材质(这个放在mtl)中 我使用CINEMA 4D导出用VS查看后的信息: CINEMA ...

  10. C#语言和SQL Server第十章笔记

    第十章 :使用关键字模糊查询 笔记 一:使用关键字 :LIKE  BETWEEN  IN进行模糊查询 通配符:  一类字符,代替一个或多个真正的字符 与LIKE关键字一起使用 通配符: 解释 实例 符 ...