http://acm.hdu.edu.cn/showproblem.php?pid=3342

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9122    Accepted Submission(s): 4230

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
 
Author
QiuQiu@NJFU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1285 2647 3333 3339 3341 
 
 
存在环就说明关系乱了、
 
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int M(+);
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int tim,dfn[N],low[N];
int top,Stack[N],instack[N];
int sumcol,col[N],point[N];
void DFS(int now)
{
low[now]=dfn[now]=++tim;
Stack[++top]=now;instack[now]=;
for(int v,i=head[now];i;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v]) DFS(v),low[now]=min(low[now],low[v]);
else if(instack[v]) low[now]=min(low[now],dfn[v]);
}
if(low[now]==dfn[now])
{
col[now]=++sumcol;
point[sumcol]++;
for(;Stack[top]!=now;top--)
{
col[Stack[top]]=sumcol;
point[sumcol]++;
instack[Stack[top]]=;
}
top--; instack[now]=;
}
} bool jud()
{
for(int i=;i<=sumcol;i++)
if(point[i]>) return false;
return true;
} int AC()
{
for(int n,m;scanf("%d%d",&n,&m)&&n;)
{
for(int u,v;m--;)
scanf("%d%d",&u,&v),ins(u,v);
DFS();
if(jud()) puts("YES");
else puts("NO");
memset(instack,,sizeof(instack));
memset(point,,sizeof(point));
memset(Stack,,sizeof(Stack));
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(col,,sizeof(col));
sumedge=sumcol=tim=top=;
}
return ;
} int I_want_AC=AC();
int main() {;}

在不知廉耻的附上只A掉样例的Tarjan

Top_sort

 #include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int N();
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[N];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int AC()
{
for(int ans,n,m,rd[N];scanf("%d%d",&n,&m)&&n;)
{
ans=n;
queue<int>que;sumedge=;
memset(rd,,sizeof(rd));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
for(int u,v;m--;)
{
scanf("%d%d",&u,&v);
ins(u+,v+); rd[v+]++;
}
for(int i=;i<=n;i++)
if(!rd[i]) que.push(i);
for(int u,v;!que.empty();)
{
ans--;
u=que.front();que.pop();
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(--rd[v]==) que.push(v);
}
}
if(!ans) puts("YES");
else puts("NO");
}
return ;
}
int I_want_AC=AC(); int main() {;}

HDU——T 3342 Legal or Not的更多相关文章

  1. HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...

  2. 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 ...

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

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

  4. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  5. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

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

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

  7. HDU 3342 Legal or Not (最短路 拓扑排序?)

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

  8. HDU——3342 Legal or Not

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

  9. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

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

随机推荐

  1. 【原创】RPM安装软件时解决依赖性问题(自动解决依赖型)

    满足以下3个条件才能自动解决依赖性: 1.使用rpmdb -redhat(在安装时会自动弹出依赖性错误) 2.所有互相依赖的软件都必须在同一个目录下面. 3.调用-aid参数.

  2. Jlink如何校验Hex

    打开SEGGER J-Flash ARM,记住必须设置好工程属性后才能校验,否则校验和为0 设置好工程属性后,直接将Hex拖入到工程中即可.在LOG窗口中会显示CRC的值 如何校验芯片中的程序的版本号 ...

  3. SpringCloud学习笔记(7)----Spring Cloud Netflix之负载均衡-Ribbon的深入理解

    1. 注解@LoadBalanced 作用:识别应用名称,并进行负载均衡. 2. 入口类:LoadBalancerAutoConfiguration 说明:类头上的注解可以知道Ribbon 实现的负载 ...

  4. 《Unix环境高级编程》读书笔记 第11章-线程

    1. 引言 了解如何使用多个控制线程在单进程环境中执行多个任务. 不管在什么情况下,只要单个资源需要在多个用户键共享,就必须处理一致性问题. 2. 线程概念 典型的Unix进程可以看成只有一个控制线程 ...

  5. [USACO17DEC]Milk Measurement(平衡树)

    题意 最初,农夫约翰的每头奶牛每天生产G加仑的牛奶 (1≤G≤109)(1≤G≤10^9)(1≤G≤109) .由于随着时间的推移,奶牛的产奶量可能会发生变化,农夫约翰决定定期对奶牛的产奶量进行测量, ...

  6. 【Python常见问题总结】

    1. python2 中 end = '' 取消换行没有用 解决办法: 在程序开始加入 from __future__ import print_function 2. 如何在电脑上同时使用pytho ...

  7. 【BZOJ 1150】[CTSC2007]数据备份Backup

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 选择的连接肯定是相邻的点对. 那么我们处理出来长度为n-1的数组a 其中a[i-1] = dis[i]-dis[i-1] 那么问题就 ...

  8. JS在页面限制checkbox最大复选数

    应该是挺简单的代码, 记录一下分享. 首先最直接的想法就是使用循环, 用局部变量记录已选的checkbox, 达到最大值就将余下的checkbox都禁止选择, 例如以下: <!DOCTYPE h ...

  9. crazyradio焊接和下载固件过程

    非常早之前买过一套crazyradio的器件和空板.可是一直没有时间焊接出来,前天早上六点起来,安静的弄了一把,识别USB.下载crazyradio固件没问题,记录下过程: 1,首先是焊接,寻常的QF ...

  10. js 压缩图片 H5

    原理 用 canvas的 toDataURI (type , int )  如果type参数的值为image/jpeg或image/webp,则第二个参数的值如果在0.0和1.0之间的话,会被看作是图 ...