poj 2553 强连通
题意:给出一个有向图,定义:若节点v所有能到达的点{wi},都能反过来到达v,那么称节点v是sink。题目要求所有的sink点。
思路:强连通缩点找出出度为零的点,输出即可。
这题主要问题是读题,了解题意之后就好做了,然后在数组开小了导致WA?挺莫名其妙的。。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std; #define MAXN 10500
#define MAXM 20000000 struct Edge
{
int v, next;
}edge[MAXM]; //边结点数组 int first[MAXN], stack[MAXN], DFN[MAXN], Low[MAXN], Belong[MAXN];
int indegree[MAXN],outdegree[MAXN];
// first[]头结点数组,stack[]为栈,DFN[]为深搜次序数组,Belong[]为每个结点所对应的强连通分量标号数组
// Low[u]为u结点或者u的子树结点所能追溯到的最早栈中结点的次序号
int instack[MAXN]; // instack[]为是否在栈中的标记数组
int n, m, cnt, scnt, top, tot; void init()
{
cnt = 0;
scnt = top = tot = 0; //初始化连通分量标号,次序计数器,栈顶指针为0
for(int i=0;i<=n+100;i++)
{
first[i]=-1;
outdegree[i]=0;
DFN[i]=0;
}
} void read_graph(int u, int v) //构建邻接表
{
edge[tot].v = v;
edge[tot].next = first[u];
first[u] = tot++;
}
void Tarjan(int v) //Tarjan算法求有向图的强连通分量
{
int min, t;
DFN[v] = Low[v] = ++tot; //cnt为时间戳
instack[v] = 1; //标记在栈中
stack[top++] = v; //入栈
for(int e = first[v]; e != -1; e = edge[e].next)
{ //枚举v的每一条边
int j = edge[e].v; //v所邻接的边
if(!DFN[j])
{ //未被访问
Tarjan(j); //继续向下找
if(Low[v] > Low[j]) Low[v] = Low[j]; // 更新结点v所能到达的最小次数层
}
else if(instack[j] && DFN[j] < Low[v])
{ //如果j结点在栈内,
Low[v] = DFN[j];
}
}
if(DFN[v] == Low[v])
{ //如果节点v是强连通分量的根
scnt++; //连通分量标号加1
do
{
t = stack[--top]; //退栈
instack[t] = 0; //标记不在栈中
Belong[t] = scnt; //出栈结点t属于cnt标号的强连通分量
}while(t != v); //直到将v从栈中退出
}
} void solve()
{
for(int i = 1; i <= n; i++) //枚举每个结点,搜索连通分量
if(!DFN[i]) //未被访问
Tarjan(i); //则找i结点的连通分量
}
int e1[MAXN];int e2[MAXN];
int main()
{
while(scanf("%d",&n),n)
{
init();
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int v,w;
scanf("%d%d",&v,&w);
e1[tot]=v;
e2[tot]=w;
read_graph(v, w);
}
int num=tot;
solve(); //求强连通分量
for(int i=0;i<num;i++)
{
if(Belong[e1[i]]!=Belong[e2[i]])
outdegree[Belong[e1[i]]]++;
}
bool ff=false;
for(int i=1;i<=n;i++)
{
if(!outdegree[Belong[i]])
{
if(ff==false)
{
printf("%d",i);
ff=true;
}
else
{
printf(" %d",i);
}
}
}
printf("\n");
}
return 0;
}
poj 2553 强连通的更多相关文章
- poj 2553强连通+缩点
/*先吐槽下,刚开始没看懂题,以为只能是一个连通图0T0 题意:给你一个有向图,求G图中从v可达的所有点w,也都可以达到v,这样的v称为sink.求这样的v. 解;求强连通+缩点.求所有出度为0的点即 ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- poj 2186 强连通分量
poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- poj 2553 The Bottom of a Graph(强连通、缩点、出入度)
题意:给出一个有向图G,寻找所有的sink点.“sink”的定义为:{v∈V|∀w∈V:(v→w)⇒(w→v)},对于一个点v,所有能到达的所有节点w,都能够回到v,这样的点v称为sink. 分析:由 ...
- POJ 2553 The Bottom of a Graph(强连通分量的出度)
题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...
随机推荐
- 阿里消息队列中间件 RocketMQ源码解析:Message发送&接收
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- seajs的模块化开发--实践笔记
2017-04-02 SeaJS是一个遵循CMD规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制.有效的解决复杂项目中命名冲突.依赖.性能等问题. SeaJS ...
- 成为Java顶尖程序员 ,看这11本书就够了(转)
学习的最好途径就是看书",这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的 ...
- java大数取余
java大数取余: 类方法:BigInteger.divideAndRemainder() 返回一个数组,key = 0为商key = 1为余数 import java.util.*; import ...
- Python模块----linecache
Python标准库提供了一个有趣的模块:linecache模块.该模块用来从文件中读取任何的行,并且将这些lines使用缓存进行优化,常见的情况是从个大文件中读取指定的行.不过由于此模块使用内存进行缓 ...
- umask的作用
--umask的作用---------------2013/11/15 umask的作用就是当用户创建一个文件时,设置默认的目录和文件权限. 创建非目录文件时,用666减umask值(目录文件用777 ...
- Linux/Unix监控其他用户和信号
--Linux/Unix监控其他用户和信号 ------------------------------------------------------2013/10/27 查看有哪些用户登录 w ...