题目链接
http://poj.org/problem?id=2553
题目
We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,…,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,…,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.
Input
The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,…,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,…,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.
Output
For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.
Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
题意
给定一个有向图,升序输出所有的汇点。
本题中汇点的定义:被有直接或间接指向关系的其他点指向的点。
分析
求出所有强连通分量,对分量缩点,求出每一个缩点的出度。出度为0的缩点中的点都是汇点。按要求升序输出即可。
和POJ2186差不多,POJ2186要求出度为0的缩点最多1个,这题允许多个。
AC代码
//141ms 1.7MB
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
typedef long long ll;
using namespace std;
const int maxn=2e4+100;
vector<int> g[maxn],G[maxn];
stack<int> sta;
int n,m,cnt,dep;
int low[maxn],dfn[maxn],insta[maxn];
int par[maxn];
int sz[maxn];//sz[i]表示强连通分量i内含结点数
void tarjan(int u)//dfs确定时间戳,根据时间戳判断强连通分量
{
low[u]=dfn[u]=++dep;//打时间戳
sta.push(u);//入栈
insta[u]=1;//标记在栈中
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!dfn[v])//未访问过
{
tarjan(v);//dfs
low[u]=min(low[u],low[v]);//更新low
}
else if(insta[v])//访问过且还在栈里面
{
//注意必须还在栈里
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])//遍历完整个深搜树后u的dfn值等于low值,则它是该深搜子树的根
{
//从栈顶到u的结点组成一个强连通分量
++cnt;//强连通分量数
while(!sta.empty())
{
sz[cnt]++;
int tmp=sta.top();sta.pop();
insta[tmp]=0;
par[tmp]=cnt;
if(tmp==u) break;
}
}
}
void init()
{
cnt=0;//强连通分量编号
dep=0;//结点深度
memset(sz,0,sizeof(sz));
memset(insta,0,sizeof(insta));
for(int i=0;i<=n;i++) par[i]=i;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
for(int i=0;i<=n;i++) g[i].clear();
}
int in[maxn],out[maxn],vis[maxn],ans[maxn];
int main()
{
while(~scanf("%d",&n))
{
if(n==0) break;
scanf("%d",&m);
init();
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=1;i<=n;i++)//确定缩点的入度和出度
for(int j=0;j<g[i].size();j++)
{
int u=i;
int v=g[i][j];
if(par[u]==par[v]) continue;
in[par[v]]++;
out[par[u]]++;
}
int sum_out=0,tot=0;
for(int i=1;i<=cnt;i++)
{
if(!out[i])//出度为0是答案
{
ans[++tot]=i;
}
}
memset(vis,0,sizeof(vis));
int sum=0;
for(int i=1;i<=tot;i++)
for(int j=1;j<=n;j++)
if(par[j]==ans[i]) vis[j]=1,sum++;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
sum--;
printf("%d%s",i,sum==0?"\n":" ");
}
}
}
return 0;
}