2013
11-12

# POJ 3352 Road Construction [解题报告] Java

It’s almost summer time, and that means that it’s almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3

Output for Sample Input 1
2

Output for Sample Input 2
0


//* @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. 算法是程序的灵魂，算法分简单和复杂，如果不搞大数据类，程序员了解一下简单点的算法也是可以的，但是会算法的一定要会编程才行，程序员不一定要会算法，利于自己项目需要的可以简单了解。