题目链接:

poj2762

题意:

给出一幅单向图。问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a

题解思路:

推断一幅图是否满足弱连通

首先想到的是将图中的 强连通分量(能互相到达的顶点集)  进行缩点

然后再依据原有边 又一次建图

假设缩点后的图是一条单链(回路,通路都能够)   则一定满足弱连通

推断是否是一条单链 能够依据建图过程中得到 入度 出度 数组进行推断

某点的入度 或 出度假设大于1则一定不是单链

另外单链仅仅能有一条  不能有多个点入度=0

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define maxn 1050
using namespace std;
struct node
{
int to,next;
} edge[maxn*6];
int head[maxn];
int s; int dfn[maxn],low[maxn],num; int sta[maxn],insta[maxn],top; int belong[maxn],block; void init()
{
memset(head,-1,sizeof(head));
memset(insta,0,sizeof(insta));
memset(dfn,0,sizeof(dfn));
block=s=num=top=0;
} void addedge(int a,int b)
{
edge[s]= {b,head[a]};
head[a]=s++;
} void Tarjan(int u,int pre)
{
dfn[u]=low[u]=++num;
insta[u]=1;
sta[top++]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
Tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(insta[v]) //回边
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]) //缩点
{
int d;
block++;
while(d!=u)
{
d=sta[--top];
insta[d]=0;
belong[d]=block;
}
}
} int rebuild(int n)
{
int indegree[maxn]= {0};
int outdegree[maxn]={0};
int u,v;
for(int i=1; i<=n; i++) //又一次建图
{
u=belong[i];
for(int j=head[i]; j!=-1; j=edge[j].next)
{
v=edge[j].to;
v=belong[v];
if(u!=v) //不在同一个强连通分量才干建边
{
outdegree[u]++;
indegree[v]++;
if(indegree[v]>1||outdegree[u]>1)
return 0;
}
}
}
int ss=0;
for(int i=1; i<=block; i++)
if(!indegree[i])
ss++;
if(ss>1) //入度=0的点有多个
return 0;
return 1;
} int main()
{
int n,m,a,b;
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&a,&b);
addedge(a,b);
}
for(int i=1;i<=n;i++) //
if(!dfn[i]) //有向图Tarjan算法
Tarjan(i,-1); // if(rebuild_topsort(n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
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(单连通 缩点)

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

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

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

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

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

  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. PyQt QString 与 Python str&unicode

    昨日,将许久以前做的模拟网页登录脚本用PyQt封装了一下,结果出大问题了, 登录无数次都提示登录失败!!而不用PyQt实现的GUI登录直接脚本登录无数次都提示登录成功!!心中甚是伤痛,于是探究起来,解 ...

  2. 使用Chrome快速实现数据的抓取(五)—— puppeteer

    如果要以自动化的方式驱动Chrome进行数据抓取,必须实现Chrome Dev Protocol协议的客户端.这个协议本身并不复杂,我在之前的文章中也简单的介绍过一下. Google本身有一个Node ...

  3. solaris之cpu

    一. Solaris的处理器硬件系统架构 Solaris支持多种处理器系统架构:SPARC.x86和x64.x64即AMD64及EMT64处理器.在版本2.5.1的时候,Solaris曾经一度被移植到 ...

  4. oracle 锁系列

    http://www.cnblogs.com/lhrbest/p/6091277.html

  5. buffer and cache -systemtap

    http://blog.csdn.net/dianhuiren/article/details/7543886

  6. Xsolla和Crytek合作,对游戏战争前线推出全新支付方式

    新闻稿: Sherman Oaks, 加州 (美国) –2014年 10月 15日-计费提供商Xsolla今日正式宣布.和著名游戏开发商以及发行商 Crytek.这次合作意味着玩家能够期待大量的游戏内 ...

  7. 报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>"

    报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>&q ...

  8. 32.NET中加密解密基本概念

    对消息的接收方来说,安全的交流方式需要同时满足3个条件: 1.完整性:消息在传输途中没有被篡改过,即消息是完好无损的. 2.保密性:接收放可以理解或解密来自发送方的信息.(不保证第三方无法获得,但保证 ...

  9. javascript中的2个感叹号的用法

    !!是逻辑"非非",即是在逻辑"非"的基础上再"非"一次.通过!或!!可以将很多类型转换成bool类型,再做其它判断.     应用场景:判 ...

  10. App Store常用推广方法

    转:http://www.cocoachina.com/bbs/read.php?tid-5000.html 天天潜水,在这里获益不少.不贡献一点似乎过意不去,所以在这里根据自己的经验谈谈基本的推广方 ...