POJ2553-The Bottom of a Graph
题意:求解Bottom(G)。即集合内的点能够互相到达。
思路:有向图的强连通。缩点,找出出度为0的点,注意符合的点要按升序输出。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAXN = 5010;
const int MAXM = 50010; struct Edge{
int to, next;
}edge[MAXM]; int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];
int Index, top;
int scc;
bool Instack[MAXN];
int num[MAXN];
int n, m;
int out[MAXN], ans[MAXN]; void init() {
tot = 0;
memset(head, -1, sizeof(head));
} void addedge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if (!DFN[v]) {
Tarjan(v);
if (Low[u] > Low[v]) Low[u] = Low[v];
}
else if (Instack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if (Low[u] == DFN[u]) {
scc++;
do {
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc;
num[scc]++;
} while (v != u);
}
} void solve() {
memset(Low, 0, sizeof(Low));
memset(DFN, 0, sizeof(DFN));
memset(num, 0, sizeof(num));
memset(Stack, 0, sizeof(Stack));
memset(Instack, false, sizeof(Instack));
Index = scc = top = 0;
for (int i = 1; i <= n; i++)
if (!DFN[i])
Tarjan(i);
} int main() {
while (scanf("%d%d", &n, &m) && n) {
init();
int u, v;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
addedge(u, v);
}
solve(); memset(out, 0, sizeof(out));
for (int u = 1; u <= n; u++) {
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (Belong[u] != Belong[v])
out[Belong[u]]++;
}
}
memset(ans, 0, sizeof(ans));
int cnt = 0;
for (int i = 1; i <= scc; i++)
for (int u = 1; u <= n; u++) {
if (out[i] == 0) {
if (Belong[u] == i)
ans[cnt++] = u;
}
}
sort(ans, ans + cnt);
for (int i = 0; i < cnt; i++)
if (i == 0) printf("%d", ans[i]);
else printf(" %d", ans[i]);
printf("\n");
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
POJ2553-The Bottom of a Graph的更多相关文章
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...
- 【图论】The Bottom of a Graph
[POJ2553]The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11182 ...
- POJ-2552-The Bottom of a Graph 强连通分量
链接: https://vjudge.net/problem/POJ-2553 题意: We will use the following (standard) definitions from gr ...
- 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 ...
随机推荐
- 从sample来学习Java堆(转)
1)Java堆 所有对象的实例分配都在Java堆上分配内存,堆大小由-Xmx和-Xms来调节,sample如下所示: public class HeapOOM { static class OOMOb ...
- 使用高性能xml序列化框架jibx作为spring mvc的xml view
package org.springframework.web.servlet.view.xml; import java.io.ByteArrayOutputStream; import java. ...
- OpenSSL win 下报错 OPENSSL_Uplink(58D46000,08): no OPENSSL_Applink
python 中使用 M2Crypto 想读写文件时总是报: OPENSSL_Uplink(58D46000,08): no OPENSSL_Applink windows 平台下使用 openssl ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
- 在SSH框架中增加SiteMesh的支持
1)引入jar包,如下两个jar包需要导入到系统的lib文件夹中: sitemesh-2.4.jar struts2-sitemesh-plugin-2.2.1.1.jar 2)修改web.xml增加 ...
- Result Cache结果高速缓存 (转)
1.1 概述 SQL 查询结果高速缓存可在数据库内存中对查询结果集和查询碎片启用显式高速缓存.存储在共享池(Share Pool)中的专用内存缓冲区可用于存储和检索高速缓存的结果.对查询访问的数据库对 ...
- JqueryUI-3
本文在于巩固基础 JqueryUI控件之滚动条 这次主要是看Jquery官网上的API文档来做的 <!DOCTYPE html> <html> <head lang=&q ...
- JavaScript跨域深入研究与解决办法(转)
什么是跨域 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首 ...
- Windows系统的安装
一.写在前面 笔者最近因为换工作,在家待业甚感无聊,于是想要整理一些在Windows系统的一些安装方法和下载资源,一来给自己做个备忘,二来把一些不成熟的想法分享出去,希望大家予以指正. ...
- css系列教程--选择器
css派生选择器:是指在某个样式表或者dom元素的行内定义行内元素的属性,而其他同名样式在其他dom节点无效的样式表定义方式.例如:div ul li{border:1px solid red;}&l ...