/**
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) 存环中的点的更多相关文章

  1. POJ 2553 The Bottom of a Graph (Tarjan)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11981   Accepted: ...

  2. POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  3. POJ 2553 The Bottom of a Graph TarJan算法题解

    本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...

  4. [poj 2553]The Bottom of a Graph[Tarjan强连通分量]

    题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...

  5. POJ 2553 The Bottom of a Graph(强连通分量)

    POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...

  6. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  7. poj 2553 The Bottom of a Graph【强连通分量求汇点个数】

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9641   Accepted:  ...

  8. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  9. POJ 2553 The Bottom of a Graph 【scc tarjan】

    图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...

随机推荐

  1. 初学orcale(一)

    Oracle数据库学习: 01.数据库简介: (1)文件型数据库: Access Office组件: Foxpro (2)NoSql数据库(泛指非关系型数据库): NoSQL(NoSQL = Not ...

  2. JavaSE环境Shiro的搭建及常用API

    通过shiroAPI来进行角色的管理 模拟用户是否登录: 模拟用户是否具有相应的权限:

  3. 2、eclipse中使用Maven

    1.导入Maven项目 1.1从spring官网下载示例工程 访问Spring官网 点击[Browse the Guides]即可看到Spring官方为我们提供的很多Demo.

  4. Django问卷调查项目思路流程

    Django问卷调查项目思路流程: 1 后端思路 : 需求分析 ---- 找出各实体对应关系 ---- 设计model架构 ---- 统一资源封装 --- 提供资源API入口 ---- 设计项目实体功 ...

  5. 再学UML-Bug管理系统UML2.0建模实例(二)

    2.3 BMS顺序图(需求模型)       在UML中,我们将顺序图分为两类,一类用于描述系统需求,构造系统的需求模型(分析模型):另一类用于指导设计与实现,构造系统的实现模型(设计模型).     ...

  6. 【Hibernate那点事儿】—— Hibernate知识总结

    前言: 上一篇简单的讲解了下Hibernate的基础知识.这里对Hibernate比较重要的一些知识点,进行总结和归纳. 手码不易,转载请注明!——xingoo 总结的知识点: 1 关于hiberna ...

  7. [EffectiveC++]item31:将文件间的编译依存关系降至最低

    P143:“声明的依赖性"替换“定义的依存性”

  8. python UI自动化实战记录一:测试需求与测试思路

    测试需求: 项目包含两个数据展示页面,数据均来自于四个数据源接口. 测试操作步骤: 选择5个大类型中的一个,每个大类型下有3个子类型,选择任一子类型,页面数据更新.需验证页面上的数据与数据源接口数据一 ...

  9. log4net 配置完成后发现不能输出日志的解决方法

    配置好log4net后发现日志不能输出,打开调试看一下几个属性都是false,(比如isdebugenable =false)这其实是项目的启动时候没有加入一行声明代码导致的,可以在程序的Assemb ...

  10. Django:Django的路由系统

    一,URLconf配置 1,基本格式 from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图函数,参数,别名), ] 2 ...