POJ 2553 The Bottom of a Graph(强连通分量)
http://poj.org/problem?id=2553
题意:给你一个有向图,要你输出图的bottom点.bottom点是如下的点v:如果点v能到达u点,那么u点也要能到达v点.
分析:
强连通分量题的老套路:求分量,缩点图,输出DAG的相关信息.
该题的答案就是那些DAG中出度为0的点 所代表的分量中包含的所有点.
下面简单分析一下(其实很简单,想想就能明白):如果只有一个强连通分量,那么该分量的所有点都是bottom. 如果该分量没有出边到其他任何分量的话,该分量的所有点依然都是bottom. 如果该分量有出边到另一个分量,那么该分量的所有点都不是bottom.以上3种情况自己分析一下.
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int maxn=5000+10;
int n,m;
vector<int> G[maxn];
stack<int> S;
int dfs_clock, scc_cnt;
int pre[maxn],low[maxn],sccno[maxn];
int num[maxn];//记录每个分量的节点数
bool out0[maxn];//标记该分量是否出度为0
void dfs(int u)
{
pre[u]=low[u]=++dfs_clock;
S.push(u);
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v])
low[u]=min(low[u],pre[v]);
}
if(low[u]==pre[u])
{
scc_cnt++;
num[scc_cnt]=0;
while(true)
{
int x=S.top(); S.pop();
sccno[x]=scc_cnt;
num[scc_cnt]++;
if(x==u) break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=0;
memset(pre,0,sizeof(pre));
memset(sccno,0,sizeof(sccno));
for(int i=1;i<=n;i++)
if(!pre[i]) dfs(i);
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
scanf("%d",&m);
for(int i=1;i<=n;i++) G[i].clear();
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
find_scc(n);
for(int i=1;i<=scc_cnt;i++) out0[i]=true;
for(int u=1;u<=n;u++)
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
int x=sccno[u], y=sccno[v];
if(x!=y) out0[x]=false;
}
bool first=true;
for(int i=1;i<=n;i++)
{
if(out0[sccno[i]])
{
if(first) printf("%d",i), first=false;
else printf(" %d",i);
}
}
puts("");
}
return 0;
}
分享到:
相关推荐
POJ2186-Popular Cows ...【Tarjan+极大强连通分量+缩点】 解题报告+AC代码 http://hi.csdn.net/!s/BGDH68 附:我所有的POJ解题报告链接 . http://blog.csdn.net/lyy289065406/article/details/6642573
北大POJ2151-Check the difficulty of problems 解题报告+AC代码
北大POJ3083-Children of the Candy Corn 解题报告+AC代码
poj 1611 The Suspects 代码 并查集的应用
POJ2942-Knights of the Round Table 【Tarjan算法】 解题报告+AC代码 http://hi.csdn.net/!s/F3L8HO ================================== 我的POJ所有解题报告:...
北大POJ1027-The Same Game 解题报告+AC代码
北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码
北大POJ1163-The Triangle 解题报告+AC代码
北大POJ2109-Power of Cryptography 解题报告+AC代码
poj 3174 Alignment of the Planets.md
北大POJ1163-The Triangle
北大POJ1942-Paths on a Grid 解题报告+AC代码
poj 1611 The Suspects.md
POJ---1456.Supermarket测试数据及答案,题目描述:A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral ...
北大POJ2739-Sum of Consecutive Prime Numbers 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5...
poj 3191 The Moronic Cowmpouter.md
poj 1989 The Cow Lineup.md
poj 3260 The Fewest Coins.md