Description
Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
One visualization of the paths is:
1 2 3
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
1 2 3
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -
Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
题解:把F个农场看作点、路看作边构造一个无向图G时,图G不存在桥。
也就是问给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
把每一个双连通分量(内部满足条件)缩为一个点,形成一棵树,加(n+1)/2条边就是双连通了(度为1的点个数为n)
注意:判断两个点是不是同一个双连通分量
1.无重边:low值相等就是同一个双连通分量
2.有重边:bfs结束时出栈的就是同一连通分量,好像有点麻烦
这里加了一个判断,不加重边
#include<stdio.h>
#include <algorithm>
#include <string.h>
#define N 5005
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
struct ed{
int to, next;
}edge[N*];
int head[N], top=;
bool mp[N][N];
int pre[N], low[N], dfs_time, out[N];
void addedge(int u,int v){
edge[top].to = v;
edge[top].next = head[u];
head[u] = top++;
}
void dfs(int u,int father){
low[u] = pre[u] = dfs_time++;
for(int i=head[u];i!=-;i=edge[i].next){
int v = edge[i].to;
if(v == father) continue;
if(!pre[v]){
dfs(v, u);
low[u] = min(low[v], low[u]);
}
else low[u] = min(low[u], pre[v]);
}
}
int main(){
int n, m, t, i, j, a, b;
while(~scanf("%d%d", &n, &m)){
memset(head, -, sizeof(head));
memset(pre,,sizeof(pre));
memset(low, , sizeof(low));
memset(out, , sizeof(out));
memset(mp, false, sizeof(mp));
dfs_time = ;top = ;
for(i=;i<=m;i++){
scanf("%d%d", &a, &b);
if(!mp[a][b]){
mp[a][b] = mp[b][a] = ;
addedge(a, b);
addedge(b, a);
}
}
dfs(,-);
t = ;
for(i=;i<=n;i++)
for(j=head[i];j!=-;j=edge[j].next){
int v = edge[j].to;
if(low[v] != low[i])
out[low[i]]++;
}
for(i=;i<=n;i++)
if(out[i] == )
t++;
printf("%d\n", (t+)/);
}
}