POJ 2553 Tarjan
题意:如果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的更多相关文章
- POJ - 2553 tarjan算法+缩点
题意: 给你n个点,和m条单向边,问你有多少点满足(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}关系,并把这些点输出(要注意的是这个关系中是蕴含关系而不是且(&&)关系) 题解: ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
- POJ 2553 The Bottom of a Graph (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph 【scc tarjan】
图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
随机推荐
- GFS分布式文件系统脚本
#!/bin/bashfor i in $(fdisk -l | grep -wo "/dev/sd[b-z]" | sort)dodd if=/dev/zero of=$i bs ...
- sysbench使用指南
sysbench 安装.使用和测试 摘要: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据库的性能测试.目前支持的数据库有MySQ ...
- How To:使用dmidecode获取机器序列号(Serial number)
使用dmidecode可以获取关于机器的诸多信息,比如机器的序列号 [root@dc1db01 ~]# dmidecode -s system-serial-number processor的 ...
- R 安装car包失败
在RStudio里安装car包的时候报错 /usr/bin/ld: cannot find -llapack /usr/bin/ld: cannot find -lblas make: *** [qu ...
- Problem 56
Problem 56 https://projecteuler.net/problem=56 Powerful digit sum A googol (10100) is a massive numb ...
- sql server的 between and 日期问题
- yum http源
统一集群内,有一个yum本地源即可,集群内其他机器的yum可配成http源 方法: yum1:本地yum源节点 yum2:集群内其他节点 yum1: ①service httpd start ②mki ...
- Object Detection: To Be Higher Accuracy and Faster
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51597496 在深度学习中有一类研究热 ...
- Master Nginx(6) - The Nginx HTTP Server
Nginx's architecture The HTTP core module The server Logging Finding files Name resolution Client in ...
- PatentTips – RDMA data transfer in a virtual environment
BACKGROUND Embodiments of this invention relate to RDMA (remote direct memory access) data transfer ...