kuangbin专题 专题九 连通图 POJ 3177 Redundant Paths
题目链接:https://vjudge.net/article/371?tdsourcetag=s_pcqq_aiomsg
题目:给定一个连通图,题目说,任意两个点至少有一条路线可以相互到达,
为保证任意两点有完全不同的路线(点可以相同,边不能相同)可以相互到达至少需要加几条边。
思路:tarjan缩点,之后重构图,找出度数为1的scc个数scc_cnt,这些点相互连接,答案可以得出是 (scc_cnt+1)/2。
两组样例:
n = 5 ,m = 4 | (1 2) (1 3) )(1 4) (1 5)
n = 8, m = 9 | (1 2) (1 4) (2 3) (3 4) (4 5) (5 6) (6 7) (5 8) (7 8 )
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- const int N = (int)5e3+;
- int head[N],dfn[N],low[N],scc_no[N],s[N],du[N];
- int n,m,tot,tim,scc,top;
- struct node{
- int to;
- int nxt;
- }e[N << ];
- inline void add(int u,int v){
- e[tot].to = v;
- e[tot].nxt = head[u];
- head[u] = tot++;
- }
- void tarjan(int now,int pre){
- dfn[now] = low[now] = ++tim;
- s[top++] = now;
- //这里有情况,两点之间可能>1条路直接连接
- //所以,需要处理下边的重复问题
- int to,pre_cnt = ;
- for(int o = head[now]; ~o; o = e[o].nxt){
- to = e[o].to;
- //只是一条无向边,处理一下
- if(to == pre && pre_cnt == ){
- pre_cnt = ;
- continue;
- }
- if(!dfn[to]){
- tarjan(to,now);
- low[now] = min(low[now],low[to]);
- }
- else low[now] = min(low[now],dfn[to]);
- }
- if(dfn[now] == low[now]){
- ++scc;
- int x;
- do{
- x = s[--top];
- scc_no[x] = scc;
- }while(now != x);
- }
- }
- void rebuild(){
- int to;
- for(int now = ; now <= n; ++now){
- for(int o = head[now]; ~o; o = e[o].nxt){
- to = e[o].to;
- if(scc_no[now] != scc_no[to]){
- ++du[scc_no[now]];
- ++du[scc_no[to]];
- }
- }
- }
- }
- void solve(){
- int p = ;
- // cout << scc << endl;
- //度数为什么是2的,因为是无向图,其实除2就是度数为1了
- for(int i = ; i <= scc; ++i)
- if(du[i] == ) ++p;
- // cout << p << endl;
- printf("%d\n",(p+)/);
- }
- int main(){
- int u,v;
- scanf("%d%d",&n,&m);
- for(int i = ;i <= n; ++i) head[i] = -;
- for(int i = ; i < m; ++i){
- scanf("%d%d",&u,&v);
- add(u,v); add(v,u);
- }
- tarjan(,);
- rebuild();
- solve();
- return ;
- }
kuangbin专题 专题九 连通图 POJ 3177 Redundant Paths的更多相关文章
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ - 3177 Redundant Paths(边双连通分支)(模板)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...
- poj 3177 Redundant Paths
题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
随机推荐
- hadoop_2.6.5集群安装
安装hadoop2.6.5集群: 1.规划设计: JacK6:NameNode,jobtracker JacK7:secondnode,datenode,tasktracker JacK8:datan ...
- 利用geojson实现模型轨迹运动
直接上代码 var viewer = new Cesium.Viewer('cesiumContainer'); //Set the random number seed for consistent ...
- Jenkins | 搭建你第一个Jenkins应用
搭建你第一个Jenkins应用 1.准备工作 第一次使用 Jenkins,您需要: 机器要求: 256 MB 内存,建议大于 512 MB 10 GB 的硬盘空间(用于 Jenkins 和 Docke ...
- python接口自动化测试 - 数据驱动DDT模块的简单使用
DDT简单介绍 名称:Data-Driven Tests,数据驱动测试 作用:由外部数据集合来驱动测试用例的执行 核心的思想:数据和测试代码分离 应用场景:一组外部数据来执行相同的操作 优点:当测试数 ...
- apace访问403错误的解决方法汇总
作为一个努力学习的实习生,遇到问题还是靠记录才能更好的学习. 首先附上故障图 翻译过来就是啥呢? 于是天真的我去百度了一下大神们的解决方法,目录没权限嘛,来个777就完事了.一开始还觉得挺合乎情理的, ...
- Fedora 安装及配置
引言 最近学习课程要用到Linux,之前装的Ubuntu双系统被我删掉了(因为后来发现那个WSL,win子系统还挺好用的),所以上午用虚拟机再装了一下老师给的Ubuntu16,也不知道怎么回事特别卡, ...
- 非常NB的一款快捷启动软件--Merry
Merry 被设计为了能将日常重复性操作简化为一个快捷键或者命令.Merry 采用完全开放的体系, 可以使用 Lua 或者外部程序来扩展 Merry 的功能. 另附一个自己扩展的LUA脚本: --启动 ...
- js小练习---实现红绿灯
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java设计模式5——适配器模式
java设计模式5--适配器模式 1.结构型模式介绍 1.1.作用 从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 分类: 适配器模式 代理模式 桥接模式 装饰模式 组合模式 ...
- Java并发读书笔记:Lock与ReentrantLock
Lock位于java.util.concurrent.locks包下,是一种线程同步机制,就像synchronized块一样.但是,Lock比synchronized块更灵活.更复杂. 话不多说,我们 ...