题意:如果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. css3 animation 中的 steps

    steps Specifies a stepping function, described above, taking two parameters. The first parameter spe ...

  2. Unity如何播放带有alpha 通道的视频

    问题: 当使用Video Player播放带有alpha 通道的视频时带有黑色背景 解决方式: 使用文件格式为WEBM的视频,对视频文件进行的修改 在RawImage中,将New Render Tex ...

  3. Labview学习笔记(三)

    一.数据 1.数值控件 (1)数值控件 根据不同的模拟状态,放置不同控件 (2)显示格式 为了程序显示,需要设置数值型控件的表示法.数值范围.显示格式等属性. 一般来说,长度越长,则可以表示的数值范围 ...

  4. 20180429NOIP提高组精英班Day1测试

  5. SOUI界面库 添加 windows系统文件图标皮肤

    最近在学习soui界面库.其中有用到SListCtrl这个控件来现在文件信息.控件用法基本上和mfc 的CListCtrl差不多.也支持图标显示.但是图标是要自己加入图标图片的.这个就有点不好弄.于是 ...

  6. VS单元测试"未能加载文件或程序集,或它的某一个依赖项"

    Autofac.Core.DependencyResolutionException : An error occurred during the activation of a particular ...

  7. 9.3.4 BeaufitulSoup4

    BeautifulSoup 是一个非常优秀的Python扩展库,可以用来从HTML或XML文件中提取我们感兴趣的数据,并且允许指定使用不同的解析器. 使用 pip install BeaufifulS ...

  8. Python网络请求urllib和urllib3详解

    Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...

  9. How do I drop a MongoDB database, from the command line?

    mongo <dbname> --eval "db.dropDatabase()" > use mydb; > db.dropDatabase(); mon ...

  10. gh-ost 号称是不需要触发器(Triggerless)支持的在线更改表结构的工具

    https://segmentfault.com/a/1190000006158503?utm_source=tuicool&utm_medium=referral