本题分两步:

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. HDU 2844 Coins(多重背包)

    点我看题目 题意 :Whuacmers有n种硬币,分别是面值为A1,A2,.....,An,每一种面值的硬币的数量分别是C1,C2,......,Cn,Whuacmers想买钱包,但是想给人家刚好的钱 ...

  2. URAL(DP集)

    这几天扫了一下URAL上面简单的DP 第一题 简单递推 1225. Flags #include <iostream> #include<cstdio> #include< ...

  3. ASP.NET 導入Excel

    常常碰到這種需求,為了避免自己每次寫Code都要東翻西找Sample,乾脆丟上來當備份 此外,也為了方便網路上的大大們Copy Paste方便,小弟已經順便標示要複製程式碼的起始結束位置 在歡樂的貼程 ...

  4. C#:实体类中做数据验证

    主要是在实体类中验证 using System; namespace Jone.Function.attribute{        /// <summary>        /// 附加 ...

  5. VS2005 工程在win7下使用管理员权限运行

    想关资料 http://stackoverflow.com/questions/13030492/set-administrator-privilege-for-my-c-application-in ...

  6. Gtk中的文本视图(GtkTexViewWidget)

    Gtk中的文本视图(GtkTexViewWidget) Gtk中的文本视图(GtkTexView Widget) 在本章的Gtk+程序设计教程中,我们将重点介绍 GtkTexView 构件. GtkT ...

  7. C# Dynamic特性

    C# 4.0 dynamic:声明动态对象 dynamic关键字用于声明一个动态对象,然后通过该动态对象去调用方法或读写属性.这是C#4.0 添加的特性.官方解释:dynamic类型是帮助我们绕过编译 ...

  8. 【HTML】Beginner1:TagsAttributesElements

    HTML(Hypertext Text Mark-up Language)&(How To Make L) HTML is used for meaning and CSS is used f ...

  9. codeforce 621A Wet Shark and Odd and Even

    水 最大偶数和 #include<iostream> #include<string> #include<algorithm> #include<cstdli ...

  10. Linux设备驱动中的异步通知与异步I/O

    异步通知概念: 异步通知的意识是,一旦设备就绪,则主动通知应用程序,这样应用程序根本就不需要查询设备状态,这一点非常类似于硬件上的“中断”概念,比较准确的称谓是“信号驱动的异步IO”,信号是在软件层次 ...