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> ...
随机推荐
- LeeCode - Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- PUT 还是 POST ?
http://www.oschina.net/translate/put-or-post http://my.oschina.net/u/1263964/blog/268932 这两个方法咋一看都可以 ...
- 用Perl编写Apache模块
前言 Apache被许多大流量网站所嫌弃,但很多企业级的场景则更为适用. Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大.灵活而健壮且容易扩展 ...
- java之内的工具分享,附带下载链接,方便以后自己寻找
class反编译工具:http://pan.baidu.com/s/1geYvX5L redis客户端工具:http://pan.baidu.com/s/1eRJ4ThC mysql客户端-[mysq ...
- HDU 2256 Problem of Precision 数论矩阵快速幂
题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...
- Scala Collection简介
Traversable vs Iterable Traversable, Iterable 都是trait. Iterable 继承 Traversable. Traversable: 支持forea ...
- IOS6学习笔记(四)
1.GCD设置一个timer计时器 - (void)awakeFromNib { __weak id weakSelf = self; double delayInSeconds = 0.25; _t ...
- Vue中class与style绑定
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson07 一 用对象的方法绑定class 很简单,举个栗子: <!D ...
- Oracle SQL Tips
左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...
- java多线程系列6-阻塞队列
这篇文章将使用经典的生产者消费者的例子来进一步巩固java多线程通信,介绍使用阻塞队列来简化程序 下面是一个经典的生产者消费者的例子: 假设使用缓冲区存储整数,缓冲区的大小是受限制的.缓冲区提供wri ...