poj 2553 The Bottom of a Graph
- 求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数
如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求
// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点个数- // 如果某个新图的节点出度数为0 且是缩点而来 那么该强连通分量里面的点都是符合要求的
#include <iostream>- #include <algorithm>
- #include <queue>
- #include <stack>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- #define MOD 1000000007
- #define maxn 60100
- #define maxm 10010
- struct Edge{
- int to;
- int next;
- Edge(){};
- Edge(int u,int v){to=u;next=v;}
- }E[maxn];
- stack<int> S;
- int V[maxm],num;
- int belong[maxm];
- int pre[maxm];
- int dfst,scc;
- int ans;
- bool tag[maxm];
- int out[maxm];
- void init(int n){
- dfst=scc=;
- num=;
- ans=;
- while(!S.empty())
- S.pop();
- for(int i=;i<=n;i++){
- V[i]=-;
- pre[i]=;
- belong[i]=;
- }
- }
- void add(int u,int v){
- E[num].to=v;
- E[num].next=V[u];
- V[u]=num++;
- }
- int tarjan(int u){
- int lowu=pre[u]=++dfst;
- int v,e;
- S.push(u);
- for(e=V[u];e!=-;e=E[e].next){
- v=E[e].to;
- if(!pre[v]){
- int lowv=tarjan(v);
- lowu=min(lowu,lowv);
- }
- else if(!belong[v]) lowu=min(lowu,pre[v]);
- }
- if(lowu==pre[u]){
- scc++;
- for(;;){
- int x=S.top();S.pop();
- belong[x]=scc;
- if(x==u) break;
- }
- }
- return lowu;
- }
- int main()
- {
- int n,m,T;
- int u,v;
- int i,j=;
- //scanf("%d",&T);
- while(scanf("%d",&n),n){
- scanf("%d",&m);
- init(n);
- for(i=;i<=m;i++){
- scanf("%d %d",&u,&v);
- add(u,v);
- }
- for(i=;i<=n;i++)
- if(!pre[i]) tarjan(i);
- // for(i=1;i<=n;i++) printf("%d ",belong[i]);
- for(i=;i<=scc;i++) out[i]=,tag[i]=;
- int e,u,v;
- for(i=;i<=n;i++)
- {
- for(e=V[i];e!=-;e=E[e].next){
- u=belong[i];
- v=belong[E[e].to];
- if(u!=v)
- out[u]++;//,printf("v=%d ",v);
- }
- }
- int flag=,rc;
- for(i=;i<=scc;i++) if(!out[i]) tag[i]=,flag=;
- if(!flag){printf("\n\n");continue;}
- flag=;
- for(i=;i<=n;i++)
- if(tag[belong[i]]) {
- if(flag)printf(" %d",i);
- else {flag=;printf("%d",i);}
- }
- printf("\n");
- }
- return ;
- }
poj 2553 The Bottom of a Graph的更多相关文章
- 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(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- 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【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
随机推荐
- 2013 Asia Chengdu Regional Contest
hdu 4786 Fibonacci Tree http://acm.hdu.edu.cn/showproblem.php?pid=4786 copyright@ts 算法源于ts,用最小生成树可以求 ...
- AnkhSVN 安装
为 visual Studio 2013 添加 AnkhSVN 步骤 到 https://ankhsvn.open.collab.net/downloads 下载 2.6x或以上 安装 AnkhSvn ...
- Building Plugins for iOS
This page describes Native Code Plugins for the iOS platform. Building an Application with a Native ...
- unity3d旋转摄像机脚本
void Update () { )) { if (axes == RotationAxes.MouseXAndY) { // Read the mouse input axis rotationX ...
- mysql数据库主外键级联删除脚本RESTRICT --> CASCADE
在项目中,我们一般在数据库设计的时候做主外键关联设计,要么就不做.但是这样不符合规范,呵呵. 建立主外键关系的时候,默认是不能级联删除的.而出现往往在删除主表的数据时报错, 需要先删除从表然后再删除主 ...
- #define x do{......} while(0)的用处
比如定义宏,#define FREE1(p) if (p) free (p)然后这样调用:if (expression)FREE1(p);elseprintf(“expression was fals ...
- Python Snippet
python按行读取文件,如何去掉换行符"\n" for line in file.readlines(): line=line.strip('\n') python没有subst ...
- 【PHP高效搜索专题(2)】sphinx&coreseek在PHP程序中的应用实例
PHP可以通过三种途径来调用sphinx 通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本) 通过安装SphinxSE,然后创建一个中介sphinxSE类型的表,再通 ...
- mysql外键级联更新删除
MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定在删除.更新父表时,对子表进行的相应操作 ...
- python 编码问题(二)
>>> a = '中文' >>> chardet.detect(a) {'confidence': 0.7525, 'encoding': 'utf-8'} > ...