思路:将所有强连通分支找出来,并进行缩点,然后找其中所有出度为0的连通分支,就是题目要求的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define Maxn 5100
#define Maxm Maxn*100
#define inf 0x7fffffff
using namespace std;
int index[Maxn],vi[Maxn],stack[Maxn],dfn[Maxn],low[Maxn],e,n,lab,top,num,list,mark[Maxn],degree[Maxn],be[Maxn];
struct Edge{
int from,to,next;
}edge[Maxm];
void addedge(int from,int to)
{
edge[e].from=from;
edge[e].to=to;
edge[e].next=index[from];
index[from]=e++;
}
void init()
{
memset(index,-,sizeof(index));
memset(degree,,sizeof(degree));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vi,,sizeof(vi));
memset(mark,,sizeof(mark));
memset(be,,sizeof(be));
e=lab=top=num=list=;
}
void Out(int u)//将该连通分量的点进行统计
{
int i,j;
list++;
do{
i=stack[--top];
be[i]=list;
vi[i]=;
mark[i]=;
}
while(i!=u);
}
int dfs(int u)
{
dfn[u]=low[u]=++lab;
stack[top++]=u;
vi[u]=;
int i,j,temp;
for(i=index[u];i!=-;i=edge[i].next)
{
temp=edge[i].to;
if(!dfn[temp])
{
dfs(temp);
low[u]=min(low[u],low[temp]);
}
if(vi[temp])
low[u]=min(low[u],dfn[temp]);
}
if(low[u]==dfn[u])//找到连通分量
Out(u);
return ;
}
int solve()
{
int i,j,temp;
for(i=;i<=n;i++)
if(!dfn[i])
dfs(i);
memset(vi,,sizeof(vi));
for(i=;i<=n;i++)
{
for(j=index[i];j!=-;j=edge[j].next)
{
temp=edge[j].to;
if(be[i]!=be[temp])
{
vi[be[i]]=;
}
}
}
for(i=;i<=n;i++)
{
if(mark[i])
{
if(vi[be[i]])
{
printf("%d",i);
break;
}
}
}
for(i++;i<=n;i++)
{
if(mark[i])
{
if(vi[be[i]])
printf(" %d",i);
}
}
printf("\n");
return ;
}
int main()
{
int m,i,j,a,b;
while(scanf("%d",&n)!=EOF,n)
{
scanf("%d",&m);
init();
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
addedge(a,b);
}
solve();
}
return ;
}

poj 2553 强连通分支与缩点的更多相关文章

  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 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...

  3. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  4. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  5. poj 2553 缩点+染色+出度

    题目链接:https://vjudge.net/problem/POJ-2553 如果不会tarjan算法,推荐博客:https://blog.csdn.net/mengxiang000000/art ...

  6. poj 2553 The Bottom of a Graph(强连通、缩点、出入度)

    题意:给出一个有向图G,寻找所有的sink点.“sink”的定义为:{v∈V|∀w∈V:(v→w)⇒(w→v)},对于一个点v,所有能到达的所有节点w,都能够回到v,这样的点v称为sink. 分析:由 ...

  7. poj 2553强连通+缩点

    /*先吐槽下,刚开始没看懂题,以为只能是一个连通图0T0 题意:给你一个有向图,求G图中从v可达的所有点w,也都可以达到v,这样的v称为sink.求这样的v. 解;求强连通+缩点.求所有出度为0的点即 ...

  8. POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  9. poj 2186 强连通分支 和 spfa

    思路: 建图时,分别建正向图edge和转置图T.用正向图edge来DFS,找出第一个被发现的强连通分支(如果该图存在题目要求的点,那么一定就是第一个被发现的).然后用spfa跑转置图T,判断被发现的点 ...

随机推荐

  1. Xcode 4 插件制作入门

    转自:http://www.onevcat.com/2013/02/xcode-plugin/ 2014.5.4更新 对于 Xcode 5,本文有些地方显得过时了.Xcode 5 现在已经全面转向了 ...

  2. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  3. GridControl表头全选操作实现之最优方法

    突然发现很久没有写博客了. 昨天整了个Windows Live Writer 就为了以后好好写写博客. 所以,开始咯. 为了积累,也为了分享. 之前在博客园中看到一篇文章:<Winform分页控 ...

  4. Laravel Configuration

    Introduction All of the configuration files for the Laravel framework are stored in the app/config d ...

  5. codeforces 630R Game

    R. Game time limit per test 0.5 seconds memory limit per test 64 megabytes input standard input outp ...

  6. threading模块

    threading — Higher-level threading interface¶ Source code: Lib/threading.py This module constructs h ...

  7. Caused by: Cannot locate the chosen ObjectFactory implementation: spring - [unknown location] 的解决方式

    1.添加网上所说的struts2 plugin jar包 2. <!-- Struts2配置 --> <filter> <filter-name>struts2&l ...

  8. MySQL BINARY和BIT类型

    BINARY和VARBINARY类型 BINARY和VARBIANRY类型同CHAR和VARCHAR类型相似,除了BIANARY和VARBINARY类型只包含二进制字符串,即它们只包含byte串而非字 ...

  9. VC中监测函数运行时间

    VC++编程时,经常会监控某个算法的计算时间,以确定算法的效率.编码举例如下, //========start: algorithm time============= // SYSTEMTIME s ...

  10. 自己写一个jQuery垂直滚动栏插件(panel)

    html中原生的滚动栏比較难看,所以有些站点,会自己实现滚动栏,导航站点hao123在一个側栏中,就自己定义了垂直滚动栏,效果比較好看,截图例如以下: watermark/2/text/aHR0cDo ...