POJ 2553 The Bottom of a Graph

题目链接

题意:给定一个有向图,求出度为0的强连通分量

思路:缩点搞就可以

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std; const int N = 5005; int n, m;
vector<int> g[N], save[N];
stack<int> S; int pre[N], dfn[N], dfs_clock, sccno[N], sccn; void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (dfn[u] == pre[u]) {
sccn++;
save[sccn].clear();
while (1) {
int x = S.top(); S.pop();
save[sccn].push_back(x);
sccno[x] = sccn;
if (x == u) break;
}
}
} void find_scc() {
dfs_clock = sccn = 0;
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
for (int i = 1; i <= n; i++)
if (!pre[i]) dfs_scc(i);
} int out[N];
int ans[N], an; int main() {
while (~scanf("%d", &n) && n) {
for (int i = 1; i <= n; i++) g[i].clear();
scanf("%d", &m);
int u, v;
while (m--) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
}
find_scc();
memset(out, 0, sizeof(out));
for (int i = 1; i <= n; i++) {
for (int j = 0; j < g[i].size(); j++) {
int v = g[i][j];
if (sccno[i] != sccno[v]) {
out[sccno[i]]++;
}
}
}
an = 0;
for (int i = 1; i <= sccn; i++) {
if (!out[i]) {
for (int j = 0; j < save[i].size(); j++)
ans[an++] = save[i][j];
}
}
sort(ans, ans + an);
for (int i = 0; i < an; i++)
printf("%d%c", ans[i], i == an - 1 ? '\n' : ' ');
}
return 0;
}

POJ 2553 The Bottom of a Graph(强连通分量)的更多相关文章

  1. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  2. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  3. 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 ...

  4. poj 2553 The Bottom of a Graph【强连通分量求汇点个数】

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9641   Accepted:  ...

  5. [poj 2553]The Bottom of a Graph[Tarjan强连通分量]

    题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...

  6. POJ 2553 The Bottom of a Graph(强连通分量的出度)

    题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...

  7. POJ 2553 The Bottom of a Graph (Tarjan)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11981   Accepted: ...

  8. POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  9. poj 2553 The Bottom of a Graph

    求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...

随机推荐

  1. POJ 2186 Tarjan

    题意:有n(n<=10000)头牛,每头牛都想成为最受欢迎的牛,给出m(m<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不是相互的,那么就是说1欢迎2不代表2欢迎1, ...

  2. Python启动浏览器Firefox\Chrome\IE

    # -*- coding:utf-8 -*- import os import selenium from selenium import webdriver from selenium.webdri ...

  3. 【SQL】INTERVAL YEAR TO MONTH 和 INTERVAL DAY TO SECOND

    INTERVAL YEAR TO MONTH: 作为年和月的时间间隔存储 INTERVAL DAY TO SECOND: 作为天.小时.分和秒的时间间隔存储(DAY,HOUR,MINUTE,SECON ...

  4. Type inference

    Type inference refers to the automatic detection of the data type of an expression in a programming ...

  5. django分页功能,templatetags的应用

    django 将不会将得到的html代码自动转化 from django.utils.html import format_html html =''' <a href='http://www. ...

  6. Object.assign和序列/反序列

    Object.assign let testObj = { a:[1,2,4], b:{ name:'ls', school:['huf','yelu'], parent:{ father:'lili ...

  7. spring cloud(五) hystrix

    开启feign 熔断 hystrix    整合hystrix-dashboard监控面板 1. 服务调用者boot工程 pom引入依赖 <!-- hystrix-dashboard 监控依赖 ...

  8. Scala语言学习笔记——方法、函数及异常

    1.Scala 方法及函数区别 ① Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量.换句话来说在类中定义的函数即是方法 ② Scal ...

  9. 解决vcenter 6.0 vcsa安装插件时报错的问题

    在安装vCenter 6.0 vsca的时候,安装插件到第二个的时候,会报出一个windows installer的错误.需要联系软件管理员或者技术支持的一个error. 经过多次的测试,我终于找到了 ...

  10. 机器学习K-Means

    1.K-Means聚类算法属于无监督学习算法. 2.原理:先随机选择K个质心,根据样本到质心的距离将样本分配到最近的簇中,然后根据簇中的样本更新质心,再次计算距离重新分配簇,直到质心不再发生变化,迭代 ...