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 ...
随机推荐
- EF-Lamdba
一丶基本语法 var userList=db.set<table>().where(c=>c.id=="001"&&c.userName.Cont ...
- django访问静态变量的设置
在项目的urls.py文件中 默认urlpatterns是空的列表需要填入url匹配的路由,默认使用static from django.conf.urls import include, url f ...
- ListView学习(一)
最近了解了LIstView的用法,在听了孙老师的课程后,终于对Adapter有了一定的理解,特作此文记之. ListView在App当中应用相当广泛,比如QQ好友列表.微博等等,都是某种特定的列表,所 ...
- DSP广告系统架构及关键技术解析(转)
广告和网络游戏是互联网企业主要的盈利模式 广告是广告主通过媒体以尽可能低成本的方式与用户达成接触的商业行为.也就是说按照某种市场意图接触相应人群,影响其中潜在用户,使其选择广告主产品的几率增加,或对广 ...
- [bzoj2654]tree_二分_kruskal
tree bzoj-2654 题目大意:给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树.题目保证有解. 注释:$1\le V\le 5\cdot 10^4 ...
- window7 查找与杀掉占用端口的进程
1.netstat -ano | findstr 3000 2.tasklist | findstr pid 3. taskkill -f -t -im 进程名
- cogs 29. 公路建设
29. 公路建设 ★ 输入文件:road.in 输出文件:road.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] A 国是一个新兴的国家,有 N 个城市,分别 ...
- PHP array_diff_ukey()
定义和用法 array_diff_ukey() 返回一个数组,该数组包括了所有出现在 array1 中但是未出现在任何其它参数数组中的键名的值.注意关联关系保留不变.与 array_diff() 不同 ...
- saprk里面的action - aggregate
上一篇讲到了spark里面的action函数: Action列表: reduce collect count first take takeSample takeOrdered saveAsTextF ...
- ACdream区域赛指导赛之专题赛系列(1)の数学专场
Contest : ACdream区域赛指导赛之专题赛系列(1)の数学专场 A:EOF女神的相反数 题意:n(<=10^18)的数转化成2进制.翻转后(去掉前导零)输出十进制 思路:water ...