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 city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
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
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
思路:
有两种解决方法:
1. dfs
2. 并查集
我用java写的代码,发现这两种方法都不能通过最后一个节点,都超时了,如果谁有更好的实现方法,可以教我一下。
dfs代码:
import java.util.*;
public class Main {
static int[][] map = new int[1001][1001];
static boolean[] visit = new boolean[1001];
static int n = 0;
static public void dfs(int start) {
visit[start] = true;
for(int i=1;i<=n;i++) {
if(visit[i] == false && map[start][i] == 1)
dfs(i);
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int i = 0, j = 0;
n = cin.nextInt();
int m = cin.nextInt();
int k = cin.nextInt();
for(i=0;i<m;i++) {
int x = cin.nextInt();
int y = cin.nextInt();
map[x][y] = map[y][x] = 1;
}
for(int s=0;s<k;s++) {
int cnt = 0;
Arrays.fill(visit, false);
int temp = cin.nextInt();
visit[temp] = true;
for(i=1;i<=n;i++) {
if(visit[i] == false) {
dfs(i);
cnt++;
}
}
System.out.println(cnt - 1);
}
}
}
并查集代码:
import java.util.Scanner;
public class Main {
static int count;
private static int[] id;
private static int[] size;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int citys = sc.nextInt(), roads = sc.nextInt(), checked = sc.nextInt();
if(citys<3){
System.out.println(0);
sc.close();
return;
}
id = new int[citys + 1];
size = new int[citys + 1];
int[][] road = new int[roads][2];
for (int i = 0; i < roads; i++) {
road[i][0] = sc.nextInt();
road[i][1] = sc.nextInt();
}
for (int i = 0; i < checked; i++) {
int check = sc.nextInt();
init();
solve(check, road);
}
sc.close();
}
private static void init() {
for (int i = 1; i < id.length; i++) {
id[i] = i;
size[i] = 1;
}
count = id.length-1;
}
private static void solve(int root, int[][] road) {
for (int i = 0; i < road.length; i++) {
int p = road[i][0], q = road[i][1];
if (p == root || q == root)
continue;
union(p,q);
}
System.out.println(count()-2);
}
private static int find(int p) {
int r = p;
while(id[r]!=r)
r=id[r];
int i = p, j = 0;
while(id[i] != r) {
j = id[i];
id[i] = p;
i = j;
}
return r;
}
private static void union(int p, int q) {
int pRoot=find(p),qRoot=find(q);
if(pRoot==qRoot)
return;
if(size[pRoot]>size[qRoot]){
id[qRoot]=pRoot;
size[pRoot]+=size[qRoot];
}else{
id[pRoot]=qRoot;
size[qRoot]+=size[pRoot];
}
count--;
}
private static int count() {
return count;
}
}