POJ2762 Going from u to v or from v to u? 强连通+缩点
题目链接:
题意:
给出一幅单向图。问这张图是否满足 随意两点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? 强连通+缩点的更多相关文章
- POJ2762 Going from u to v or from v to u(单连通 缩点)
判断图是否单连通,先用强连通分图处理,再拓扑排序,需注意: 符合要求的不一定是链拓扑排序列结果唯一,即在队列中的元素始终只有一个 #include<cstdio> #include< ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- [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 ...
- 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 ...
- 【缩点+拓扑判链】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 ...
- Oracle基本数据字典:v$database、v$instance、v$version、dba_objects
v$database: 视图结构: SQL> desc v$database; Name Null? Type - ...
- 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 ...
- 临时文件相关的v$tempfile v$sort_usage与V$tempseg_usage
SQL> select username,user,segtype,segfile#,segblk#,extents,segrfno# from v$sort_usage; SEGFILE#代表 ...
- [强连通分量] 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 ...
随机推荐
- wpf企业应用之主从结构列表
主从结构在企业级应用中相当常见,这里结合我的例子谈一下wpf中主从结构列表展示的常用做法,具体效果见 wpf企业级开发中的几种常见业务场景. 首先,Model有两种,主表对应model(假设为mode ...
- POJ2104 K-th Number 不带修改的主席树 线段树
http://poj.org/problem?id=2104 给定一个序列,求区间第k小 通过构建可持久化的点,得到线段树左儿子和右儿子的前缀和(前缀是这个序列从左到右意义上的),然后是一个二分的ge ...
- Hdu4903 The only survival
The only survival Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- bzoj 2555
暴力. 收获: 1.第一道后缀自动机,大概知道怎么写了,有一些原理性的东西还要理解. 2.计算right集合的大小 /***************************************** ...
- Html的学习随笔
在<head>的<style>中定义样式,有#号,比如#header就是定义一种名为header的样式,后面用id=header来调用:而无#号,比如直接就是header,那后 ...
- euclidea 3.0 全三星 攻略
euclidea攻略 游戏地址 http://www.euclidea.xyz/en/game/#/packs 攻略 Alpha level : 1.1 line tool 3L3E 智障题 1.2 ...
- CodeForces 32C. Flea 水题
C. Flea time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- 封装libsvm成可程序调用的C/C++类
libsvm很早之前就用了,现在封装一下方便自己使用,也方便大家更快的使用这个库,这个库一个挺有用的特性就是对测试样本的概率估计.源码在随笔的最后.liblinear的版本也是类似移植,主要是处理好数 ...
- ZOJ3673:1729
1729 is the natural number following 1728 and preceding 1730. It is also known as the Hardy-Ramanuja ...
- mysql---总体备份和增量备份
总体备份: 对整张表或者整个数据库甚至全部数据库进行备份. 增量备份: 对某一范围内的数据进行备份. 1.总体备份: 对表进行备份: 针对存储引擎为myisam的表,能够直接复制frm.myd.myi ...