题意:如果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. .net 程序集加载,版本不匹配的解决方法

    经常有些时候,A.dll引用的是Microsoft.EntityFrameworkCore.dll version=1.0.0.0 publicKeyToken="adb9793829dda ...

  2. CAD从线型文件加载线型记录(com接口)

    主要用到函数说明: _DMxDrawX::LoadLinetypeFromFile 从线型文件加载线型记录,详细说明如下: 参数 说明 BSTR pszLinetypeFile 线型文件名,支持htt ...

  3. tp定时任务,传参问题

    <?phpnamespace app\command; use think\console\Command;use think\console\Input;use think\console\i ...

  4. linux环境图数据库neo4j安装

    自定义yum源 Neo4j Stable Yum Repo First, you'll want our key: cd /tmp wget http://debian.neo4j.org/neote ...

  5. 8.mysql执行语句的顺序

    mysql执行语句的顺序     一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...

  6. Codeforces 918C/917A - The Monster

    传送门:http://codeforces.com/contest/918/problem/C 一个括弧串由字符‘(’和‘)’组成.一个正确的串可被递归地定义,定义如下: ①空串e是一个正确的串: ② ...

  7. GeoTrust 企业(OV)型 通配符(Wildcard) SSL证书

      GeoTrust 企业(OV)型 通配符(Wildcard)SSL证书(GeoTrust True BusinessID Wildcard SSL Certificates),支持通配符(Wild ...

  8. Sencha Touch 2中如何动态添加button

    原理很简单无非就是在一个容器上面选中id,用容器的add方法将button循环加入. 现在我们来试一下 1.先定义一个Container组件用,以后在里面添加button ? 1 2 xtype:'c ...

  9. Java端百度云推送消息Demo

    因为在做Java服务器有用到推送消息机制,于是到网上找了一下,就自己试着敲了一个demo.这个demo主要是简单的一个对app消息推送. jar:百度云消息推送Java端的jar. package x ...

  10. Spring MVC-集成(Integration)-生成XML示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_xml.htm 说明:示例基于Spring MVC 4.1.6. 以下示例说明如何 ...