Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13040   Accepted: 3383

Description

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

Input

The first line contains a single integer T, the number of test cases. And followed T cases.

The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.

Output

The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

Sample Input

1
3 3
1 2
2 3
3 1

Sample Output

Yes

Source

这题要注意,边是单向边,先用强连通分量缩点,建图,再拓扑排序若是单向链刚对,否刚为错就可以a了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1051
#define E 30000
#define M 1000000
using namespace std;
int head[N],head2[N],next2[E],vec2[E],sta[M],re,ans,next[E],id[N],in[N],vec[E],vis[N],dfn[N],low[N],clock_m,edge_m,edge_m2;
int addedge(int s,int e){
vec[edge_m]=e;next[edge_m]=head[s];head[s]=edge_m++;
}
int addedge2(int s,int e){
vec2[edge_m2]=e;next2[edge_m2]=head2[s];head2[s]=edge_m2++;
}
int init(){
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(head,-1,sizeof(head));
memset(head2,-1,sizeof(head2));
memset(id,0,sizeof(id));
memset(in,0,sizeof(in));
edge_m=0;clock_m=0;ans=0;re=0;edge_m2=0;
}
int tarjan(int x){
dfn[x]=low[x]=++clock_m;
sta[++ans]=x;
vis[x]=1;
for(int i=head[x];i!=-1;i=next[i]){
int goal=vec[i];
if(!dfn[goal]){
tarjan(goal);
low[x]=min(low[x],low[goal]);
}
else if(/*vis[goal]*/!id[goal])
low[x]=min(low[x],dfn[goal]);
}
if(low[x]==dfn[x]){
re++;int v;
do{
v=sta[ans--];
vis[v]=0;
id[v]=re;
}while(v!=x);
}
return 1;
}
int topsort(int n){
ans=0;
for(int i=1;i<=re;i++){
if(in[i]==0){
sta[ans++]=i;
}
}
if(ans>1)return 0;
while(ans>0){
ans--;
int qtop=sta[ans];
for(int j=head2[qtop];j!=-1;j=next2[j]){
in[vec2[j]]--;
if(in[vec2[j]]==0)
sta[ans++]=vec2[j];
}
if(ans>1)
return 0;
}
return 1;
}
int main()
{
int tcase,n,m,s,e;
scanf("%d",&tcase);
while(tcase--){
//system("PAUSE");
init();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&s,&e);
addedge(s,e);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
{
tarjan(i);
}
if(re==1){
printf("Yes\n");
continue;
}
for(int i=1;i<=n;i++){
for(int j=head[i];j!=-1;j=next[j]){
if(id[vec[j]]!=id[i]){
in[id[vec[j]]]++;
addedge2(id[i],id[vec[j]]);
}
}
}
if(topsort(re))printf("Yes\n");
else printf("No\n");
}
return 0;
}

poj2762 Going from u to v or from v to u?的更多相关文章

  1. POJ2762 Going from u to v or from v to u? 强连通+缩点

    题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互 ...

  2. POJ2762 Going from u to v or from v to u(单连通 缩点)

    判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...

  3. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  4. [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud     Going from u to v or from v to u? Tim ...

  5. 【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  6. Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

    v$database: 视图结构: SQL> desc v$database; Name                                      Null?    Type - ...

  7. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  8. 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage

    SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...

  9. [强连通分量] POJ 2762 Going from u to v or from v to u?

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17089 ...

随机推荐

  1. 为什么不能在scrollview中直接添加一个image,然后使animation.begin()??

    http://stackoverflow.com/questions/17267451/animation-cant-begin-in-scrollview-in-windows-phone 以上是我 ...

  2. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  3. 自动测试工具SilkTest全面介绍

    象交互,并最终记录测试结果,用户可以根据这些测试结果来判断测试成功还是失败. 4Test 脚本语言 和绝大多数自动化测试工具一样, SilkTest 可以自动捕捉,检测和重复用户交互的操作从而驱动测试 ...

  4. html5 乒乓球(碰撞检测)

    演示地址 http://koking.8u.hanmandarin.com/html5/1.html 简单介绍 小球可以在方框内部自由运动 可以通过方向键控制黑色砖块上下左右移动去与小球发生碰撞 代码 ...

  5. 用HTML5、地理定位API和Web服务来开发移动应用

    HTML 5 是一项让人振奋的技术,这有着充分的理由.这将会是一次技术突破,因为它可以将桌面应用程序功能带入浏览器中.除了传统浏览器外,对于移动浏览器,其潜力甚至更大.不仅如此,最流行的移动浏览器甚至 ...

  6. 20个热门jQuery的提示和技巧

    以下是一些非常有用的jQuery提示和所有jQuery的开发技巧. 1.优化性能复杂的选择 查询DOM中的一个子集,使用复杂的选择时,大幅提高了性能: var subset = $("&qu ...

  7. ORACLE告警日志

    告警日志介绍 告警日志文件是一类特殊的跟踪文件(trace file).告警日志文件命名一般为alert_<SID>.log,其中SID为ORACLE数据库实例名称.数据库告警日志是按时间 ...

  8. C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库

    第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...

  9. Oracle如何禁止并行

    PURPOSE -------   To explain how to disable Parallel Execution on Session/System level     SCOPE &am ...

  10. [Linked List]Swap Nodes in Pairs

    Total Accepted: 73777 Total Submissions: 219963 Difficulty: Medium Given a linked list, swap every t ...