当前位置: 首页 > news >正文

浅谈基础的图算法——Tarjan求强联通分量算法(c++)

文章目录

  • 强联通分量SCC
    • 概念
    • 例子
    • 有向图的DFS树
    • 代码
    • 例题讲解
      • [POI2008] BLO-Blockade
        • 题面翻译
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 思路
        • AC代码
      • 【模板】割点(割顶)
        • 题目背景
        • 题目描述
        • 输入格式
        • 输出格式
        • 样例 #1
          • 样例输入 #1
          • 样例输出 #1
        • 提示
      • 思路
      • AC代码

强联通分量SCC

SCC之前也有写博客讲解
戳这里

概念

  • 在有向图中, 如果两个点 u, v 满足同时存在从 u 到 v 和从 v 到 u 的路径, 则称两个点强连通
  • 如果有向图任意两个点强连通, 则称为强连通图. 有向图的极大强连通子图称为强连通分量
  • 注意到强连通关系是传递的,所以有向图可以划分为若干不交的强连通分量

例子

在这里插入图片描述

有向图的DFS树

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

下面展示的是洛谷模板 缩点一题的代码
包含强联通分量和拓扑排序两部分

#include<bits/stdc++.h>
using namespace std;
const int N=10000+15;
int n,m,idx,timestap,top,s;
int p[N],head[N],sd[N],dfn[N],low[N];
int sta[N],vis[N];
int h[N],in[N],dist[N];
struct T
{int to;int ne;int fr;
}edge[N*10],ed[N*10];
void add(int x,int y)
{edge[++idx].ne=head[x];edge[idx].fr=x;edge[idx].to=y;head[x]=idx;
}
void tarjan(int x)
{low[x]=dfn[x]=++timestap;sta[++top]=x;vis[x]=1;for (int i=head[x];i;i=edge[i].ne){int v=edge[i].to;if (!dfn[v]) {tarjan(v);low[x]=min(low[x],low[v]);}else if (vis[v]){low[x]=min(low[x],low[v]);}}if (dfn[x]==low[x]){int y;while (y=sta[top--]){sd[y]=x;vis[y]=0;if (x==y) break;p[x]+=p[y];}}
}
int topo()
{queue <int> q;int tot=0;for (int i=1;i<=n;i++)if (sd[i]==i&&!in[i]){q.push(i);dist[i]=p[i];} while (!q.empty()){int k=q.front();q.pop();for (int i=h[k];i;i=ed[i].ne){int v=ed[i].to;dist[v]=max(dist[v],dist[k]+p[v]);in[v]--;if (in[v]==0) q.push(v);}}int ans=0;for (int i=1;i<=n;i++)ans=max(ans,dist[i]);return ans;
}
int main()
{scanf("%d%d",&n,&m);for (int i=1;i<=n;i++)scanf("%d",&p[i]);int u,v;for (int i=1;i<=m;i++){scanf("%d%d",&u,&v);add(u,v);}for (int i=1;i<=n;i++)if(!dfn[i])tarjan(i);for (int i=1;i<=m;i++){int x=sd[edge[i].fr],y=sd[edge[i].to];if (x!=y){ed[++s].ne=h[x];ed[s].to=y;ed[s].fr=x;h[x]=s;in[y]++;}}int ans=topo();printf("%d\n",ans);return 0;
}

例题讲解

[POI2008] BLO-Blockade

题面翻译

B 城有 n n n 个城镇, m m m 条双向道路。

每条道路连结两个不同的城镇,没有重复的道路,所有城镇连通。

把城镇看作节点,把道路看作边,容易发现,整个城市构成了一个无向图。

请你对于每个节点 i i i 求出,把与节点 i i i 关联的所有边去掉以后(不去掉节点 i i i 本身),无向图有多少个有序点 ( x , y ) (x,y) (x,y),满足 x x x y y y 不连通。

【输入格式】

第一行包含两个整数 n n n m m m

接下来 m m m 行,每行包含两个整数 a a a b b b,表示城镇 a a a b b b 之间存在一条道路。

【输出格式】

输出共 n n n 行,每行输出一个整数。

