poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/**
problem: http://poj.org/problem?id=2553
将所有出度为0环中的点排序输出即可。
**/ #include<stdio.h>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std; class Graphics{
const static int MAXN = ;
const static int MAXM = MAXN * MAXN;
private:
struct Edge{
int to, next;
}edge[MAXM];
struct Point{
int dfn, low, color;
Point(){dfn = low = color = ;}
}point[MAXN], emptyPoint;
int first[MAXN], sign, colorNum, dfnNum, sumOfPoint;
bool vis[MAXN];
vector<int> ring[MAXN];
stack<int> stk;
void tarjan(int u){
point[u].dfn = ++ dfnNum;
point[u].low = dfnNum;
vis[u] = true;
stk.push(u);
for(int i = first[u]; i != -; i = edge[i].next){
int to = edge[i].to;
if(!point[to].dfn){
tarjan(to);
point[u].low = min(point[to].low, point[u].low);
}else if(vis[to]){
point[u].low = min(point[to].dfn, point[u].low);
}
}
if(point[u].low == point[u].dfn){
vis[u] = false;
point[u].color = ++colorNum;
ring[colorNum].push_back(u);
while(stk.top() != u){
vis[stk.top()] = false;
point[stk.top()].color = colorNum;
ring[colorNum].push_back(stk.top());
stk.pop();
}
stk.pop();
}
}
public:
void clear(int n){
sign = colorNum = dfnNum = ;
sumOfPoint = n;
for(int i = ; i <= n; i ++){
first[i] = -;
vis[i] = false;
ring[i].clear();
point[i] = emptyPoint;
}
while(!stk.empty()) stk.pop();
}
void addEdgeOneWay(int u, int v){
edge[sign].to = v;
edge[sign].next = first[u];
first[u] = sign ++;
}
void tarjanAllPoint(){
for(int i = ; i <= sumOfPoint; i ++){
if(!point[i].dfn){
tarjan(i);
}
}
}
vector<int> getAns(){
vector<int> ans;
int *outdegree = new int[sumOfPoint+];
for(int i = ; i <= sumOfPoint; i ++){
outdegree[i] = ;
}
tarjanAllPoint();
for(int i = ; i <= sumOfPoint; i ++){
for(int j = first[i]; j != -; j = edge[j].next){
int to = edge[j].to;
if(point[to].color != point[i].color){
outdegree[point[i].color] ++;
}
}
}
for(int i = ; i <= colorNum; i ++){
if(!outdegree[i]){
for(int j = ; j < ring[i].size(); j ++){
ans.push_back(ring[i][j]);
}
}
}
sort(ans.begin(), ans.end());
delete []outdegree;
return ans;
}
}graph; int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF && n){
graph.clear(n);
while(m --){
int a, b;
scanf("%d%d", &a, &b);
graph.addEdgeOneWay(a, b);
}
vector<int> ans = graph.getAns();
bool first = ;
for(int i = ; i < ans.size(); i ++){
if(first) first = ;
else putchar(' ');
printf("%d", ans[i]);
}
putchar('\n');
}
return ;
}
poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点的更多相关文章
- 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找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- 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(强连通分量)
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 【scc tarjan】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...
随机推荐
- CSP学习之CryptoAPI初识
Crypto API目的就是提供开发者在windows下使用PKI的编程接口. Crypto 提供了很多的加解密相关函数,如编码.解码.加密解密,哈希,数字证书.证书管理证书存储等. 有关 ...
- HashMap put、get方法源码分析
HashMap.java的实现是面试必问的问题. JDK版本 java version "1.8.0_91" Java(TM) SE Runtime Environment (bu ...
- SQL Server ->> CLR存储过程枚举目录文件并返回结果集
因工作需要写了个CLR存储过程枚举目录文件并返回结果集 using System; using System.IO; using System.Collections.Generic; using S ...
- 将CSV文件中的数据导入到SQL Server 数据库中
导入数据时,需要注意 CSV 文件中的数据是否包含逗号以及双引号,存在时,导入会失败 选择数据库 -> 右键 -> 任务 -> 导入数据 ,然后根据弹出的导入导出向导(如下图)中的提 ...
- 1.GlusterFS 初识
一. GlusterFS 初始 1.1 分布式文件系统出现 计算机通过文件系统管理.存储数据,而现在数据信息爆炸的时代中人们可以获取的数据成指数倍的增长,单纯通过增加硬盘个数来扩展计算机文件系统的存储 ...
- May 11th 2017 Week 19th Thursday
If you fell down yesterday, stand up today. 昨天跌倒了,今天仍然要站起来. From Herbert George Wells. If you fell d ...
- NO.002-2018.02.07《越人歌》先秦:佚名
参考之后略有修改,疑问点“不訾诟耻”释义 越人歌原文.翻译及赏析_古诗文网 蒙羞被好兮不訾诟耻_释义_吴江诗词网 越人歌 先秦:佚名 今夕何夕兮,搴舟中流.今晚是怎样的晚上啊河中漫游.搴(qiān ...
- 将springboot打包成的jar文件做成windows服务
1.在idea中用maven将程序打成jar,放到运行的目录中. 2.去github上面下载winsw: https://github.com/kohsuke/winsw/releases 3. 将W ...
- Android 回调的理解,觉得写得好就转过来。。。收藏一下
转自:一个经典例子让你彻彻底底理解java回调机制 以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了很多也不是很明白,现在知道了,所 ...
- [转]Linux学习
Linux简介与厂商版本 http://www.cnblogs.com/vamei/archive/2012/09/04/2671103.html Linux开机启动(bootstrap) http: ...