POJ 2553 - The Bottom of a Graph(强连通分量, 缩点)

[强连通]poj 2553 The Bottom of a Graph /** [强连通]poj 2553 The Bottom of a Graph 这个题目的意思还是得认真读原题,不得不吐槽那本图论的书上中文坑爹的翻译。 题目定义了有向图上一个叫做sink的东西,结点u是sink的条件是,图上任意一点w, 如果u能到达w,那么w也能到达u。 思路,强连通缩点后位于叶子上的即使sink */ #include #include #inclu 阅读详情

题意:如果v点能够到的点,反过来也能够到v点,则称这个点为sink点,输出所有的sink点


思路:求下强连通分量,出度为0的连通分量里的点都是sink点


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 1e4+5;
int n, m, dfn[maxn], low[maxn], belong[maxn], out[maxn];
int dfs_clock, scc_cnt;
bool is[maxn];
vector<int> g[maxn];
stack<int> s;

void tarjan(int u)
{
    dfn[u] = low[u] = ++dfs_clock;
    s.push(u);
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(!belong[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u])
    {
        scc_cnt++;
        while(1)
        {
            int x = s.top(); s.pop();
            belong[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

void find_scc()
{
    while(!s.empty()) s.pop();
    dfs_clock = scc_cnt = 0;
    memset(belong, 0, sizeof(belong));
    memset(dfn, 0, sizeof(dfn));
    for(int i = 1; i <= n; i++)
        if(!dfn[i])
            tarjan(i);
}

void solve()
{
    memset(out, 0, sizeof(out));
    memset(is, 0, sizeof(is));
    for(int u = 1; u <= n; u++)
        for(int i = 0; i < g[u].size(); i++)
        {
            int v = g[u][i];
            if(belong[u] != belong[v])
                out[belong[u]]++;
        }
    for(int i = 1; i <= scc_cnt; i++)
        if(!out[i])
            is[i] = 1;
    int lead = 0;
    for(int i = 1; i <= n; i++)
        if(is[belong[i]])
        {
            if(lead++) printf(" ");
            printf("%d", i);
        }
    puts("");
}

int main(void)
{
    while(cin >> n && n)
    {
        scanf("%d", &m);
        for(int i = 0; i < maxn; i++)
            g[i].clear();
        for(int i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            g[u].push_back(v);
        }
        find_scc();
        solve();
    }
    return 0;
}



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

Description

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph. 
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

The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2


使用STM输出互补SPWM波(三相各差120°) 使用STM32输出SPWM波形(三相各差120°) 阅读详情

相关推荐

Q学习(Q-learning)入门小例子及python实现

一、从马尔科夫过程到Q学习 # 有一定基础的读者可以直接看第二部分 Q学习(Q-learning)算法是一种与模型无关的强化学习算法,以马尔科夫决策过程(Markov Decision Processes, MDPs)为理论基础。 标准的马尔科夫决策过程可以用一个五元组<S,A,P,R,γ>表示,其中: S是一个离散有界的状态空间; A是一个离散的动作空间; P为状态转...

wlm_py的博客 4万+

The Bottom of a Graph Poj 2553

牛客网 文章目录Description题意:题解: Description We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being

qq_35975367的博客 497

HFSS与AI融合:智能算法如何重塑天线设计流程

本文探讨了HFSS与AI融合如何通过智能算法革新天线设计流程。在5G/6G通信时代,AI技术如机器学习和遗传算法显著提升了高频电磁场仿真效率,将传统设计周期从数周缩短至数小时,并优化了天线性能。文章详细介绍了AI与HFSS的集成策略、关键技术突破及典型应用案例,展示了智能算法在天线设计中的巨大潜力。

weixin_29035147的博客 781

poj2553

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, i...

qq_44238714的博客 356

软件工程实验报告--用Visio绘制数据流图、ER图、状态图、软件结构图、程序流程图、系统流程图

用Visio绘制数据流图、ER图、状态图、软件结构图、程序流程图、系统流程图

m0_73814332的博客 4164

SpaceMesh Mining 教程

當參與Spacemesh的用戶在完成P盤工作後需要完成兩個部分工作:PoET+PoST,這兩部分均是在不同的規定時間完成,具體可參考後續的時間表。註意!只有PoET和PoST均完成的用戶才可以獲得收益。Spacemesh相關P盤教程:說明:1.Spacemesh每個紀元epoch周期為14天,在8月7日完成PoET的將在8月20日完成PoST,收益將在8月25日後發放(之後用戶收益將每隔14天發放一次) 後續PoET時間為8月7日+N14 **後續PoST時間為8月20日+N。

Yc Chan 的个人博客 1045

poj2553(强连通分量)

题意:给出有向图,图的底部是所有汇节点的子集,即,底部(G)={v∈V\x-∀w∈V:(v→w)⇒(w→v)},w在……里面G可以从v, v也可从w。让我们求有多少个点是绘点的问题。 思路:先求解出强连通分量,然后将求得的强连通分量看成是一个新点,进行缩点,要求绘点,那么求出这些新点中出度为零的点即可,最后从N个点中找出绘点并记录输出即可。 定义: 强连通图:在有向图中,任意节点除法都可以到达其余所有节点,则称为强连通图。 强连通分量:在非强连通图的有向图中,选取部分点为强连通图,该强连通子图称为强连通分量

Keep_Trying_Go的博客 469

Ubuntu系统下主板GPU温控pwm风扇

在Linux系统下——以Ubuntu为例,配置主板sys_fan等接口连接pwm温控风扇转速根据NVIDIA GPU温度,为服务器GPU等无主动散热显卡提供散热。

罗思哲的博客 5035

poj 2553 缩点+染色+出度

题目链接:https://vjudge.net/problem/POJ-2553 如果不会tarjan算法,推荐博客:https://blog.youkuaiyun.com/mengxiang000000/article/details/51672725 题意大意:第一行输入一个n(n==0时结束程序),和一个m分别代表点数和边数,接下来一行有2*m个数字,每两个数字u,v表示一条有向边,即u可以到达v。...

weixin_30815469的博客 133

《QT+PCL》点云的点选与框选

【代码】《QT+PCL》点云的点选与框选。

阁楼 3602

vue支付项目-APP支付宝支付功能

如果用户已安装支付宝客户端,商家 App 唤起支付宝完成支付,支付完后跳回到商家 App 内,最后展示支付结果。如果用户没有安装支付宝客户端,商家 App 内会调起支付宝网页支付收银台,用户登录支付宝账号,支付完后展示支付结果。1.点击按钮开通或者续费,会验证是否选择会员商品以及对协议是否查看选中(这里根据项目需要自行验证,可忽略),验证通过打开选择支付方式弹窗页面,选择微信支付或者支付宝支付;4.可取消支付,放弃支付会返回会员页面,页面提示支付取消;1.判断支付方式,如果是2,则是支付宝支付方式。

总结前端开发中遇到的问题,分享前端知识 1万+

POJ2553 The Bottom of a Graph(强连通量Tarjan算法)

为尊重原创,本博客是学习:https://blog.youkuaiyun.com/mengxiang000000/article/details/51672725,然后加上自己的再度理解,只是代码的搬运工,如果有侵权可联系。 原题链接:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 6...

Shaosen的博客 362

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit:3000MS Memory Limit:65536K Total Submissions:7881 Accepted:3263 Description We will use t...

diansitao5461的博客 185

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【poj2553】The Bottom of a Graph(强连通分量缩点)

题目链接:http://poj.org/problem?id=2553 【题意】 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出。sink点是指该点能到达的点反过来又能回到该点。 【思路】 不难想象sink点一定是在强连通分量中,而且强连通分量缩点后出度为0,就可以说明该强连通分量内所有的点都是sink点。 之前wa了一发是因为写成了out[i],注意是从缩点构成的...

ayci54153的博客 171

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

题干: We will use the following (standard) definitions from graph theory. LetVbe a nonempty and finite set, its elements being called vertices (or nodes). LetEbe a subset of the Cartesian productV...

xuanweiace的博客 423

POJ--2553The Bottom of a Graph--强连通分量+缩点

The Bottom of a Graph Time Limit:3000MS Memory Limit:65536K Total Submissions:12596 Accepted:5177 Description We will use the following (standard) definitions from graph theo...

lanshan1111的博客 204

POJ2553 The Bottom of a Graph(强连通分量+缩点)

题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v)。 把图的强连通分量缩点,那么答案显然就是所有出度为0的点。 用Tarjan找强连通分量: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; ...

dengliugong3918的博客 147

poj2553 The Bottom of a Graph--Kosaraju算法 & 缩点 & 强连通分量

原题链接:http://poj.org/problem?id=2553 题意:n个点,m对点的关系,定义link点:一个点u所能到达的点,反过来都能到达u,那么点u就是link点。升序输出所有的link点。 分析:强连通缩点后,找到出度为0的即可,具体为什么,看 给的两个实例。 #define _CRT_SECURE_NO_DEPRECATE #include #

半壶老酒 607

POJ2553 The Bottom of a Graph 强连通分量+缩点

【题意】求sink点,即其可达的点均有一条路径回到该点,按序号升序输出。先求各强连通分量,出度为0的强连通分量中的点即为所求的sink点。(若出度不为0,即某强连通分量有向外的边,必没有“回”路,那么该强连通分量内的点均不是sink点)【代码】View Code 转载于:https://www.cnblogs.com/byluoluo/archive/2013/03/05/2944173.h...

weixin_30855099的博客 139

POJ 2553 The Bottom of a Graph 强连通分量+缩点 tarjan or kosaraju

题目的意思是求有向图中满足“自己可达的顶点都能到达自己”的顶点个数 显然,在一个强连通分量中,每个点都符合要求,但是 如果强连通分量中有某个点跟外面的某个点相连了,这个强连通分量就不符合要求了,很显然,外面的点是无法回到这个点上的,如果能回到这个点,就是强连通分量中的一员了,这是矛盾的。 那么结论就是,缩点后,求出度为0的强连通分量中的顶点。 tarjan写的 #include #inc

水 1010

POJ-2553 The Bottom of a Graph (Tarjan强联通+缩点)

题目连接 题意: 一个有向图,然后让你求这个图的底部(求出所有没有出度的强联通分量),如果没有底部输出空行,升序输出 数据范围:n < 5000 思路: Tarjan缩点后,判断某个点是否有出度,找到出度为0的点,把这个点(缩点后的点,缩点前是强联分量),把这个点内的所有点升序输出 AC代码: /* Tarjan 缩点 我们既然能在有向图中找到环,那么我...

Harington的博客 346

POJ-2553 The Bottom of a Graph (强连通分量[Tarjan])

注意理解题意即可(只有理解了,才能看懂样例...) 满足题意的v点有两种情况: ①对于任一点w,v可达w,且w可达v,即v、w互相可达,满足该情况的点在同一个强连通分量中(且该强连通分量不与其他点连通) ②对于任一点w,v均不可达w,满足该情况的点是出度为0的点 所以可以先求出所有的强连通分量,并缩点,则只剩下情况②,则所有出度为0的缩点都满足题意,即缩点中的点都满足题意

idealism_xxm的专栏 495
上一篇: HAUT 1262 魔法宝石 (最短路变形 or 暴力)
下一篇: POJ 3694 Network(割边+LCA)
cillyb
博客等级 码龄11年 225粉丝 497原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值