Semiconnected--强连通缩点
1451: Semiconnected
时间限制: 1 Sec 内存限制: 32 MB
提交: 79 解决: 20
题目描述
For a directed graph G = (V, E), if for all pairs of nodes u,v, u can always reach v or v can always reach u , then we call this a Semiconnected graph. Now you are given a directed
graph, you should tell us whether it is a Semiconnected graph. If it is, output “yes”, or else“no”.
输入
There are several test cases, for each test case, there are two integers in the first line, n and m n(n<=10000) ,m(m<=50000) , means that there are n nodes and m edges in this graph.
The nodes are numbered from 1 to n. Then m lines each with two integers u and v means there is an edge from u to v.
输出
For each test case, output “yes” if it is a Semiconnected graph, or else “no” instead.
样例输入
4 3
1 2
2 3
3 4
4 3
1 2
2 3
4 3
样例输出
yes
no
题意:给你一个有向图,若从图中任意两个点u,v。都有从u到v的路径,或者从v到u的路径,则该图为半连通的,是则输出yes,不是则输出no
思路:若该图符合以上情况,
1:入度为0的点多于一个,不符合。
2:对该图进行缩点后,若为半连通图,则当前的图为一条直线,统计新图中入度,出度为0的点是不是都只有一个即可。
代码如下:
#include "stdio.h" //强连通缩点后,判断新图中入度为0,出度为0的点是否都只有一个
#include "string.h"
#include "vector"
#include "stack"
using namespace std; #define N 10005
#define M 50005 struct node
{
int x,y;
int next;
}edge[M];
int idx,head[N];
void Init(){ idx=0; memset(head,-1,sizeof(head)); }
void Add(int x,int y)
{
edge[idx].x=x; edge[idx].y=y;
edge[idx].next=head[x]; head[x]=idx++;
} int n;
int dfs_clock;
bool mark[N];
int scc_cnt;
int belong[N];
stack<int> q;
int num[N];
int low[N],dfn[N];
int MIN(int a,int b) { return a<b?a:b; } void DFS(int x)
{
int i,y;
q.push(x);
mark[x] = true;
low[x] = dfn[x] = ++dfs_clock;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y=edge[i].y;
if(!dfn[y])
{
DFS(y);
low[x] = MIN(low[x],low[y]);
}
else if(mark[y])
low[x] = MIN(dfn[y],low[x]);
}
if(low[x]==dfn[x])
{
int temp;
scc_cnt++;
while(1)
{
temp = q.top();
q.pop();
belong[temp] = scc_cnt;
num[scc_cnt]++;
mark[temp] = false;
if(temp==x) break;
}
}
} int ru_du[N],chu_du[N]; void Solve(int id)
{
int i;
int x,y;
dfs_clock = scc_cnt = 0;
memset(dfn,0,sizeof(dfn));
memset(num,0,sizeof(num));
memset(belong,0,sizeof(belong));
memset(mark,false,sizeof(mark));
DFS(id);
for(i=1; i<=n; ++i)
{
if(!dfn[i])
DFS(i);
}
memset(ru_du,0,sizeof(ru_du));
memset(chu_du,0,sizeof(chu_du));
for(i=0; i<idx; ++i)
{
x = edge[i].x;
y = edge[i].y;
if(belong[x]!=belong[y])
{
chu_du[belong[x]]++;
ru_du[belong[y]]++;
}
}
int ru_0=0;
int chu_0=0;
for(i=1; i<=scc_cnt; ++i)
{
if(ru_du[i]==0)
ru_0++;
if(chu_du[i]==0)
chu_0++;
}
if(ru_0==1 && chu_0==1)
printf("yes\n");
else
printf("no\n");
} int ru[N],chu[N]; int main()
{
int i,m;
int x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
Init();
memset(ru,0,sizeof(ru));
memset(chu,0,sizeof(chu));
while(m--)
{
scanf("%d%d",&x,&y);
Add(x,y);
chu[x]++;
ru[y]++;
}
int ans=0,id=1;
for(i=1; i<=n; ++i)
{
if(ru[i]==0)
{
ans++;
id=i;
}
}
if(ans>1) { printf("no\n"); continue; }
Solve(id);
}
return 0;
}
Semiconnected--强连通缩点的更多相关文章
- poj2553 强连通缩点
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10114 Accepted: ...
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...
- hdu 2767 Proving Equivalences 强连通缩点
给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- poj-1904(强连通缩点)
题意:有n个王子,每个王子都有k个喜欢的女生,王子挑选喜欢的女生匹配,然后再给你n个王子最开始就定好的匹配,每个王子输出能够结合且不影响其他王子的女生匹配 解题思路:强连通缩点,每个王子与其喜欢的女生 ...
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
- BZOJ1179 [Apio2009]Atm Tarjan 强连通缩点 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1179 题意概括 有一个有向图,每一个节点有一个权值,其中有一些结束点. 现在,你要从S出发,到达任 ...
- BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...
- 强连通缩点— HDU1827
强连通缩点以后最终形成的是一棵树 我们可以根据树的性质来看缩点以后的强连通分量图,就很好理解了 /* gyt Live up to every day */ #include<cstdio> ...
随机推荐
- 【JavaScript回顾】闭包
什么是闭包? 闭包是指有权访问另一个 函数作用域中的变量的函数(也就是说,你这个函数用到的变量另外一个域的就算闭包) <script> function f1() { var age = ...
- 用c#写的一个局域网聊天客户端 类似小飞鸽
用c#写的一个局域网聊天客户端 类似小飞鸽 摘自: http://www.cnblogs.com/yyl8781697/archive/2012/12/07/csharp-socket-udp.htm ...
- 线段树的区间更新---A Simple Problem with Integers
POJ 3468 Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 100 ...
- 【Asphyre引擎】关于AsphyreTypes中OverlapRect的改动,都是泪啊!!!
OverlapRect改动:两个参数对调了.想问问LP,这样真的好吗? Sphinx304版本的代码: function OverlapRect(const Rect1, Rect2: TRect): ...
- Python参数组合
参数定义的顺序必须是:①必选参数.②默认参数.③可选参数.④命名关键字参数.⑤关键字参数 #a,b为必选参数:c为默认参数:args为可变参数:kw为关键字参数 def f1(a,b,c=0,*arg ...
- [TypeScript] 建置输出单一JavaScript档案(.js)与Declaration档案(.d.ts)
[TypeScript] 建置输出单一JavaScript档案(.js)与Declaration档案(.d.ts) 问题情景 开发人员使用Visual Studio来开发TypeScript,可以很方 ...
- MUI - 图片预览(perviewimage)的优化
主要对mui图片全屏预览插件做了以下三点补充 1.添加了预览图片文字说明,使用的时候需要添加以下css及DOM属性 .mui-slider-img-content { position: absolu ...
- ABAP:SAP报表性能的优化
大部分ABAPer都是从SAP报表及打印开始学起的,大家也都认为写个SAP报表程序是最简单不过的事了. 但是实际情况真的如此吗?写报表时除了保证数据的准确性,您可曾考虑过报表的性能问题吗? 由于报表程 ...
- andriod 获取电池的信息
<?xml version="1.0"?> <LinearLayout android:orientation="vertical" andr ...
- [ html canvas 透明度 globalApha ] canvas绘图属性 透明度 globalApha 属性演示
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...