i i i 行输出的整数表示把与节点 i i i 关联的所有边去掉以后(不去掉节点 i i i 本身),无向图有多少个有序点 ( x , y ) (x,y) (x,y),满足 x x x y y y 不连通。

【数据范围】

n ≤ 100000 n\le 100000 n100000 m ≤ 500000 m\le500000 m500000

题目描述

There are exactly n n n towns in Byteotia.

Some towns are connected by bidirectional roads.

There are no crossroads outside towns, though there may be bridges, tunnels and flyovers. Each pair of towns may be connected by at most one direct road. One can get from any town to any other-directly or indirectly.

Each town has exactly one citizen.

For that reason the citizens suffer from loneliness.

It turns out that each citizen would like to pay a visit to every other citizen (in his host’s hometown), and do it exactly once. So exactly n ⋅ ( n − 1 ) n\cdot (n-1) n(n1) visits should take place.

That’s right, should.

Unfortunately, a general strike of programmers, who demand an emergency purchase of software, is under way.

As an act of protest, the programmers plan to block one town of Byteotia, preventing entering it, leaving it, and even passing through.

As we speak, they are debating which town to choose so that the consequences are most severe.

Task Write a programme that:

reads the Byteotian road system’s description from the standard input, for each town determines, how many visits could take place if this town were not blocked by programmers, writes out the outcome to the standard output.

给定一张无向图,求每个点被封锁之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y

输入格式

In the first line of the standard input there are two positive integers: n n n and m m m ( 1 ≤ n ≤ 100 000 1\le n\le 100\ 000 1n100 000, 1 ≤ m ≤ 500 000 1\le m\le 500\ 000 1m500 000) denoting the number of towns and roads, respectively.

The towns are numbered from 1 to n n n.

The following m m m lines contain descriptions of the roads.

Each line contains two integers a a a and b b b ( 1 ≤ a < b ≤ n 1\le a<b\le n 1a<bn) and denotes a direct road between towns numbered a a a and b b b.

输出格式

Your programme should write out exactly n n n integers to the standard output, one number per line. The i t h i^{th} ith line should contain the number of visits that could not take place if the programmers blocked the town no. i i i.

样例 #1
样例输入 #1
5 5
1 2
2 3
1 3
3 4
4 5
样例输出 #1
8
8
16
14
8
思路

魔改一下 tarjan 求割点的过程。
在这里插入图片描述

AC代码
#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int n,m,h[N],idx;
int dfn[N],low[N],siz[N],tot;
long long ans[N];
bool cut[N];
int e[N],ne[N];
inline void add(int u,int v){e[++idx]=v;ne[idx]=h[u];h[u]=idx;
}
void tarjan(int u){dfn[u]=low[u]=++tot;siz[u]=1;int flag=0,sum=0;for(int i=h[u];i;i=ne[i]){int v=e[i];if(!dfn[v]){tarjan(v);siz[u]+=siz[v];low[u]=min(low[u],low[v]);if(low[v]>=dfn[u]){ans[u]+=(long long)siz[v]*(n-siz[v]);sum+=siz[v];flag++;if(u!=1||flag>1) cut[u]=true;}}else low[u]=min(low[u],dfn[v]);}if(!cut[u]) ans[u]=2*(n-1);else ans[u]+=(long long)(n-sum-1)*(sum+1)+(n-1);
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m;for(int i=1;i<=m;i++){int x,y;cin>>x>>y;add(x,y);add(y,x);}tarjan(1);for(int i=1;i<=n;i++)cout<<ans[i]<<endl;return 0;
}

【模板】割点(割顶)

题目背景

割点

题目描述

给出一个 n n n 个点, m m m 条边的无向图,求图的割点。

输入格式

第一行输入两个正整数 n , m n,m n,m

下面 m m m 行每行输入两个正整数 x , y x,y x,y 表示 x x x y y y 有一条边。

输出格式

第一行输出割点个数。

第二行按照节点编号从小到大输出节点,用空格隔开。

样例 #1
样例输入 #1
6 7
1 2
1 3
1 4
2 5
3 5
4 5
5 6
样例输出 #1
1 
5
提示

对于全部数据, 1 ≤ n ≤ 2 × 1 0 4 1\leq n \le 2\times 10^4 1n2×104 1 ≤ m ≤ 1 × 1 0 5 1\leq m \le 1 \times 10^5 1m1×105

