The Bottom of a Graph
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 11044 | Accepted: 4542 |
Description
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+1is 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
Output

Sample Input
3 3 1 3 2 3 3 1 2 1 1 2 0
Sample Output
1 3 2
Source
定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10000
using namespace std;
bool vis[N];
int n,m,x,y,sum,tim,tot,top;
int out[N],dfn[N],low[N],ans[N],head[N],stack[N],belong[N],point[N];
inline int read()
{
,f=;char ch=getchar();
;ch=getchar();}
+ch-';ch=getchar();}
return f*x;
}
struct Edge
{
int from,to,next;
}edge[500010];
void add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
void begin()
{
tot=;top=;sum=,tim=;
memset(edge,,sizeof(edge));
memset(stack,,sizeof(stack));
memset(head,,sizeof(head));
memset(,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(belong,,sizeof(belong));
memset(ans,,sizeof(ans));
}
int tarjan(int now)
{
dfn[now]=low[now]=++tim;
stack[++top]=now;vis[now]=true;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
if(vis[t]) low[now]=min(low[now],dfn[t]);
else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
}
if(dfn[now]==low[now])
{
sum++;belong[now]=sum;
for(;stack[top]!=now;top--)
{
vis[stack[top]]=false;
belong[stack[top]]=sum;
}
vis[now]=false;top--;
}
}
void shrink_point()
{
;i<=n;i++)
for(int j=head[i];j;j=edge[j].next)
if(belong[i]!=belong[edge[j].to])
out[belong[i]]++;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
m=read();begin();
;i<=m;i++)
x=read(),y=read(),add(x,y);
;i<=n;i++)
if(!dfn[i]) tarjan(i);
shrink_point();
x=;
;i<=n;i++)
if(!out[belong[i]]) ans[++x]=i;
sort(ans+,ans++x);
if(x)
{
;i<x;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[x]);
}
else printf("\n");
}
;
}
注意:注意数组的大小!!
The Bottom of a Graph的更多相关文章
- The Bottom of a Graph(tarjan + 缩点)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9139 Accepted: ...
- 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 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- POJ 2553 The Bottom of a Graph (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- 【图论】The Bottom of a Graph
[POJ2553]The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11182 ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- poj--2553--The Bottom of a Graph (scc+缩点)
The Bottom of a Graph Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Oth ...
- POJ——T2553 The Bottom of a Graph
http://poj.org/problem?id=2553 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10987 ...
- POJ-2552-The Bottom of a Graph 强连通分量
链接: https://vjudge.net/problem/POJ-2553 题意: We will use the following (standard) definitions from gr ...
随机推荐
- Python3简明教程(十一)—— 类
本节中将通过定义一些简单的 Python 类,来学习 Python 面向对象编程的基本概念. 定义类 在写你的第一个类之前,你应该知道它的语法.我们以下面这种方式定义类: class nameofth ...
- [SQL]连续三天有销售额
店铺 销售日期 销售额 A 2017-10-11 300 A 2017-10-12 200 B 2017-10-11 400 B 2017-10-12 200 A 2017-10-13 100 A 2 ...
- 记忆化搜索 || POJ 1088 滑雪
从任意一点可以往上下左右比它小的数那里走,问最远长度是多少 *解法:每一点dfs搜索一遍 记忆化搜索:http://blog.csdn.net/acmer_sly/article/details/53 ...
- 简单批处理命令直接启动你的AVD
大家都知道,要想启动AVD,一般方法是先打开Android SDK and AVDmanager,再选择你要启动的AVD选择start(废话) 那么,有没有一种简单的方法在任何位置一键启动你指定的av ...
- nodejs实现网站数据的爬取
// 引入https模块,由于我们爬取的网站采用的是https协议 const https = require('https'); // 引入cheerio模块,使用这个模块可以将爬取的网页源代码进行 ...
- luogu P1042 乒乓球
题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华就是其中一位,他退役 ...
- Python数据分析 Pandas模块 基础数据结构与简介(一)
pandas 入门 简介 pandas 组成 = 数据面板 + 数据分析工具 poandas 把数组分为3类 一维矩阵:Series 把ndarray强大在可以存储任意数据类型可以专门处理时间数据 二 ...
- LeetCode(52) N-Queens II
题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- Android开发——程序锁的实现(可用于开发钓鱼登录界面)
1. 程序锁原理 1.1 实现效果: 在用户打开一个应用时,若此应用是我们业务内的逻辑拦截目标,那就在开启应用之后,弹出一个输入密码的界面,输入密码正确则进入目标应用.若不输入直接按返回键,则直接返回 ...