The Bottom of a Graph(tarjan + 缩点)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 9139 | Accepted: 3794 |
Description
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+1in G 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
Output

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
Source
参考代码这里:http://blog.csdn.net/ehi11/article/details/7884851
缩点:(这个概念也是看了别人的理解)先求有向图的强连通分量 , 如果几个点同属于一个强连通 , 那就给它们标上相同的记号 , 这样这几个点的集合就形成了一个缩点。
题目大意:求出度为0的强连通分量
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int M = ;
int n , m ;
int stack [M] , top = , index = ;
bool instack [M] ;
int dfn[M] , low[M] ;
int cnt = ;
vector <int> e[M] ;
int belong[M] ;
int out[M] ; void init (int n)
{
top = ;
cnt = ;
index = ;
memset (stack , - , sizeof(stack)) ;
memset (instack , , sizeof(instack)) ;
memset (dfn , - , sizeof(dfn)) ;
memset (low , - , sizeof(low)) ;
for (int i = ; i <= n ; i++)
e[i].clear () ;
memset (belong , - , sizeof(belong)) ;
memset (out , , sizeof(out)) ;
} void tarjan (int u)
{
int v ;
dfn[u] = low[u] = index++ ;
instack[u] = true ;
stack[++top] = u ;
for (int i = ; i < e[u].size () ; i++) {
v = e[u][i] ;
if (dfn[v] == -) {
tarjan (v) ;
low[u] = min (low[u] , low[v]) ;
}
else if (instack[v])
low[u] = min (low[u] , dfn[v]) ;
}
if (low[u] == dfn[u]) {
cnt++ ;
do {
v = stack[top--] ;
instack[v] = false ;
belong[v] = cnt ;
} while (u != v) ;
}
} int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
int u , v ;
while (~ scanf ("%d" ,&n)) {
if (n == )
break ;
init (n) ;
scanf ("%d" , &m) ;
while (m--) {
scanf ("%d%d" , &u , &v) ;
e[u].push_back (v) ;
}
for (int i = ; i <= n ; i++) {
if (dfn[i] == -)
tarjan (i) ;
}
for (int i = ; i <= n ; i++) {
for (int j = ; j < e[i].size () ; j++) {
if (belong [i] != belong[e[i][j]])
out[belong[i]] ++;
}
}
int k = ;
for (int i = ; i <= n ; i++) {
if (out[belong[i]] == ) {
if (k++)
printf (" ") ;
printf ("%d" , i) ;
}
}
puts ("") ;
}
return ;
}
The Bottom of a Graph(tarjan + 缩点)的更多相关文章
- POJ2533&&SP1799 The Bottom of a Graph(tarjan+缩点)
POJ2553 SP1799 我们知道单独一个强连通分量中的所有点是满足题目要求的 但如果它连出去到了其他点那里,要么成为新的强连通分量,要么失去原有的符合题目要求的性质 所以只需tarjan缩点求出 ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- POJ 2553 The Bottom of a Graph (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- poj--2553--The Bottom of a Graph (scc+缩点)
The Bottom of a Graph Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Oth ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- 【图论】The Bottom of a Graph
[POJ2553]The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11182 ...
随机推荐
- 『片段』OracleHelper (支持 多条SQL语句)
C# 调用 Oracle 是如此尴尬 >System.Data.OracleClient.dll —— .Net 自带的 已经 过时作废. >要链接 Oracle 服务器,必须在 本机安装 ...
- flatbuffers 使用问题记录
1. 命名空间的问题 ----------------------------- namespace 1.0.3 版本包含文件类型前面不需要加命名空间,但是1.1.0 中包含需要在类型前加命名空间 i ...
- 变量监控 指令 gt-wach
index8.html <html><head> <title>变量监控指令 gt-watch</title> <script src=" ...
- Bootstrap系列 -- 38. 基础导航条
在制作一个基础导航条时,主要分以下几步: 第一步:首先在制作导航的列表(<ul class=”nav”>)基础上添加类名“navbar-nav” 第二步:在列表外部添加一个容器(div), ...
- openwrt的路由器重置root密码
家里路由器刷了openwrt,结果长期没登录,忘了root密码. 很容易就找到了这里介绍的办法 http://www.openwrt.org.cn/bbs/thread-12327-1-1.html ...
- dp式子100个……
1. 资源问题1-----机器分配问题F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2. 资源问题2------01背包问题F[I,j]:=max(f[i- ...
- iOS边练边学--NSURLSession、NSURLSessionTask的介绍与使用以及url中包含了中文的处理方法
一.NSURLSession.NSURLSessionTask的使用步骤 首先创建NSURLSession对象 通过NSURLSession对象创建对应的任务 <1>NSURLSessio ...
- DVR分布式路由
1. 背景 没有使用DVR的场景: 从图中可以明显看到东西向和南北向的流量会集中到网络节点,这会使网络节点成为瓶颈. 如果启用DVR,如下图: 对于东西向的流量, 流量会直接在计算节点之间传递. 对于 ...
- VS2013打开项目提示此版本的应用程序不支持其项目类型(.csproj)
命令行或者Vs自带的命令提示符输入: devenv.exe /resetskippkgs 重新打开项目即可.
- 【探秘ES6】系列专栏(一):ES6简介
摘要:新一代JavaScript标准,ES6即将发布.[探秘ES6]系列专栏将一一剖析ES6的诸多新特性,让Web开发者对此有清晰全面的了解.本文为系列的第一篇,带你了解ES6到底是什么以及有哪些令人 ...