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

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

POJ Monthly--2006.02.26,zgl & twb





题意:jiajia有一个洞穴,洞穴中有n个房间,房间的连接是有方向的,jiajia想知道u与v之间是不是有路径u->v或v->u





思路:单连通问题,首先将图中的强连通的部分进行缩点,构成一颗树,接下来拓扑排序,判断拓扑排序的两点之间是不是有边,如果没有边则图不符合要求。



#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <algorithm> using namespace std; const int Max = 1100; const int INF = 0x3f3f3f3f; typedef struct node
{
int v; int next; }Line ; Line Li[Max*6]; int Head[Max],top; int Map[Max][Max]; int Du[Max],pre[Max]; int vis1[Max],dfn[Max],low[Max]; bool vis2[Max]; int dep; int Point[Max*5][2],Num; int topo[Max],num; int a[Max],ToNum; stack < int >S; void AddEdge(int u,int v)
{
Li[top].v = v; Li[top].next = Head[u]; Head[u] = top++;
} int Find(int x)
{
return pre[x]==-1?x:pre[x] = Find(pre[x]);
} void Tarjan(int u) //强连通缩点
{ dfn[u] = low[u] = dep++; vis1[u] = 1; S.push(u); for(int i=Head[u];i!=-1;i=Li[i].next)
{
if(vis1[Li[i].v]==1)
{
low[u]=min(low[u],dfn[Li[i].v]);
}
if(vis1[Li[i].v]==0)
{
Tarjan(Li[i].v); low[u]=min(low[u],low[Li[i].v]);
} if(vis2[Li[i].v])
{
Point[Num][0]=u; Point[Num++][1]=Li[i].v;
}
} if(low[u]==dfn[u]) //如果low[u]==dfn[u],则说明是强连通的根节点。
{
vis2[u]=true; topo[num++] = u; while(1)
{
if(S.empty())
{
break;
}
int v = S.top(); S.pop(); vis1[v]=2; if(v==u)
{
break;
}
pre[v]=u; }
}
} void Toposort()//BFS拓扑排序
{ queue<int>Q;
for(int i=0;i<num;i++)
{
if(Du[topo[i]]==0)
{
Q.push(topo[i]); }
}
while(!Q.empty())
{
int u=Q.front(); a[ToNum++]=u; Q.pop(); for(int i=0;i<num;i++)
{
if(Map[u][topo[i]])
{
Du[topo[i]]--; if(Du[topo[i]]==0)
{
Q.push(topo[i]);
}
}
}
}
} int main()
{ int T; int n,m; scanf("%d",&T); while(T--)
{
scanf("%d %d",&n,&m); top = 0; memset(Head,-1,sizeof(Head)); int u,v; for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v); AddEdge(u,v);
} memset(vis1,0,sizeof(vis1)); memset(Map,0,sizeof(Map)); memset(vis2,false,sizeof(vis2)); memset(Du,0,sizeof(Du)); memset(pre,-1,sizeof(pre)); dep = 0; Num =0 ;num = 0; while(!S.empty())
{
S.pop();
} for(int i=1;i<=n;i++)
{
if(vis1[i]==0)
{
Tarjan(i);
}
}
for(int i=0;i<Num;i++)
{ int x = Find(Point[i][0]); int y = Find(Point[i][1]); Map[x][y]=1; Du[y]++;
} ToNum = 0; Toposort(); bool flag=false; for(int i=0;i<ToNum-1;i++)
{
if(!Map[a[i]][a[i+1]])//判断相邻的是不是存在边
{
flag=true; break;
}
} if(flag)
{
printf("No\n");
}
else
{
printf("Yes\n");
} } return 0;
}

Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序的更多相关文章

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

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

  2. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  3. 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: 15812 ...

  4. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  5. Java实现判断单联通(强连通缩点+拓扑排序)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. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

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

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

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

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

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

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

随机推荐

  1. 所有设备的CSS像素

    mydevice.io Mobile devices, in Responsive Web Design, relate to a core value which is the value of C ...

  2. 对Oracle数据库坏块的理解

    1.物理坏块和逻辑坏块 在数据库中有一个概念叫做数据块的一致性,Oracle的数据块的一致性包括了两个层次:物理一致性和逻辑一致性,如果一个数据块在这两个层次上存在不一致性,那就对应到了我们今天要要说 ...

  3. Matlab里面的SVM

    支持向量机是建立在统计学习理论基础之上的新一代机器学习算法,支持向量机的优势主要体现在解决线性不可分问题,它通过引入核函数,巧妙地解决了在高维空间中的内积运算,从而很好地解决了非线性分类问题. 构造出 ...

  4. IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)

    //加上头部 和底部 - (void)viewDidLoad { [super viewDidLoad]; [self tableView]; // 设置行高 self.tableView.rowHe ...

  5. poj1061-青蛙的约会(扩展欧几里德算法)

    一,题意: 两个青蛙在赤道上跳跃,走环路.起始位置分别为x,y. 每次跳跃距离分别为m,n.赤道长度为L.两青蛙跳跃方向与次数相同的情况下, 问两青蛙是否有方法跳跃到同一点.输出最少跳跃次数.二,思路 ...

  6. PG, Pool之间的一些数量关系

    先说一下我的环境: Ceph cluster中包含6台OSD节点 (osd.0 - 5), 一共有10个Pool (0 - 9), 这些Pool共享了144个PG (这个数字是所有Pool的PG_SI ...

  7. profile

    项目开发中,使用git最为版本控制工具,往往会建立开发分支.测试分支和生产分支. 各个分支的数据库url,所依赖的接口url可能不同,直接配置的话,在合并分支时往往出现冲突.使用profile可以有效 ...

  8. OAuth2.0 微博登陆网站功能的实现(一)获取用户授权及令牌 Access Token

    在登陆一些网站的时候,可以选择登陆方式为第三方登陆,例如微博登陆,以爱奇艺为例,进入首页,点击 ”登陆“,会弹出登录框: 除了本站登陆外,还可以选择其他第三方登陆,比如微博登陆.QQ 登陆.微信登陆等 ...

  9. js弹窗

    常用人JS弹窗,lhgDialog 4.20

  10. redis 自启动

    第一步: 在/etc/init.d/目录下建立一个名字为 redis 的启动脚本 cd /etc/init.d touch redis 然后在这个脚本中添加如下脚本  <注意修改自己的PIDFI ...