强连通分量+缩点(poj2553)
http://poj.org/problem?id=2553
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 8748 | Accepted: 3625 |
Description
Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1).
Then p is called a path from vertex v1 to vertex vn+1 inG and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of
all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.
Input
numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with
the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.
Output

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
求出连通块里的点满足下面条件:所有能到达点v的点w,v也能到达所有的w,因此要求的是联通块,然后缩点,求出度为零的连通块里的点,然后按照升序输出元素;
程序:
#include"stdio.h"
#include"string.h"
#include"queue"
#include"stack"
#include"iostream"
#define M 5009
#define inf 100000000
using namespace std;
struct node
{
int v;
node(int vv)
{
v=vv;
}
};
vector<node>edge[M];
stack<int>q;
int use[M],low[M],dfn[M],belong[M],num,index,in[M],out[M];
void tarjan(int u)
{
dfn[u]=low[u]=++index;
q.push(u);
use[u]=1;
for(int i=0;i<(int)edge[u].size();i++)
{
int v=edge[u][i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(use[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
num++;
int p;
do
{
p=q.top();
q.pop();
use[p]=0;
belong[p]=num;
}while(p!=u);
}
}
void slove(int n)
{
num=index=0;
memset(use,0,sizeof(use));
memset(dfn,0,sizeof(dfn));
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
}
int main()
{
int n,m,i;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
for(i=1;i<=n;i++)
edge[i].clear();
for(i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(node(v));
}
slove(n);
if(num==1)
{
for(i=1;i<=n;i++)
{
if(i==1)
printf("%d",i);
else
printf(" %d",i);
}
printf("\n");
continue;
}
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int u=1;u<=n;u++)
{
for(int j=0;j<(int)edge[u].size();j++)
{
int v=edge[u][j].v;
if(belong[u]!=belong[v])
{
out[belong[u]]++;
in[belong[v]]++;
}
}
}
int ff=0;
for(i=1;i<=n;i++)
{
if(!out[belong[i]])
{
if(ff==0)
printf("%d",i);
else
printf(" %d",i);
ff++;
}
}
printf("\n");
}
}
强连通分量+缩点(poj2553)的更多相关文章
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...
- 【poj2553】The Bottom of a Graph(强连通分量缩点)
题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)
题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)
这篇文章是从网络上总结各方经验 以及 自己找的一些例题的算法模板,主要是用于自己的日后的模板总结以后防失忆常看看的, 写的也是自己能看懂即可. tarjan算法的功能很强大, 可以用来求解强连通分量, ...
随机推荐
- thinkphp openfire 添加用户 骨架
public function addadd(){ header("Content-Type:text/html; charset=utf-8"); $user=$_POST['n ...
- Mysql经常使用基本命令汇总及默认账户权限与改动
一直仅仅是在浅显利用数据库存储数据.也被windows惯坏了.非常多命令使用的时候记不起来.so,换LINUX系统!不再使用GUI管理数据库!也想深入学习下Mysql.从权限管理開始.也就诞生了这篇学 ...
- LabVIEW中数组的自动索引
我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...
- 【Java NIO的深入研究】 ServerSocketChannel
Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样.ServerSocketChannel类在 jav ...
- hdu5087——Revenge of LIS II
Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- mysqldump如何针对某些数据库进行备份?针对某个数据库进行备份?
需求描述: 通过mysqldump工具对mysql服务器中的某几个数据库进行备份. 或者就对其中的一个数据库进行备份. 操作过程: 1.通过--databases参数后面加上数据库的名字进行备份 [m ...
- mysql数据库,如何在登录mysql之后执行操作系统上的SQL脚本?
需求描述: 通过mysql客户端登录到mysql数据库,如何执行操作系统上的SQL脚本文件呢? 操作过程: 1.编写测试脚本文件 [mysql@redhat6 scripts]$ cat SeCoun ...
- DoBox 下载
DoBox下载 一款简单十分好用的办公助手,用于记录您接下来需要做的事情.待办事项小工具 - DoBox DoBox下载 下载地址:http://www.wxzzz.com/?id=141 最新版本: ...
- cocos2d-x 2.2 创建项目
楼主用的是2.2版本号 曾经的版本号是要在vs中加入模版 建立项目 但新版本号更新后使用python建立项目 最好是python2.7以上 找到create_project.py文件所在路径 too ...
- 【VR】Leap Motion 官网文档 FingerModel (手指模型)
前言: 感谢关注和支持这个Leap Motion系列翻译的朋友们,非常抱歉因为工作原因非常久没有更新,今后这个翻译还会继续(除非官方直接给出中文文档).本篇献给大家的是 <FingerModel ...