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.
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.
Line 1: A single integer that is the number of new paths that must be built.
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
2
Explanation of the sample:
One visualization of the paths is:
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
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.
USACO 2006 January Gold
/*算法思想: 给一个无向图,问至少添加多少条边才能使得每两个点之间存在至少两条路径相互到达 首先可以把原图中的联通分量缩成一个点,这样,原图实际上就变成了一棵树,我们找到树上的叶子 节点的个数sum,最终我们要添加的边的个数就是 (sum+1)/2 ,这样问题就变成了怎么找原图中缩点 后叶子节点的个数。观察到,缩点的后,还保留在图中的边,一定是原图中的桥,新图的即缩点后得 到的树就是由这些桥构成的,那么问题再次转化成了找原图中的桥,我们可以用tarjan找出原图中的 桥,在找桥的图中顺便把在一个联通分量中的点缩成一个点,缩点可以用并查集来做。最后我们统计 每个桥的端点的度数,度数是1,这个端点是叶子节点,sum+1 */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 5005 using namespace std; int fa[N]; struct data { int en,val,next; } edge[N*4]; int head[N],tot,low[N],dfn[N],bridge[N][3],d[N],n,m,Time; int find_set(int x) { if(x!=fa[x]) fa[x]=find_set(fa[x]); return fa[x]; } void add_edge(int st, int en) { edge[tot].en=en; edge[tot].next=head[st]; head[st]=tot++; } void find_bridge(int v,int from) //tarjan找桥 { low[v]=dfn[v]=Time++; for(int i=head[v];i!=-1;i=edge[i].next) { if(dfn[edge[i].en]<0) { find_bridge(edge[i].en,v); low[v]=min(low[v],low[edge[i].en]); if(low[edge[i].en]<=dfn[v]) { int f1=find_set(edge[i].en); int f2=find_set(v); if(f1!=f2) fa[f1]=f2; } else { tot++; bridge[tot][0]=v; bridge[tot][1]=edge[i].en; } } else if(edge[i].en!=from && low[v]>dfn[edge[i].en]) low[v]=dfn[edge[i].en]; } } int main() { tot=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); add_edge(a,b); add_edge(b,a); } for(int i=1;i<=n;i++) fa[i]=i; memset(dfn,-1,sizeof(dfn)); memset(low,0,sizeof(low)); Time=1; tot=0; find_bridge(1,-1); int sum=0; memset(d,0,sizeof(d)); for(int i=1;i<=tot;i++) { int f1=find_set(bridge[i][0]); int f2=find_set(bridge[i][1]); d[f1]++; d[f2]++; } for(int i=1;i<=n;i++) if(d[i]==1) sum++; printf("%d\n",(sum+1)/2); return 0; }