POJ 3177 Redundant Paths 无向图边双联通基础题

题意:

给一个无向图,保证任意两个点之间有两条完全不相同的路径

求至少加多少边才能实现



题解:

得先学会一波tarjan无向图

桥的定义是:删除这条边之后该图不联通

一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足 DFN(u)<Low(v).(因为 v 想要到
达 u 的父亲必须经过(u,v)这条边,所以删去这条边,图不连通)

先用Tarjan无向图缩边双联通分量,这样原图就构成了一颗树,

对于树的叶子节点来说,显然他们需要连边,可以证明的是,我们连至多(叶子节点个数+1)/2的边就可以完成加边(叶子节点两两相连)

所以答案就是(叶子节点个数+1)/2

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define N 5010
 5 #define M 10100
 6 using namespace std;
 7 int head[N],cut[M],n,m,ecnt=2,u,v,dfn[N],low[N],indx,fa[N],du[N],ans;
 8 struct edge
 9 {
10     int u,v,nxt;
11 }e[M*2];
12 inline int find(int x)
13 {
14     return fa[x]=fa[x]==x?x:find(fa[x]);
15 }
16 void add(int u,int v)
17 {
18     e[ecnt].v=v;
19     e[ecnt].nxt=head[u];
20     e[ecnt].u=u;
21     head[u]=ecnt++;
22     e[ecnt].v=u;
23     e[ecnt].nxt=head[v];
24     e[ecnt].u=v;
25     head[v]=ecnt++;
26 }
27 void dfs(int u,int E)
28 {
29     dfn[u]=low[u]=++indx;
30     for (int i=head[u];i;i=e[i].nxt)
31     {
32     if (i==(E^1)) continue;
33     int v=e[i].v;
34     if (!dfn[v])
35     {
36         dfs(v,i);
37         if (low[v]<low[u]) low[u]=low[v];
38         if (low[v]>dfn[u]) cut[i]=cut[i^1]=1;
39     }
40     else
41         if (dfn[v]<low[u])
42         low[u]=dfn[v];
43     }
44 }
45 int main()
46 {
47     scanf("%d%d",&n,&m);
48     for (int i=1;i<=m;i++)
49     {
50     scanf("%d%d",&u,&v);
51     add(u,v);
52     }
53     dfs(1,-1);
54     for (int i=1;i<=n;i++)
55     fa[i]=i;
56     for (int i=2;i<ecnt;i+=2)
57     if (!cut[i]) fa[find(e[i].u)]=find(e[i].v);
58     for (int i=2;i<ecnt;i+=2)
59     if (cut[i]) du[find(e[i].u)]++,du[find(e[i].v)]++;
60     for (int i=1;i<=n;i++)
61     if (find(i)==i && du[i]==1) ans++;
62     printf("%d",(ans+1)/2);
63     return 0;
64 }
时间: 2024-11-05 11:24:57

POJ 3177 Redundant Paths 无向图边双联通基础题的相关文章

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

POJ 3177 Redundant Paths(边双连通分量)

[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化为给出一棵树,加边使得其成为边双连通分量的最小边数, 只要从叶节点连一条边到任意节点,那么就可以使得这个叶节点加入到双连通分量中, 那么优先叶节点和叶节点连接,所以其答案为(叶节点+1)/2 [代码] #include <cstdio> #include <algorithm> #in

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

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

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

poj 3177 Redundant Paths

Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using

poj 3177 Redundant Paths (双联通)

/******************************************************* 题目:Redundant Paths (poj 2177) 链接:http://poj.org/problem?id=3177 算法:双联通+缩点 思路:先找出所有双联通分量,把这些分量缩成一个点 再找出所有度为一的点,用这些点数加一除2就可以了 ********************************************************/ #include<cs

POJ 3177 Redundant Paths(边双联通图)

Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc