题目链接: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的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  3. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  5. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  6. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  7. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

  8. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

  9. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

随机推荐

  1. 软件发布!DOTA2统计学

    更新日志: 1.2 增加DOTABUFF作为数据源,可以与DOTAMAX切换 1.1 增加关于对话框 增加版本信息 修复列表框头的错误   受到 http://tieba.baidu.com/p/38 ...

  2. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

    题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...

  3. HTTP核心模块(HTTP Core)

    alias 语法:alias file-path|directory-path;默认值:no使用字段:location这个指令指定一个路径使用某个某个,注意它可能类似于root,但是document ...

  4. Spring(四)核心容器 - BeanDefinition 解析

    前言 在上篇文章中,我们讨论了 refresh 的前四个方法,主要是对 ApplicationContext 上下文启动做一些准备工作.原计划是对接下来的 invokeBeanFactoryPostP ...

  5. JVM 面试题汇总

    JVM 面试题汇总 1.什么是 JVM?它有什么作用? 答:JVM 是 Java Virtual Machine(Java 虚拟机)的缩写,顾名思义它是一个虚拟计算机,也是 Java 程序能够实现跨平 ...

  6. gcd(最大公约数)算法

    PS: 求一个两个数之间的最大公约数,往往需要被记起. int gcd(int x, int y) { if(y == 0) return x; int r = x % y; return gcd(y ...

  7. 实验17:NAT

    实验14-1:静态NAT 配置 Ø    实验目的通过本实验可以掌握(1)静态NAT 的特征(2)静态NAT 基本配置和调试 Ø    拓扑结构 实验步骤n    步骤1:配置路由器R1 提供NAT ...

  8. POJ_3663_贪心

    题目描述: 给你一堆数和一个限定的空间大小,要求求出两个数的和小于等于空间大小的对数. 思路: 贪心,先给一堆数从大到小排序. 第一个数取数组第一个,第二个数从第二个开始依次往后取,只要某个第二个数满 ...

  9. Codeforces_442_A_枚举

    http://codeforces.com/problemset/problem/442/A 想想成5*5的图,一共能划10条线,枚举2^10次即可. 判断每种情况是否符合条件的方法,若存在点,被线穿 ...

  10. js dom一些操作,记录一下自己写的没有意义,可以简略翻过 第八章

    第八章,一些dom操作,和几个常用的函数 var s= document.getElementById("new"); console.log(s.length); var a= ...