题意:如果v点能到的所有点反过来又能到v点,则v点是sink点,排序后输出所有的sink点。

思路:Tarjan缩点,输出所有出度为0的连通块内的点。

PS:一定要记得把数组清零!!!!!!!否则自己怎么死的都不知道。

原题请戳这里

#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dfn[5005],low[5005],p[5005],out[5005],n,m,t,cnt;
bool vis[5005];
vector<short>v[5005];
stack<short>stk;
priority_queue<int,vector<int>,greater<int> >pq;
void tarjan(int x)
{
vis[x]=1,stk.push(x),low[x]=dfn[x]=cnt++;
for(int i=0;i<v[x].size();i++)
if(!dfn[v[x][i]])
tarjan(v[x][i]),low[x]=min(low[x],low[v[x][i]]);
else if(vis[v[x][i]])
low[x]=min(low[x],dfn[v[x][i]]);
if(low[x]==dfn[x]){
int y;t++;
do y=stk.top(),stk.pop(),vis[y]=0,p[y]=t;while(y!=x);
}
}
void find(int x){for(int i=1;i<=cnt;i++)if(p[i]==x)pq.push(i);}
int main()
{
register int xx,yy;
while(scanf("%d",&n)&&n)
{
memset(out,0,sizeof(out));
memset(dfn,0,sizeof(dfn));
memset(vis,0,sizeof(vis));
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
v[i].clear();
cnt=t=0;
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&xx,&yy),v[xx].push_back(yy);
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)
for(int j=0;j<v[i].size();j++)
if(p[i]!=p[v[i][j]]) out[p[i]]++;
for(int i=1;i<=t;i++)
if(out[i]==0) find(i);
while(!pq.empty())
printf("%d ",pq.top()),pq.pop();
printf("\n");
}
}

POJ 2553 Tarjan的更多相关文章

  1. POJ - 2553 tarjan算法+缩点

    题意: 给你n个点,和m条单向边,问你有多少点满足(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}关系,并把这些点输出(要注意的是这个关系中是蕴含关系而不是且(&&)关系) 题解: ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. python tips:描述符descriptor

    描述符(descriptor)是实现了__get__.__set__.__del__方法的类,进一步可以细分为两类: 数据描述符:实现了__get__和__set__ 非数据描述符:没有实现__set ...

  2. WinForm窗体中窗口控件的生成

    1:button控件的生成方式 Button button = new Button(); button.Size = new Size(80, 80); button.Location = new ...

  3. 【转载】使用IntelliJ IDEA 配置Maven(入门)

    1. 下载Maven 官方地址:http://maven.apache.org/download.cgi 解压并新建一个本地仓库文件夹 2.配置本地仓库路径   3.配置maven环境变量      ...

  4. javaWEB中web.xml配置文件相关

    1.常用节点的执行顺序: context-param -> listenter -> filter -> servlet -> interceptor,其中有多个filter的 ...

  5. 【Apache Kafka】二、Kafka安装及简单示例

    (一)Apache Kafka安装 1.安装环境与前提条件   安装环境:Ubuntu16.04   前提条件: ubuntu系统下安装好jdk 1.8以上版本,正确配置环境变量 ubuntu系统下安 ...

  6. c++ map: 当map的value是void*指针

    #include <iostream> #include <map> #include <vector> using namespace std; //key is ...

  7. 使用Robo 3T 软件管理MongoDB数据库如何执行命令行shell

    比如使用命令行的方式查看数据库runoobdb中的sites集合(数据表)中的所有数据 1.在连接名的地方鼠标右键选择“open shell” 2.在出现的shell窗口中输入一下命令行,然后按ctr ...

  8. 【模板】非旋转Treap

    Treap,也叫做树堆,是指有一个随机附加域满足堆的性质的二叉搜索树. 如果一棵二叉搜索树插入节点的顺序是随机的,那我们得到的二叉搜索树在大多数情况下是平衡的,期望高度是log(n). 但有些情况下我 ...

  9. 获取当前日期,或指定日期的农历js代码

    时间不早了,直接上代码啦-- var CalendarData=new Array(100);var madd=new Array(12);var tgString="甲乙丙丁戊己庚辛壬癸& ...

  10. cogs 290. [CTSC2000] 丘比特的烦恼

    290. [CTSC2000] 丘比特的烦恼 ★★★   输入文件:cupid.in   输出文件:cupid.out   简单对比时间限制:1 s   内存限制:128 MB 随着社会的不断发展,人 ...