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 ...
随机推荐
- TCP的三次握手与四次释放
TCP的三次握手与四次释放 一.名词解释 序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生:给字节编上序号后,就给 ...
- hdu 2112 最短路
本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点 就是个最短路 Sample Input 6 xiasha westlake xiasha station 60 x ...
- 【POJ】1862:Stripies【贪心】【优先队列】
Stripies Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20456 Accepted: 9098 Descrip ...
- 【BZOJ】1864: [Zjoi2006]三色二叉树
1864: [Zjoi2006]三色二叉树 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1295 Solved: 961[Submit][Status ...
- Codeforces Beta Round #97 (Div. 1) B. Rectangle and Square 暴力
B. Rectangle and Square 题目连接: http://codeforces.com/contest/135/problem/B Description Little Petya v ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- CF 277.5 B.BerSU Ball 二分图的最大匹配 模版题
题意:求二分图的最大匹配数量 模版如下: //二分图匹配(匈牙利算法的DFS实现) //初始化:g[][]两边顶点的划分情况 //建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹 ...
- CGI 、PHP-CGI、FASTCGI、PHP-FPM
CGI是干嘛的? CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.web server(比如说nginx)只是内容的分发者.比如,如果请求的是/index/ht ...
- clip-path 教程:使用 CSS 中的 clip-path 轻松实现多边形
作为一个前端开发,一个主要的工作就是来实现设计师设计的UI界面.而在UI界面中,各种各样的形状元素应用则是随处可见,比如三角形: 以前碰到这种形状的时候,会使用各种黑科技的技巧,比如使用CSS中的bo ...
- MySQL数据库事务各隔离级别加锁情况--read uncommitted篇(转)
本文转自https://m.imooc.com/article/details?article_id=17291,感谢作者 1.目的 1.1 合适人群 1.数据库事务特征我只是背过,并没有很深刻的理解 ...