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& ...
随机推荐
- 201509-2 日期计算 Java
思路: 每月有多少天是固定的,放到数组中,2月单独判断一下. import java.util.Scanner; public class Main { public static void main ...
- git submodule update --init 和 --remote的区别
git 的submodule 工具方便第三方库的管理,比如gitlab 上的各种开源工具,spdlog等 在项目目录下创建.gitmodule 里可以添加第三方库,然后在更新第三方库时,有两个选项 g ...
- python使用进程池多进程时,如何打印错误信息
一.说明 1.python进程池进行多进程运行时,如果有错误,该进程会直接跳过,并且不会打印错误信息. 2.如果需要了解到进程内的错误信息,此时就需要通过捕获异常来输出错误信息了. 二.具体方法如下: ...
- 面向对象 part4 构造函数对象重写
出处 其中深奥之处非看一次能了解 !对象真的有点绕,但是又很严谨
- Python操作redis总结
安装模块及配置 首先安装redis,在Ubuntu下输入指令pip install redis即可.下载完成后,cd到指定目录下,打开指定文件,如下图所示: 输入密码打开后,修改指定地方的内容,与上篇 ...
- v-charts使用总结(随时补充)
柱状图.折线图.环图的常用配置(配置连接地址https://v-charts.js.org/#/line) :data 绑定基本数据 { // 第一个参数为维度(就是横轴,例如时间),剩余为指标(就是 ...
- python妹子图爬虫5千张高清大图突破防盗链福利5千张福利高清大图
meizitu-spider python通用爬虫-绕过防盗链爬取妹子图 这是一只小巧方便,强大的爬虫,由python编写 所需的库有 requests BeautifulSoup os lxml 伪 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- 基于JSP开发医院预约挂号系统 Java源码
开发环境: Windows操作系统 开发工具: Eclipse+Jdk+Tomcat+MYSQL数据库 运行效果图: 源码及原文链接:http://javadao.xyz/forum.php?mod= ...
- 工作记录mysql主从复制
环境ubuntu 16.04 主配置 1.编辑主MySQL配置文件vim /etc/mysql/mysql.conf.d/mysqld.cnf 更改server-id,它位于[mysqld]段.这个数 ...