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 ...
随机推荐
- pycharm安装使用,python运算规则
首先讲了pycharm的安装和使用,基本上算是个脚本编辑器.另外pycharm的一些操作方法:http://edu.51cto.com/course/9043.html 葫芦老师录的pycharm视频 ...
- Lost connection to MySQL server at 'reading initial communication packet', system error: 0的一个解决方案
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT在icmp之前/etc/init.d/iptables restar ...
- empireCMS 帝国cms功能总结
上1 系统 对应左菜单为 系统设置 系统参数设置 基本属性 站点名称,网站地址,关键字,简介,首页模式,php时间, 前台功能,操作时间,来源地址,验证码 用户属性 后台验证码开启,次数限制,时间限制 ...
- Using PWM Output as a Digital-to-Analog Converter
http://www.ti.com/lit/an/spraa88a/spraa88a.pdf http://www.ti.com/litv/zip/spraa88a The high-resoluti ...
- [Asp.net web api]缓存
摘要 为了提高接口的性能,我们常做的优化就包括缓存,对经常访问但变化不大的数据进行缓存.或者使用http的缓存,减少请求的次数. web api缓存 在提供的api,我们也可以实现缓存,来减少访问的次 ...
- python定位性能的工具
参考: http://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/ http://xianglong.me/article/analys ...
- C#程序集系列10,强名称程序集
当一个程序集的名称,版本,文化,Public Key都做了设置,就可以把这个程序集叫做"强名称程序集".强名称程序集可以防止被仿冒或篡改.本篇首先创建一个强名称程序集,接着模拟篡改 ...
- java基础知识概要图
- Android实例剖析笔记(二)
摘要:用实例讲解Andriod的开发过程,以NotesList为实例介绍Android的菜单机制 简介 android提供了三种菜单类型,分别为options menu,context menu,su ...
- Extjs文件选择器
Ext.hoo.component.FileBrowserComponent.js /** * Ext.hoo.component.FileBrowserWindow 系统文件浏览选择组件,可以选定电 ...