Redundant Paths
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18196 | Accepted: 7551 |
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 forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.
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.
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
Line 1: Two space-separated integers: F and R
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Line 1: A single integer that is the number of new paths that must be built.
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
2
题意: 给定一个连通的无向图 G,至少要添加几条边,才能使其变为双连通图。
思路:去掉桥,其余的连通分支就是边双连通分支了。一个有桥的连通图要变成边双连通图的话,
把双连通子图收缩为一个点,形成一颗树。需要加的边为 (leaf+1)/2 (leaf 为叶子结点个数)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN = 5010; //点数
const int MAXM = 20010; //边数
struct Edge
{
int to,next;
int cut; //是否被标记
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN]; //数组的值是1~lock
int Index,top;
int block;//边双连通块数
bool Instack[MAXN];
int bridge;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cut = false;
head[u] = tot++;
}
void Tarjan(int u,int pre)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int pre_cnt = 0;
for(int i = head[u]; i != -1; i = edge[i].next) {
v = edge[i].to;
if(v == pre && pre_cnt == 0){
pre_cnt++;
continue;
}
if(!DFN[v]) {
Tarjan(v,u);
Low[u] = min(Low[u],Low[v]);
if(Low[v] > DFN[u]) {
bridge++;
edge[i].cut = true;
edge[i ^ 1].cut = true;
}
}
else if(Instack[v] && Low[u] > DFN[v]) {
Low[u] = DFN[v];
}
}
if(Low[u] == DFN[u]) {
block++;
do {
v = Stack[--top];
Instack[v] = false;
Belong[v] = block;
}while(v != u);
}
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
int du[MAXN];
void solve(int n)
{
memset(DFN,0,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index = top = block = 0;
Tarjan(1,0);
int ans = 0;
memset(du,0,sizeof(0));
for(int i = 1; i <= n; i++) {
for(int j = head[i]; j != -1; j = edge[j].next) {
int v = edge[j].to;
if(Belong[i] != Belong[v]) {
//printf("%d %d\n",i,v);
du[Belong[i]]++;
du[Belong[v]]++;
}
}
}
for(int i = 1; i <= block; i++) {
if(du[i] == 2) {
ans++;
}
}
printf("%d\n",(ans + 1) / 2);
}
int main(void)
{
int n,m;
int u,v;
while(scanf("%d%d",&n,&m) != EOF) {
init();
while(m--) {
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n);
}
return 0;
}