POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
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+1 in 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
G. Each test case starts with an integer number v, denoting the number of vertices of
G=(V,E), where the vertices will be identified by the integer 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
line.

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。
题意:在n个点m条边的有向图里面,问有多少个点是汇点。
解释一下输入:分别是V顶点数,E边数,下一行每两个点是一条边的出入点。 思路:很明显的Tarjan缩点,满足题意的汇点就是缩点以后出度为0的点。
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 5e3+;
int stack[maxn],dfn[maxn],low[maxn],head[maxn],dfs_num,top;
int color[maxn],col_num,in[maxn],out[maxn],mm[maxn],a[maxn];
bool vis[maxn];
class edge
{
public:
int to,next;
}e[maxn*maxn];
inline int gmin(int a,int b)
{
return a<b?a:b;
}
void Tarjan ( int x ) {
dfn[ x ] = ++dfs_num ;
low[ x ] = dfs_num ;
vis [ x ] = true ;//是否在栈中
stack [ ++top ] = x ;
for ( int i=head[ x ] ; i!= ; i=e[i].next ){
int temp = e[ i ].to ;
if ( !dfn[ temp ] ){
Tarjan ( temp ) ;
low[ x ] = gmin ( low[ x ] , low[ temp ] ) ;
}
else if ( vis[ temp ])low[ x ] = gmin ( low[ x ] , dfn[ temp ] ) ;
}
if ( dfn[ x ]==low[ x ] ) {//构成强连通分量
vis[ x ] = false ;
color[ x ] = ++col_num ;//染色
while ( stack[ top ] != x ) {//清空
color [stack[ top ]] = col_num ;
vis [ stack[ top-- ] ] = false ;
}
top -- ;
}
} int main()
{
int n,m;
while(scanf("%d",&n)){
if(!n)break;
scanf("%d",&m);
col_num=dfs_num=top=;
for(int i=;i<=n;i++)
head[i]=in[i]=out[i]=dfn[i]=;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
e[i].next=head[x];
e[i].to=y;
head[x]=i;
}
for(int i=;i<=n;i++)if(!dfn[i])Tarjan(i);
for(int i=;i<=n;i++)
{
for(int j=head[i];j;j=e[j].next)
{
int t=e[j].to;
if(color[i]!=color[t])
{
out[color[i]]++;
in[color[t]]++;
}
}
}
int k=,ans=;
for(int i=;i<=col_num;i++)
if(!out[i])mm[++k]=i;
for(int i=;i<=k;i++)
for(int j=;j<=n;j++)
if(mm[i]==color[j])a[++ans]=j;
sort(a+,a+ans+);
//printf("%d %d %d\n",k,ans,col_num);忘记初始化 debug多组样例一直过不了而加的..
for(int i=;i<=ans;i++)
printf("%d%c",a[i],i!=ans?' ':'\n');
}
return ;
}
POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)的更多相关文章
- 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 TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- poj 2553 The Bottom of a Graph(强连通、缩点、出入度)
题意:给出一个有向图G,寻找所有的sink点.“sink”的定义为:{v∈V|∀w∈V:(v→w)⇒(w→v)},对于一个点v,所有能到达的所有节点w,都能够回到v,这样的点v称为sink. 分析:由 ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- 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: ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
随机推荐
- 201503-1 图像旋转 Java
思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...
- 吴裕雄--天生自然 PHP开发学习:连接 MySQL、创建表
<?php $servername = "localhost"; $username = "root"; $password = "admin& ...
- 微服务项目开发学成在线_day01_CMS服务端开发
05-CMS需求分析-什么是CMS 什么是CMS?CMS (Content Management System)即内容管理系统,不同的项目对CMS的定位不同.CMS有哪些类型? 每个公司对每个项目的C ...
- MyBatis从入门到精通(第5章):5.4 Example 介绍
jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.Eclipse Version: 2019-12 M2 (4.14.0) MyBatis从入门到精通(第5章):MyBatis代码 ...
- python语法基础-并发编程-进程-进程锁和进程间通信
############### 守护进程 ############## """ 守护进程 父进程中将一个子进程设置为守护进程,那么这个子进程会随着主进程的结束而结束 ...
- ZJNU 2235 - EnDlEsS ChAsE
因为速度值保证各不相同 所以n只战斗人形会出现 n! 种不同情况 可以用不同id表示不同人形的速度 比如1 2 3三只人形 他们可能的排列有 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 ...
- 关于live2D的使用
<script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidge ...
- 计量经济与时间序列_ACF自相关与PACF偏自相关算法解析(Python,TB(交易开拓者))
1 在时间序列中ACF图和PACF图是非常重要的两个概念,如果运用时间序列做建模.交易或者预测的话.这两个概念是必须的. 2 ACF和PACF分别为:自相关函数(系数)和偏自相关函数(系数). ...
- 前端框架Bootstrap(10.7国庆补写)
框架的官网地址:https://v3.bootcss.com/ 主要学习Bootstrap框架提供的样式.组件.插件的使用. 首先下载到本地,在项目中导入使用: 下载的文件中包含:min.css的是压 ...
- TPO6-1 Powering the Industrial Revolution
By 1800 more than a thousand steam engines were in use in the British Isles, and Britain retained a ...