MYF

POJ 3352 Road Construction

题目链接

POJ 3352

方法:Tarjan构建边-双连通图

题目分析

题目大意

n个点,m条边,希望从任意一个点到另一个点之间都可以有两条路可以走

解析

这两天正在学习双连通分量的东西,网上搜到了这道题,做一做。

首先,我们为了让他有两条路可以走,那么就等价于将图建成边-双连通图。看到网上的做法,很神奇,将每一个双连通分量先看成一个点,这样,整个图就可以形成一棵树,那么可以统计出树的叶节点的个数leaf,然后两两连边,如果最后有剩余,再连一条边出去即可,共计(left+1)/2条。

根据Tarjan算法,我们知道在同一个双连通分量中,他们的low值是相同的。那么,我们能不能直接找桥呢?因为桥是树的边。答案是不能!比如一个蝴蝶结的形状|><|左边的三角形和右边的三角形的low值是不同的,这个图中不存在桥,但是我们无法找到两条完全不重复的路径,因为中间的割点怎么走都会经过,所以我们对于这样的图还要再连一条线。所以我们统计每个分量的入度的时候必须根据low值来确定

然后找出来所有入度为1的点,总个数位leaf,结果为(leaf+1)/2

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <bitset>
#include <cstring>
#include <iostream>
#include <iosfwd>
#include <deque>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
#define PB push_back
#define MP make_pair
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#define debug2(x,y) cout<<"Debug : ---"<<x<<" , "<<y<<"---"<<endl;
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int mod=1000000007;
const int INF=0x3f3f3f3f;
const double eps=1e-11;
//求无向图的割顶和桥
const int maxn=1000+10; //顶点数
int n,m;//n个点 m条边 顶点下标0~n-1
int dfs_clock;//时钟,每访问一个节点增1
vector<int> G[maxn];//G[i]表示i节点邻接的所有节点
int pre[maxn];//pre[i]表示i节点被第一次访问到的时间戳,若pre[i]==0表示i还未被访问
int low[maxn];//low[i]表示i节点及其后代能通过反向边连回的最早的祖先的pre值
bool iscut[maxn];//标记i节点是不是一个割点
int cut[maxn];//cut[i]表示割i点的时图中联通分量的增加量
int in[maxn];
vector<pair<int, int> >Bridge;

int dfs(int u,int fa=-1)//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割顶和桥
{
if (fa == -1) cut[u]--; //如果是根的话,割时增加的量为“连出去的量减一”
int lowu=pre[u]=++dfs_clock;
int child=0; //子节点数目
for(int i=0; i<G[u].size(); i++)
{
int v=G[u][i];
if(!pre[v]){
child++;//未访问过的节点才能算是u的孩子
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u]){
cut[u]++;
iscut[u]=true; //u点是割顶
if(lowv>pre[u]) //割桥判定
Bridge.push_back(make_pair(u, v));
}
}
else if(pre[v]<pre[u] && v!=fa){//v!=fa确保了(u,v)是从u到v的反向边
lowu=min(lowu,pre[v]);
}
}
if(fa<0 && child==1 )
iscut[u]=false;//u若是根且孩子数<=1,那u就不是割顶
return low[u]=lowu;
}
void work(int n,int m){
// input & initialize
dfs_clock=0;//初始化时钟
memset(pre,0,sizeof(pre));
memset(iscut,0,sizeof(iscut));
memset(cut, 0, sizeof(cut));
memset(in, 0, sizeof(in));
Bridge.clear();
for(int i=1;i<=n;i++) G[i].clear();
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);//下标1~n
G[u].push_back(v);
G[v].push_back(u);
}

//Apllication
int k = 0;
for (int i=1; i<=n; i++) {
if (!pre[i]) {
k++;
dfs(i);//每次遍历一个连通块
}
}

//统计入度
for (int i=1; i<=n; i++) {
for (int j=0; j<G[i].size(); j++) {
int v = G[i][j];
if (low[i]!=low[v]) {
in[low[v]]++;
}
}
}

int leaf=0;
for (int i=1; i<=n; i++) {
if (in[i] == 1) {
leaf++;
}
}
cout<<(leaf + 1)/2<<"\n";
}
int main(){
int n,m;
while (scanf("%d%d",&n,&m)!=EOF) {
work(n, m);
}
}