POJ 2553
来源:互联网 发布:淘宝拍摄产品多少钱 编辑:程序博客网 时间:2024/06/05 00:22
求强连同分量的经典题。 if for every node w in G that is reachable from v, v is also reachable from w. 是说在v可以到达的所有点也都可以到达v,由此就可以知道求解缩点以后出度为0的点中的节点数字即可。1<=v<=5000,最好用链表。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N=10002;
const int M=50002;
typedef struct node
{
int v;
int next;
};
node edge[M];
int head[N];
int n,m;
int scc;//强连通分量
int index;//每个节点的访问次序编号 时间戳
int cnt;//边的数量
int dfn[N];//标记结点i的时间戳
int low[N];//记录节点u或u的子树中的所有节点的最小标号
int belong[N];//属于哪个分支
bool instack[N];//是否在栈中
int num[N];//记录一个强连通分量中结点的个数
int sstack[N];
int top;
int out[N];//出度
void addedge(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int MIN(int a,int b)
{
if(a<b)
return a;
return b;
}
void tarjan(int u)
{
dfn[u]=low[u]=++index;
sstack[++top]=u;
instack[u]=true;
for (int j=head[u];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(dfn[v]==0)//未曾访问过
{
tarjan(v);
low[u]=MIN(low[u],low[v]);
}
else if(instack[v])
{
low[u]=MIN(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
scc++;
while(1)
{
int tmp=sstack[top--];
instack[tmp]=0;
belong[tmp]=scc;
num[scc]++;
if(tmp==u)
break;
}
}
}
void solve()
{
scc=top=index=0;
int outnum;
memset(num,0,sizeof(num));
memset(dfn,0,sizeof(dfn));
for (int i=1;i<=n;i++)
{
if (!dfn[i])
tarjan(i);
}
memset(out,0,sizeof(out));
for (int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=edge[j].next)
{
int u=belong[i];
int v=belong[edge[j].v];
if(u!=v)
{
out[u]=1;
}
}
}
outnum=0;
int aug;
for(int i=1;i<=n;i++)
{
if(!out[belong[i]])
{
printf("%d ",i);
}
}
printf("\n");
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
cnt=0;
if(n==0)
break;
memset(head,-1,sizeof(head));
for (int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
}
solve();
}
}
- POJ 2553
- poj 2553
- POJ 2553
- POJ 2553
- poj 2553
- POJ 2553
- poj 2553 tarjan算法
- POJ 2553-题意很重要...
- poj 2553 tarjan
- poj 2553 强连通
- poj 2553 强连通
- poj 2553
- POJ 2553&&ZOJ 1979
- poj 2553 Tarjan
- poj-2553 Frogger
- POJ 2553 Tarjan
- POJ
- poj
- Visual Studio控制台程序输出窗口一闪而过的解决方法
- 电脑、手机日常使用经验
- ubuntu 找回系统托盘的输入法图标
- 使用SharedPreferences处理数据的 新建 储存 读取 删除
- java 实现日历选择器
- POJ 2553
- POJ 2752
- 消除闪屏的双缓冲技术在AWT和Swing中的实现
- 获取JPasswordField组件中的密码
- Eclipse快捷键 10个最有用的快捷键
- POJ 3036
- web harvest 之初体验
- POJ 3522
- Ubuntu 12.10 程序图标放到启动器上