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. 将文件放到Android模拟器的SD卡中的两种解决方法

    两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...

  2. position:absolute实现垂直居中

    一些图标通常要垂直居中 如下所示: 而css中没有直接的样式.需要我们自己调试. 我用了position:absolute;来实现. 要想使得position:absolute;有效,它的父元素必须也 ...

  3. 动态可视化库Vis.js:社交关系谱

    Form Here:http://code.csdn.net/news/2819345 Vis.js 是一个动态的.基于浏览器的可视化库,可处理大量的动态数据并能与这些数据进行交互操作.该项目是由Al ...

  4. 【POJ 2823 Sliding Window】 单调队列

    题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...

  5. 集合框架-TreeSet

    TreeSet是Set集合的常见子类. TreeSet:底层结构是 二叉树 元素是有排序的,但是不可以有重复元素. 相关代码演练: /* TreeSet ;元素是有序的,但是不可以元素重复. */ i ...

  6. Import MySQL Dumpfile, SQL Datafile Into My Database

    How can I import a MySQL dumpfile into my database? I'm using CentOS Linux 5 server. My old hosting ...

  7. APP安全测评checklist

    leader不要打我啊,我要借用一下我组app的安全测评检查方案,这些最基本的安全防范措施应该是每个app都要注意的吧: 对了,首先,你的app得先混淆啊~:AndroidStudio 混淆打包 先来 ...

  8. SQL Server 创建作业系列问题

    一.从IClassFactory为CLSID为{AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2}的COM组件创建实例失败. 尊重原著作:本文转载自http://www.hao ...

  9. WindowsForm界面 运行顺序 Form属性

    WindowsForm界面 运行顺序 Form属性 什么是类:1 类是面向对象编程的基本单元.类包含了两个成员 字段(Field)和方法方法(Method) 2 字段即变量 方法即函数什么是字段:字段 ...

  10. AOP面试遇到的问题

    1.什么是AOP? 面向切面的编程,找出纸和笔,画一个箭头,两道竖线将这个箭头砍断,这就是AOP 举例来说,某个方法正在运行呢,要想在前面加个日志,加在这里,后面加个日志,加在这里,前面加transa ...