本题分两步:

1 使用Tarjan算法求全部最大子强连通图。而且标志出来

2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集。

Tarjan算法就是模板算法了。

这里使用一个数组和一个标识号,就能够记录这个顶点是属于哪个子强连通图的了。

然后使用DFS递归搜索全部点及其边,假设有边的还有一个顶点不属于本子强连通图。那么就说明有出射的边。

有难度的题目:

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
using namespace std; const int MAX_VEC = 5005;
int n, m;
vector<int> gra[MAX_VEC];
stack<int> stk;
int dfn[MAX_VEC];//tarjan算法记录深度探索得到的标号
int low[MAX_VEC];//tarjan算法记录回溯得到的最低顶点编号
bool inStk[MAX_VEC];//记录是否在栈里面,也能够记录是否被訪问过了
int connectGrp[MAX_VEC];//标志所属的连通子图标号 == connectNum
int vecNum;//顶点标号
int connectNum;//最大强连通子图标号
int out[MAX_VEC];//出度记录 void tarjan(int u)
{
dfn[u] = low[u] = vecNum++;
stk.push(u);
inStk[u] = 1;
for (int i = 0; i < (int)gra[u].size(); i++)
{
int v = gra[u][i];
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);//两处的不同,和含义不同
}
else if (inStk[v]) low[u] = min(dfn[v], low[u]);//两处的不同
}
if (low[u] == dfn[u])
{
++connectNum;
while (stk.size())
{
int v = stk.top(); stk.pop();
inStk[v] = false;
connectGrp[v] = connectNum;
if (u == v) return;
}
}
} void solveConnect()
{
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(inStk, 0, sizeof(inStk));
for (int i = 1; i <= n; i++)
{
if (!dfn[i]) tarjan(i);
}
} void dfsCountOut(int u)
{
inStk[u] = true; //记录是否被訪问过了
for (int i = 0; i < int(gra[u].size()); i++)
{
int v = gra[u][i];
if (connectGrp[u] != connectGrp[v])
{
out[connectGrp[u]]++;//这个组的出度添加; connectGrp[] == connectNum
}
if (!inStk[v]) dfsCountOut(v);//深度优先,不须要使用额外空间
}
} void countConnectOut()
{
memset(inStk, 0, sizeof(inStk)); //这里仅仅记录是否被訪问过的了。
memset(out, 0, sizeof(out));
for (int i = 1; i <= n; i++)
{
if (!inStk[i]) dfsCountOut(i);
}
} int main()
{
int u, v;
while (scanf("%d", &n) && n)
{
connectNum = 0;
vecNum = 1;
scanf("%d", &m);
for (int i = 1; i <= n; i++)
gra[i].clear();
while (stk.size()) stk.pop(); //清零。重中之重 for (int i = 0; i < m; i++)
{
scanf("%d %d", &u, &v);
gra[u].push_back(v); //有向图
} solveConnect();
countConnectOut(); for(int i = 1; i <= n; ++i)
if(out[connectGrp[i]] == 0)
printf("%d ", i);
putchar('\n');
}
return 0;
}

POJ 2553 The Bottom of a Graph TarJan算法题解的更多相关文章

  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强连通分量]

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

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

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

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

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

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

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

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

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

  8. poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点

    /** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...

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

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

随机推荐

  1. ANDROID_MARS学习笔记_S02_001_Spinner

    1.strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> < ...

  2. velocity-1.7学习笔记

    Velocity是由Apache软件组织提供的一项开放源码项目,它是一个基于Java的模板引擎.通过Velocity模板语言(Velocity Template Language,VTL)定义模板(T ...

  3. Java之关键字static和final的使用

    static 在Java中声明属性.方法时,可使用关键字static来修饰. 1.static变量       按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或 ...

  4. jQuery name属性与checked属性结合获取表单元素值

    var paytype = $("input[name='paytype']:checked").val(); alert(paytype); input元素下名称为paytype ...

  5. 基于WebForm+EasyUI的业务管理系统形成之旅 -- 首页Portal界面拖拽(Ⅵ)

    上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 构建Web界面>,主要介绍系统界面布局.导出数据等. 本篇将介绍首页Portal界面拖拽. 一.首页Portal界面拖拽 ...

  6. (转载) mysql中,option是保留字段么?

    (转载)http://book.77169.org/101/50364.htm update thread set active=0,option='lock',manager='书生' where ...

  7. 一行代码搞定Adapter

    15年Google I/O大会发不了三个重要支持库 >Material design (Android Support Design) >百分比布局:Percent support lib ...

  8. leetcode 逆转字符串 当年的第一题,今天再写一遍,物是人非

    public class Solution { public String reverseWords(String s) { if(s==null||s.length()==0) return &qu ...

  9. Java笔记(二十四)……集合工具类Collections&Arrays

    Collections 集合框架的工具类,方法全部为静态 Collections与Collection的区别 Collection是集合框架的一个顶层接口,里面定义了单列集合的共性方法 Collect ...

  10. HADOOP2.2.0安装配置指南

    一.     集群环境搭建 这里我们搭建一个由三台机器组成的集群: Ip地址 用户名/密码 主机名 集群中角色 操作系统版本 192.168.0.1 hadoop/hadoop Hadoop-mast ...