【图论】The Bottom of a Graph
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 11182 | Accepted: 4608 |
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+1 is 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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=5101;
const int INF=999999;
int N,M;
vector<int> vec[MAXN];
bool inq[MAXN];
int que[MAXN];
int dfn[MAXN],low[MAXN];
int tar[MAXN];
bool oud[MAXN];
int tot,Col,tmp; void Tarjan(int x){
tot++;
dfn[x]=low[x]=tot;
que[++tmp]=x;
inq[x]=true;
for(int i=0;i<vec[x].size();i++){
int to=vec[x][i];
if(!dfn[to]){
Tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(inq[to]) low[x]=min(low[x],dfn[to]);
}
if(dfn[x]==low[x]){
++Col; tar[x]=Col;
inq[x]=false;
while(x!=que[tmp]){
int k=que[tmp];
tar[k]=Col;
inq[k]=false;
tmp--;
}
tmp--;
}
return ;
}
vector<int> vec2[MAXN];
int ans[MAXN];
int ans2[MAXN];
int atmp,atmp2;
int main(){
while(1){
N=read(); if(!N) break;
M=read();
memset(inq,false,sizeof(inq));
memset(oud,false,sizeof(oud));
atmp=atmp2=0;
memset(dfn,0,sizeof(dfn));
tmp=0,Col=0,tot=0;
memset(tar,0,sizeof(tar));
for(int i=1;i<=N;i++) vec[i].clear(),vec2[i].clear();
for(int i=1;i<=M;i++){
int u=read(),v=read();
vec[u].push_back(v);
}
for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=1;i<=N;i++){
//size[tar[i]]++;
vec2[tar[i]].push_back(i);
for(int j=0;j<vec[i].size();j++){
if(tar[i]!=tar[vec[i][j]])
oud[tar[i]]=true;
}
}
for(int i=1;i<=Col;i++){
if(!oud[i]) ans[++atmp]=i;
}
for(int i=1;i<=atmp;i++){
for(int j=0;j<vec2[ans[i]].size();j++){
ans2[++atmp2]=vec2[ans[i]][j];
}
}
sort(ans2+1,ans2+atmp2+1);
for(int i=1;i<atmp2;i++) printf("%d ",ans2[i]);
printf("%d\n",ans2[atmp2]);
}
}
【图论】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
poj——The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- 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 ...
随机推荐
- TOJ 1049 Jesse's problem (最短路 floyd)
描述 All one knows Jesse live in the city , but he must come to Xiasha twice in a week. The road is to ...
- 设计模式之Prototype
设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) 介绍: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 为什么要用Prototype ...
- 【shell】shell编程(六)-shell函数的应用
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; [return ...
- 查看服务器是否被DDOS攻击的方法
伴随着现代互联网络快速发展,更加容易出现被攻击.尤其是ddos攻击已经不在是大网站需要关心的事情了.不少中小型企业,也在遭受ddos攻击.站长对ddos攻击不了解,所以网站被ddos攻击的时候,都不会 ...
- 打印 pmic register value
打印 PMIC register value 方式有二種, 一種是使用 adb shell cat pmic register 一種是直接在 code 裡 call dump pmic registe ...
- 64_m1
MAKEDEV-3.24-18.fc26.x86_64.rpm 13-Feb-2017 22:33 101030 MUMPS-5.0.2-8.fc26.i686.rpm 14-Feb-2017 13: ...
- 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...
- jquery - 实例1
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="text2.aspx.cs& ...
- 详解java中的TreeSet集合
TreeSet是实现Set接口的实现类.所以它存储的值是唯一的,同时也可以对存储的值进行排序,排序用的是二叉树原理.所以要理解这个类,必须先简单理解一下什么是二叉树. 二叉树原理简述 假如有这么一个集 ...
- python_day5学习笔记
一.正则表达式 字符: \d 匹配任何十进制数:相当于类[0-9] \D 匹配任何非数字字符:相当于类[^0-9] \s 匹配任何空白字符:相当于类[ \t\n\r\f\v] \S 匹配任何非空 ...