java并查集_并查集(Java)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:

3 2 3

1 2

1 3

1 2 3

Sample Output:

1

0

0

这道题目是对并查集的一种典型应用,可以转化为:每次占领一个城市,并查集连接时便略过该城市,最后查看有几个互不相干的集合n,

便说明了至少还需要n-1条路,代码如下:

import java.util.*;public classBattleOverCities2 {static int[] weight ;static int[] parent;static intnums ;public static voidmain(String args[])

{

Scanner scanner= newScanner(System.in);int cities =scanner.nextInt();int roads =scanner.nextInt();int checked =scanner.nextInt();int[][] road = new int[roads][2];for(int i = 0 ; i < roads ; i ++)

{

road[i][0] = scanner.nextInt(); //road用来记录有多少条道路

road[i][1] =scanner.nextInt();

}int[] check = new int[checked] ;for(int i = 0 ; i < checked ; i ++)

{

check[i]= scanner.nextInt(); //check[]用来记录被占领的城市

}

scanner.close();for(int i = 0 ; i < checked ; i ++)

{

init(cities);

solve(road,check[i]);

System.out.println(nums-2);

}

}public static void init(intcities)

{

parent= new int[cities+1] ;

nums=cities ;

weight= new int[cities+1] ;for(int i = 1 ; i < cities + 1 ; i ++)

{

parent[i]=i ;

weight[i]= 1;

}

}public static int find(intcitiy)

{while(citiy !=parent[citiy])

{

citiy=parent[parent[citiy]];

}returncitiy ;

}public static void union(int[] road )

{int citiy1 = find(road[0]) ;int citiy2 = find(road[1]) ;if(citiy1 == citiy2) return;if(weight[citiy1] >weight[citiy2])

{

parent[citiy2]=citiy1 ;

weight[citiy1]+=weight[citiy2] ;

}else{

parent[citiy1]=citiy2 ;

weight[citiy2]+=weight[citiy1] ;

}

nums--;

}public static void solve(int[][] road , intcheck)

{for(int i = 0 ; i < road.length ; i ++)

{if(road[i][0] == check || road[i][1] == check) //当需要连接的城市中有一座被占领,则掠过这条路

continue;

union(road[i]) ;

}

}

}

还有一道题目:

1021 Deepest Root (25 分)

A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​4​​) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes' numbers.

Output Specification:

For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components where K is the number of connected components in the graph.

Sample Input 1:

5

1 2

1 3

1 4

2 5

Sample Output 1:

3

4

5

Sample Input 2:

5

1 3

1 4

2 5

3 4

Sample Output 2:

Error: 2 components

这道题目需要求图的生成树的最大深度是多少,首先需要判断图是否都相连,这里用到了并查集,如果互不相连的集合数目大于1,则返回互不相连的

个数,如果为1,则使用dfs找到生成树的最大深度,代码如下:

import java.util.*;public classDeepestRoot {static intnums ;public static voidmain(String args[])

{

Scanner scanner= newScanner(System.in);

nums=scanner.nextInt() ;int[] nodes = new int[nums + 1] ;int[] weight = new int[nums + 1] ;

Map> map = new HashMap<>() ;for(int i = 1 ; i < nums + 1 ; i ++)

{

nodes[i]=i ;

weight[i]= 1;

}int t =nums ;for(int i = 1 ; i < t ; i ++)

{int p =scanner.nextInt() ;int q =scanner.nextInt() ;

union(p,q,nodes,weight);if(!map.containsKey(p))

map.put(p,new ArrayList()) ;if(!map.containsKey(q))

map.put(q,new ArrayList()) ;

map.get(p).add(q) ;

map.get(q).add(p) ;

}

scanner.close();if(nums != 1)

{

System.out.println("Error: "+nums+" components");

}else{int max = 0;

Map> mp = new HashMap<>() ;for(int i = 1 ; i < t + 1 ; i ++)

{if(map.get(i).size() == 1)

{int c = dfs(map,i,0,new int[t+1],0) ;if(c >max)

{

mp.put(c,new ArrayList<>()) ;

mp.get(c).add(i);

max=c ;

}else if(c ==max)

mp.get(max).add(i) ;

}

}

Collections.sort(mp.get(max));

System.out.print(mp.get(max).get(0));for(int i = 1 ; i < mp.get(max).size() ; i ++)

{

System.out.println();

System.out.print(mp.get(max).get(i));

}

}

}public static int dfs(Map> map,int q,int count,int[] checked,intmax)

{

checked[q]= 1;int p = 0;for(int i = 0 ; i < map.get(q).size() ; i ++)

{int t =map.get(q).get(i);if(checked[t] == 1) continue;int l = dfs(map,t,count+1,checked,max) ;if(max < l) max =l ;

p++;

}if(p == 0) returncount ;returnmax ;

}public static int find(int n,int[] parent)

{while(parent[n] !=n)

{

n=parent[n] ;

}returnn ;

}public static void union(int p,int q,int[] nodes,int[] weight)

{int n =find(p,nodes) ;int m =find(q,nodes) ;if(n == m ) return;if(weight[n] >weight[m])

{

nodes[m]=n ;

weight[n]+=weight[m] ;

nums--;

}else{

nodes[n]=m ;

weight[m]+=weight[n] ;

nums--;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值