这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径。

方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变成DAG。

现在问题就变成,如何判断DAG是否是单连通图——用拓扑排序——如果拓扑排序过程中出现1个以上入度为0的点那就不是单连通图,因为有2个入度0的点,那这两个点肯定都无法到达对方。

另外,注意题目没说给的图是连通的!。。

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 1111
#define MAXM 6666
struct Edge{
int u,v,next;
}edge[MAXM];
int NE,head[MAXN];
void addEdge(int u,int v){
edge[NE].u=u; edge[NE].v=v; edge[NE].next=head[u];
head[u]=NE++;
} int belong[MAXN],bn,stack[MAXN],top;
bool instack[MAXN];
int dn,dfn[MAXN],low[MAXN];
void dfs(int u){
dfn[u]=low[u]=++dn;
stack[++top]=u; instack[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(dfn[v]==){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
int v; ++bn;
do{
v=stack[top--];
belong[v]=bn;
instack[v]=;
}while(u!=v);
}
} int deg[MAXN];
bool toposort(){
queue<int> que;
for(int i=; i<=bn; ++i){
if(deg[i]==) que.push(i);
}
if(que.size()>) return ;
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(--deg[v]==) que.push(v);
}
if(que.size()>) return ;
}
return ;
}
int main(){
int t,n,m,a,b;
scanf("%d",&t);
while(t--){
NE=;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&a,&b);
addEdge(a,b);
} top=bn=dn=;
memset(instack,,sizeof(instack));
memset(dfn,,sizeof(dfn));
for(int i=; i<=n; ++i){
if(dfn[i]==) dfs(i);
} int tmp=NE; NE=;
memset(head,-,sizeof(head));
memset(deg,,sizeof(deg));
for(int i=; i<tmp; ++i){
int u=belong[edge[i].u],v=belong[edge[i].v];
if(u==v) continue;
addEdge(u,v);
++deg[v];
}
if(toposort()) puts("Yes");
else puts("No");
}
return ;
}

POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)的更多相关文章

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

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

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

  4. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

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

  6. POJ2762 单向连通图(缩点+拓扑排序

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

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

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

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

  9. poj2762 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: 13040 ...

随机推荐

  1. js打印(控件)及多种方式

    非常好用的LODOP打印控件 Lodop打印控件简单使用方法 1.安装. 2.调用LodopFuncs.js文件. 3.增加OBJECT对象 <script language="jav ...

  2. NGUI 粒子显示在上级

    http://bbs.taikr.com/thread-2272-1-1.html [NGUI]3.0+版本,粒子在UI后面显示 -- : 48人阅读 评论() 收藏 举报 [Unity3D][NGU ...

  3. 学号160809212姓名田京诚C语言程序设计实验2选择结构程序设计

    编写一个C程序,输入3个数,并按由大到小的顺序输出. 1 #include <stdio.h> void main(){ int a,b,c,t; printf("请输入三个整数 ...

  4. CSS 确定选中变红色

    textarea:focus { border: 1px solid #f4645f; outline: none; } blockquote { border-left: 4px solid #f4 ...

  5. 【云计算】开源装机自动化系统 CloudBoot OSInstall 介绍

    "CloudBoot"(OSinstall) 发布了. 产品更新及特点如下: 新增虚拟化操作系统适配:支持主流操作系统:RedHat.CentOS.SUSE.Ubuntu.Wind ...

  6. 【持续集成】[Jenkins]Job中如何传递自定义变量

    [Jenkins]Job中如何传递自定义变量 来自dweiwei   2015-06-27 18:37:19|  分类: 自动化测试 |举报 |字号大中小 订阅 用微信  “扫一扫” 将文章分享到朋友 ...

  7. 【UGUI】Canvas和Rect Transform

    Canvas 1.所有的UI元件都需要放在Canvas里 2.UI元件的绘制顺序,与在 Hierarchy的顺序相同,在上面的元素会先被绘制,位于后续绘制元素的下面 3.可以选择3种不同的渲染模式: ...

  8. AES 推荐文章

    链接如下,写得很好!http://blog.csdn.net/a00553344/article/details/4002507

  9. Java for LeetCode 189 Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  10. Java for LeetCode 141 Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...