题解 SP1799 【BOTTOM - The Bottom of a Graph】
裸的
本题重点:找出
link点:一个点
其实不难知道单独一个强连通分量中的所有点是满足题目要求的。
那我们要找到这个一组强连通分量,它必为出度为
因为出度不为
所以这道题其实就是求出度为
总结:
我们需要先建立图,然后跑
参考程序:
#include<bits/stdc++.h>
using namespace std;
const int N=5000+10;
struct data
{
int from,to, next;
}edge[N*N>>1];
int n,m;
int cnt,col,top;
int dfn[N],low[N],st[N],color[N],col_s[N];
int cd[N];
int head[N],tot;
void Add_edge(int x,int y)//链式前向星
{
edge[++tot].from=x;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++cnt;
st[++top]=u;
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!color[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
color[u]=++col;
while(st[top]!=u)
color[st[top--]]=col;
top--;
}
}
int main()
{
while(cin>>n)
{
if(n==0)break;
cin>>m;
memset(dfn,0,sizeof(dfn));
memset(color,0,sizeof(color));
memset(low,0,sizeof(low));
memset(head,0,sizeof(head));
memset(cd,0,sizeof(cd));
tot=col=cnt=0;//不要忘记了初始化
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
Add_edge(x,y);
}
for(int i=1;i<=n;i++)
if(!dfn[i])Tarjan(i);
for(int i=1;i<=tot;i++)
{
int u=edge[i].from,v=edge[i].to;
if(color[u]!=color[v])
cd[color[u]]++;//找到连接一个分量与另一个分量的边,即出度
}
int t=0;
for(int i=1;i<=n;i++)
if(!cd[color[i]])//出度为0
cout<<i<<" ";
cout<<endl;
}
}