天梯赛--红色警报--dfs或者并查集



7-9 红色警报(25 分)
战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,则不要发出警报。


输入格式:
输入在第一行给出两个整数N(0 < N ≤ 500)和M(≤ 5000),分别为城市个数(于是默认城市从0到N-1编号)和连接两城市的通路条数。随后M行,每行给出一条通路所连接的两个城市的编号,其间以1个空格分隔。在城市信息之后给出被攻占的信息,即一个正整数K和随后的K个被攻占的城市的编号。


注意:输入保证给出的被攻占的城市编号都是合法的且无重复,但并不保证给出的通路没有重复。


输出格式:
对每个被攻占的城市,如果它会改变整个国家的连通性,则输出Red Alert: City k is lost!,其中k是该城市的编号;否则只输出City k is lost.即可。如果该国失去了最后一个城市,则增加一行输出Game Over.。


输入样例:
5 4
0 1
1 3
3 0
0 4
5
1 2 0 4 3
输出样例:
City 1 is lost.
City 2 is lost.
Red Alert: City 0 is lost!
City 4 is lost.
City 3 is lost.

Game Over.

分析:这个题考察的就是图论的连通分量问题,求解连通分量两种方式:利用图的遍历即深度优先搜索,或者利用并查集实现

方法1:深搜,看代码吧,注释已经写得很详细了:

import java.util.*;
public class Main {
    static Scanner in = new Scanner(System.in);
    static int maxn = 505;
    static int n,m;
    static boolean[] vis= new boolean[maxn];
    static int[][] ma = new int[maxn][maxn];
    static void dfs(int x) {
    	vis[x] = true;
    	for(int i =0;i < n;i++) {
    		if(!vis[i]&&ma[x][i]==1)
    			dfs(i);
    	} 
    }
    static int countNum() {//统计个数
    	int ans = 0;
    	Arrays.fill(vis, false);
    	for(int i = 0;i < n;i++) {
    		if(!vis[i]) {
    			dfs(i);
    			ans++;
    		 }
    	}
    	return ans;
    }
	public static void main(String[] args) {
		n = in.nextInt();
		m = in.nextInt();
		int k,s,e,num,temp,c;
		for(int i = 0;i < m;i++) {
			s = in.nextInt();
			e = in.nextInt();
			ma[s][e] = 1;
			ma[e][s] = 1;
		}
		num = countNum();//记下初始的分量
		k = in.nextInt();
		for(int i = 0;i < k;i++) {
			c = in.nextInt();
			for(int j = 0;j < n;j++)
				if(ma[c][j]>0) {//该点被去掉之后,通向该点的所有边都要去掉
					ma[c][j] = 0;
					ma[j][c] = 0;
			 }
			temp = countNum();
			//每个城市被去掉之后,它本身就是一个连通分量,所以要加一,即 now - 1 >  orginal
			if(temp > num +1 )
				System.out.println("Red Alert: City "+c+" is lost!");
			else
				System.out.println( "City "+c+" is lost.");
			num = temp;
			if(i==n-1)//被攻占的的城市数等于总的城市个数,游戏结束
				System.out.println("Game Over.");
		}			
	}
}

方法二:使用并查集,其实思路都是一样的,只是统计连通分量的方式不一样罢了

注意:这个方法和方法一不一样的是,方法一去掉了点(城市)以后,统计分量的时候这个点是计算在内的,而方法二是去掉这个点的再计算的,所以最后的判断条件有点差别,看代码:

import java.util.*;
public class Main {
    static Scanner in = new Scanner(System.in);
    static int maxn = 505;
    static int n,m;
    static boolean[] vis= new boolean[maxn];
    static int[] f = new int[maxn];
    static Edge[] e = new Edge[maxn];
    static void init() {
       for(int i = 0;i < n;i++)
    		f[i] = i;
    }
    static int find(int x) {
    	int t = x;
    	while(t!=f[t]) {
    		t = f[t];
    	}
    	return t;
    }
    static void Merge(int x,int y) {//统计个数
    	int a = find(x);
    	int b = find(y);
    	if(a!=b)
    		f[a] = b;
    }
    static int CountNum() {
    	int ans = 0;
    	for(int i = 0;i < n;i++)
    		if(!vis[i]&&f[i]==i)
    			ans++;
    	return ans;
    		
    }
	public static void main(String[] args) {
		Arrays.fill(vis, false);
		n = in.nextInt();
		m = in.nextInt();
		int k,x,y,num,temp,c;
		init();
		for(int i = 0;i < m;i++) {
			x = in.nextInt();
			y = in.nextInt();
			e[i] = new Edge(x, y);
			Merge(x, y);
		}
		num = CountNum();//记下初始的分量
		k = in.nextInt();
		for(int i = 0;i < k;i++) {
			init();
			c = in.nextInt();
			vis[c] = true;	
			 for(int j=0 ;j < m ;j++){  
		          if(!vis[e[j].x]&&!vis[e[j].y]) {//去掉被攻占的城市影响的边  
		              Merge(e[j].x,e[j].y);          
		          }  
		      } 
			temp = CountNum();
		 //如果去掉这个点以后连通分量减少或者不变,就不会影响通信,如果增加肯定是影响通信的
			if(temp > num)
				System.out.println("Red Alert: City "+c+" is lost!");
			else
				System.out.println( "City "+c+" is lost.");
			num = temp;
			if(i==n-1)
				System.out.println("Game Over.");
		}			
	}
}

class Edge {
	int x,y;
	Edge(int x,int y){
		this.x = x;
		this.y = y;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值