poj2553 求汇点
来源:互联网 发布:视频一对一聊天软件 编辑:程序博客网 时间:2024/06/07 00:37
题目大意:如果v点能够到的点,反过来能够到达v点,则称这个点为sink点,输出所有的sink点
其实就是求联通分量罢了。
想要互相到达,就是说明v这点所在的联通分量没有出度,如果有的话,v点一定可以顺着那条出度的边指向某个点G,但是G是没法到达v的。如果可以到达,那么又构成联通了,G会归与这个联通图的。所以说,找所有出度为0的连通图所包含的点即可
注意点1:排序输出
注意点2:不要用数组存图,内存会超
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
const int MAXN=5100;
int n;
int DFN[MAXN];
int LOW[MAXN];
int vis[MAXN];
int belong[MAXN];//belong[i]表示i属于缩点后的哪个节点
int cnt;
int out[MAXN];
int tot;
struct Edge
{
int v;
int next;
}edge[MAXN*MAXN];
int edgecount;
int head[MAXN];
void Init()
{
edgecount=0;
memset(head,-1,sizeof(head));
}
void Add_edge(int u,int v)
{
edge[++edgecount].v=v;
edge[edgecount].next=head[u];
head[u]=edgecount;
}
stack<int > St;
void Tarjan(int u)//从节点x开始搜索
{
DFN[u]=LOW[u]=++tot;
vis[u]=1;//为1表示在队列里面
St.push(u);
for(int k=head[u];k!=-1;k=edge[k].next)
{
int v=edge[k].v;
if(!DFN[v])//还未访问过
{
Tarjan(v);
LOW[u]=min(LOW[u],LOW[v]);
}
else if(vis[v])//被访问过,还在队列里
{
LOW[u]=min(LOW[u],DFN[v]);
}
}
if(LOW[u]==DFN[u])
{
int x;
++cnt;
while(1)
{
x=St.top();
St.pop();
vis[x]=0;
belong[x]=cnt;
if(x==u)break;
}
}
}
void Solve()
{
tot=0;
cnt=0;//缩点后的点数
memset(DFN,0,sizeof(DFN));
memset(LOW,0,sizeof(LOW));
memset(vis,0,sizeof(vis));
memset(out,0,sizeof(out));
while(!St.empty()) St.pop();
for(int i=1;i<=n;i++)
{
if(DFN[i]==0)Tarjan(i);
}
for(int u=1;u<=n;u++)
{
for(int k=head[u];k!=-1;k=edge[k].next)
{
int v=edge[k].v;
if(belong[u]!=belong[v])
{
out[belong[u]]++;
}
}
}
int ans1=0;
int ans2=0;
int ff=0;
int sum[MAXN];
int numm=0;
for(int i=1;i<=cnt;i++)
{
if(out[i]==0)
{
for(int j=1;j<=n;j++)
{
if(belong[j]==i)
{
sum[numm++]=j;
}
}
}
}
sort(sum,sum+numm);
cout<<sum[0];
for(int i=1;i<numm;i++)
{
cout<<" "<<sum[i];
}
cout<<endl;
}
int main()
{
int v;
while(scanf("%d",&n)!=EOF&&n)
{
Init();
int mm,aa,bb;
cin>>mm;
for(int u=1;u<=mm;u++)
{
cin>>aa>>bb;
Add_edge(aa,bb);
}
Solve();
}
return 0;
}
- poj2553 求汇点
- poj2553
- poj2553--Tarjan
- poj2553 Longest Ordered Subsequence
- POJ2553解题报告 强连通分支
- poj2553 The Bottom of a Graph
- poj2553 - The Bottom of a Graph
- POJ2553 The Bottom of a Graph
- POJ2553 The Bottom of a Graph (Tarjan)
- poj2553 有向图缩点,强连通分量。
- 强连通分量+缩点(poj2553)
- POJ2553-The Bottom of a Graph
- poj2553 The Bottom of a Graph
- poj2553 The Bottom of a Graph (Tarjan)
- poj2553——The Bottom of a Graph
- SCC::poj2186 Popupar Cows && poj2553 The Bottom of A Graph
- ZOJ1979 POJ2553 The Bottom of a Graph,经典Tarjan
- POJ2553 The Bottom of a Graph 强连通 tarjan
- android第三方框架 xlistview 的使用
- Java之线程安全的单例设计模式
- 整理了部分数据分析用图表
- [BZOJ]1601 灌水 最小生成树
- python 字符串和编码
- poj2553 求汇点
- Mybatis 使用count
- Andrew NG 机器学习 练习4-Neural Networks Learning
- java图片色阶调整、亮度调整
- fail树(bzoj 3172: [Tjoi2013]单词)
- 简单三步搭建ss服务器和ss提速
- Python3 标准库概览
- 51nod 1065 最小正子段和
- BZOJ 1798 [Ahoi2009]Seq 维护序列seq 线段树模板