2013
11-12

# Redundant Paths

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:

   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.

//* @author: [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
import java.io.*;
public class Main
{
static int a,cnt=1;
static int[] rank,un;
static boolean[][] map;
public static void main(String[] args) throws NumberFormatException, IOException
{
a=Integer.parseInt(ss[0]);
map=new boolean[a+1][a+1];
rank=new int[a+1];
un=new int[a+1];
for(int i=1;i<=a;i++)
un[i]=i;
int b=Integer.parseInt(ss[1]);
for(int i=0;i< b;i++)
{
int x=Integer.parseInt(ss[0]);
int y=Integer.parseInt(ss[1]);
map[x][y]=map[y][x]=true;
}
dsf(1,1,rank,un);
int cnt=0;
for(int i=1;i<=a;i++)
rank[i]=0;
for(int i=1;i<=a;i++)
{
int yy=root(i,un);
for(int j=1;j<=a;j++)
{
if(!map[i][j]) continue;
int ww=root(j,un);
if(ww!=yy)
{
rank[yy]++;
if(rank[yy]>1) break;
}
}
}
for(int i=1;i<=a;i++)
{
if(rank[i]==1)cnt++;
}
System.out.println((cnt+1)/2);
}

static void dsf(int n,int pre,int[] rank,int[] un)
{
if(rank[n]!=0)
{
for(int i=1;i<=a;i++)
if(rank[i]>rank[n]) union(i,n,un);
}
else
{
rank[n]=cnt++;
for(int i=1;i<=a;i++)
{
if(map[i][n]&&i!=pre&&root(i,un)!=root(n,un))
dsf(i,n,rank,un);
}
rank[n]=0;
}
}

static void union(int a,int b,int[] un)
{
int x1=root(a,un);
int y1=root(b,un);
if(x1>y1)un[x1]=y1;
else un[y1]=x1;
}

static int root(int a,int[] un)
{
int b=a;
while(b!=un[b])
b=un[b];
return un[a]=b;
}
}

1. a是根先忽略掉，递归子树。剩下前缀bejkcfghid和后缀jkebfghicd，分拆的原则的是每个子树前缀和后缀的节点个数是一样的，根节点出现在前缀的第一个，后缀的最后一个。根节点b出现后缀的第四个位置，则第一部分为四个节点，前缀bejk，后缀jkeb，剩下的c出现在后缀的倒数第2个，就划分为cfghi和 fghic，第3部分就为c、c