poj3177 Redundant Paths

时间:2022-08-13 16:35:00

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.

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.

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

Hint

Explanation of the sample:

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.

 
模拟赛第二题居然考边双联通分量……我图论很差的啊
题意是给一个连通图,最少添加多少条边,使得任意两点之间有两条无公共边的路(可以有公共点)
这题有个结论的……若tarjan缩完点后所有叶节点的个数是x,那么答案是(x+1)/2
这个要画图理解,有点麻烦。(这时候你就需要善良的学长)
总之就是每次选取lca最大的两个点连无向边,一直到叶节点搞完为止。+1是因为如果两两配对完还剩一个还要再连一条
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{int to,next;}e[1000010];
int n,m,cnt=1,cnt3,tt,sum;
int head[100010];
int dfn[100010],low[100010],belong[100010];
int zhan[100010],top;
bool inset[100010];
int I[100010],O[100010];
inline void ins(int u,int v)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v)
{
ins(u,v);
ins(v,u);
}
inline void dfs(int x,int fa)
{
zhan[++top]=x;inset[x]=1;
dfn[x]=low[x]=++tt;
for(int i=head[x];i;i=e[i].next)
if (i!=(fa^1))
if (!dfn[e[i].to])
{
dfs(e[i].to,i);
low[x]=min(low[x],low[e[i].to]);
}else if (inset[e[i].to])low[x]=min(low[x],dfn[e[i].to]);
if (low[x]==dfn[x])
{
cnt3++;
int p=-1;
while (p!=x)
{
p=zhan[top--];
belong[p]=cnt3;
inset[p]=0;
}
}
}
inline void tarjan()
{
for (int i=1;i<=n;i++)if (!dfn[i])dfs(i,0);
}
int main()
{
n=read();m=read();
for (int i=1;i<=m;i++)
{
int x=read(),y=read();
insert(x,y);
}
tarjan();
for (int i=1;i<=n;i++)
for (int j=head[i];j;j=e[j].next)
if (belong[i]!=belong[e[j].to])
{
O[belong[i]]++;
I[belong[e[j].to]]++;
}
for (int i=1;i<=cnt3;i++)
if (I[i]==1)sum++;
printf("%d\n",(sum+1)/2);
}