【图论】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 ...
随机推荐
- 详解JS中Number()、parseInt()和parseFloat()的区别
三者的作用: Number(): 可以用于任何数据类型转换成数值: parseInt().parseFloat(): 专门用于把字符串转换成数值: 一.Number( ): (1)如果是Boolean ...
- 移动端页面使用rem布局
阿里团队的高清布局方案代码 所谓高清方案就是根据设备屏幕的DPR(设备像素比,又称DPPX,比如dpr=2时,表示1个CSS像素由4个物理像素点组成) 动态设置 html 的font-size, 同时 ...
- LCD实验学习笔记(十):TFT LCD
硬件组成: REGBANK是LCD控制寄存器组,含17个寄存器及一块256*16的调色板,用来设置参数. LCDCDMA中有两个FIFO,当FIFO空或数据减少到阈值,自动发起DMA传输,从内存获取图 ...
- kernel cmdline
從 lk 傳送到 kerel 的 cmdline 會放在開機後的 adb /proc/cmdline 開到 android 後,又會被讀出來 /system/core/init/util.cpp 27 ...
- 64_l3
libguac-client-ssh-0.9.13-3.20170521git6d2cfda...> 23-May-2017 09:58 64570 libguac-client-ssh-0.9 ...
- python 使用headless chrome滚动截图
from selenium import webdriver from selenium.webdriver.chrome.options import Options import util chr ...
- const 引用的分析
const 引用: 在初始化常量引用时,允许用任意表达式作为初始值,只要该表达式的结果能转换成引用的类型即可.尤其,允许为一个常量引用绑定非常量的对象.字面值,甚至是一个表达式.我们来看 const ...
- C 实现一个简易的Http服务器 (二)
正文 - 直接搞起 C 实现一个简易的Http服务器 很久以前写过一个简易的http服务器, 后面和一个朋友交流, 反思后发现问题不少.在这里简单搞一下. 让其更加简单去表现httpd本质, 弱化协议 ...
- java多线程以及Android多线程
Java 多线程 线程和进程的区别 线程和进程的本质:由CPU进行调度的并发式执行任务,多个任务被快速轮换执行,使得宏观上具有多个线程或者进程同时执行的效果. 进程:在操作系统来说,一个运行的程序或者 ...
- linux下运行jmeter脚本
1. win下生成测试计划 2. 上传至linux下 3.运行测试计划 sh jmeter.sh -n -t second_login.jmx -l res.jtl 错误1: solution ...