点的编号均大于 0 0 0 小于等于 n n n

tarjan图不一定联通。

思路

个点就是去掉这个点之后,图中的强联通分量变多了,那么这个点就是一个割点
因为这样,假设割点左边有一个子图,右边也有一个子图,由于这个点是割点,那么左右一定是没有其他边联通的, 所以该点的联通的连v满足low[v]>=dfn[u],最后特判一下根

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N =1e6+10;
vector<int> g[N<<1];
int n,m,ind[N],low[N],dfn[N],ans,s,ans1[N],tot,num,cut[N],vis[N],now,root;
void tarjan(int u){low[u]=dfn[u]=++now,vis[u]=1;for(int i=0;i<g[u].size();i++){int v=g[u][i];if(!dfn[v]){tarjan(v);if(u==root)	s++;else{low[u]=min(low[u],low[v]);if(low[v]>=dfn[u])	cut[u]=1;}}elselow[u]=min(low[u],dfn[v]);}
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m;for(int i=1,u,v;i<=m;i++){cin>>u>>v;g[u].push_back(v);g[v].push_back(u);}for(int i=1;i<=n;i++){s=0;if(dfn[i])	continue;root=i,now=0;tarjan(i);ind[i]=s;	}	for(int i=1;i<=n;i++)if(ind[i]>1)ans1[++tot]=i;for(int i=1;i<=n;i++){	if(ind[i]!=0)continue;if(cut[i]==1)ans1[++tot]=i;}sort(ans1+1,ans1+tot+1);cout<<tot<<endl;for(int i=1;i<=tot;i++)	cout<<ans1[i]<<" ";
}

这是我的第二十二篇文章,如有纰漏也请各位大佬指正
辛苦创作不易,还望看官点赞收藏打赏,后续还会更新新的内容。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 本地Linux服务器创建我的世界MC私服并实现与好友异地远程联机游戏
  • java学习笔记 VSCode
  • Promethues Metrics
  • 深度学习助力自动驾驶:YOLO目标检测系统的实现与优化
  • 大数据mapper书写范式hdfs
  • 【中级软件设计师】加密技术、数字签名、数字证书 (附软考真题)
  • 大数据系列之:CentOS7安装Python3详细步骤
  • PicGo + gitee 免费搭建个人图床
  • Typora mac版本激活
  • Python | Leetcode Python题解之第327题区间和的个数
  • 深入理解 AWS CodePipeline
  • leetcode169:多数元素
  • 使用 宝塔面板 部署 php网站
  • 操作系统|day4.Linux、Linux内核、Linux负载、Linux文件存储
  • Java Bean Validation 注解:@NotEmpty、@NotBlank 和 @NotNull 的区别
  • Android框架之Volley
  • Codepen 每日精选(2018-3-25)
  • HTTP--网络协议分层,http历史(二)
  • IOS评论框不贴底(ios12新bug)
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • javascript数组去重/查找/插入/删除
  • laravel with 查询列表限制条数
  • Spark RDD学习: aggregate函数
  • Spring核心 Bean的高级装配
  • 阿里研究院入选中国企业智库系统影响力榜
  • 半理解系列--Promise的进化史
  • 搭建gitbook 和 访问权限认证
  • 订阅Forge Viewer所有的事件
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 如何选择开源的机器学习框架?
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • 转载:[译] 内容加速黑科技趣谈
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • # Redis 入门到精通(一)数据类型(4)
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • #162 (Div. 2)
  • #pragma once与条件编译
  • #pragma预处理命令
  • #进阶:轻量级ORM框架Dapper的使用教程与原理详解
  • #微信小程序(布局、渲染层基础知识)
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (微服务实战)预付卡平台支付交易系统卡充值业务流程设计
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)ABI是什么
  • (转)JAVA中的堆栈
  • (转)Sublime Text3配置Lua运行环境
  • (转)甲方乙方——赵民谈找工作
  • (转)四层和七层负载均衡的区别
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .mp4格式的视频为何不能通过video标签在chrome浏览器中播放?
  • .Net 6.0 处理跨域的方式
  • .net Application的目录
  • .net FrameWork简介,数组,枚举
  • .net refrector