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

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

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

原题请戳这里

  1. #include<queue>
  2. #include<stack>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<algorithm>
  7. using namespace std;
  8. int dfn[5005],low[5005],p[5005],out[5005],n,m,t,cnt;
  9. bool vis[5005];
  10. vector<short>v[5005];
  11. stack<short>stk;
  12. priority_queue<int,vector<int>,greater<int> >pq;
  13. void tarjan(int x)
  14. {
  15. vis[x]=1,stk.push(x),low[x]=dfn[x]=cnt++;
  16. for(int i=0;i<v[x].size();i++)
  17. if(!dfn[v[x][i]])
  18. tarjan(v[x][i]),low[x]=min(low[x],low[v[x][i]]);
  19. else if(vis[v[x][i]])
  20. low[x]=min(low[x],dfn[v[x][i]]);
  21. if(low[x]==dfn[x]){
  22. int y;t++;
  23. do y=stk.top(),stk.pop(),vis[y]=0,p[y]=t;while(y!=x);
  24. }
  25. }
  26. void find(int x){for(int i=1;i<=cnt;i++)if(p[i]==x)pq.push(i);}
  27. int main()
  28. {
  29. register int xx,yy;
  30. while(scanf("%d",&n)&&n)
  31. {
  32. memset(out,0,sizeof(out));
  33. memset(dfn,0,sizeof(dfn));
  34. memset(vis,0,sizeof(vis));
  35. memset(p,0,sizeof(p));
  36. for(int i=1;i<=n;i++)
  37. v[i].clear();
  38. cnt=t=0;
  39. scanf("%d",&m);
  40. for(int i=1;i<=m;i++)
  41. scanf("%d%d",&xx,&yy),v[xx].push_back(yy);
  42. for(int i=1;i<=n;i++)
  43. if(!dfn[i])tarjan(i);
  44. for(int i=1;i<=n;i++)
  45. for(int j=0;j<v[i].size();j++)
  46. if(p[i]!=p[v[i][j]]) out[p[i]]++;
  47. for(int i=1;i<=t;i++)
  48. if(out[i]==0) find(i);
  49. while(!pq.empty())
  50. printf("%d ",pq.top()),pq.pop();
  51. printf("\n");
  52. }
  53. }

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. EF-Lamdba

    一丶基本语法 var userList=db.set<table>().where(c=>c.id=="001"&&c.userName.Cont ...

  2. django访问静态变量的设置

    在项目的urls.py文件中 默认urlpatterns是空的列表需要填入url匹配的路由,默认使用static from django.conf.urls import include, url f ...

  3. ListView学习(一)

    最近了解了LIstView的用法,在听了孙老师的课程后,终于对Adapter有了一定的理解,特作此文记之. ListView在App当中应用相当广泛,比如QQ好友列表.微博等等,都是某种特定的列表,所 ...

  4. DSP广告系统架构及关键技术解析(转)

    广告和网络游戏是互联网企业主要的盈利模式 广告是广告主通过媒体以尽可能低成本的方式与用户达成接触的商业行为.也就是说按照某种市场意图接触相应人群,影响其中潜在用户,使其选择广告主产品的几率增加,或对广 ...

  5. [bzoj2654]tree_二分_kruskal

    tree bzoj-2654 题目大意:给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树.题目保证有解. 注释:$1\le V\le 5\cdot 10^4 ...

  6. window7 查找与杀掉占用端口的进程

    1.netstat -ano | findstr 3000 2.tasklist | findstr pid 3. taskkill -f -t -im 进程名

  7. cogs 29. 公路建设

    29. 公路建设 ★   输入文件:road.in   输出文件:road.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] A 国是一个新兴的国家,有 N 个城市,分别 ...

  8. PHP array_diff_ukey()

    定义和用法 array_diff_ukey() 返回一个数组,该数组包括了所有出现在 array1 中但是未出现在任何其它参数数组中的键名的值.注意关联关系保留不变.与 array_diff() 不同 ...

  9. saprk里面的action - aggregate

    上一篇讲到了spark里面的action函数: Action列表: reduce collect count first take takeSample takeOrdered saveAsTextF ...

  10. ACdream区域赛指导赛之专题赛系列(1)の数学专场

    Contest : ACdream区域赛指导赛之专题赛系列(1)の数学专场 A:EOF女神的相反数 题意:n(<=10^18)的数转化成2进制.翻转后(去掉前导零)输出十进制 思路:water ...