• Java实现蓝桥杯 历届试题 合根植物


    问题描述
      w星球的一个种植园,被分成 m * n 个小格子(东西方向m行,南北方向n列)。每个格子里种了一株合根植物。
      这种植物有个特点,它的根可能会沿着南北或东西方向伸展,从而与另一个格子的植物合成为一体。

    如果我们告诉你哪些小格子间出现了连根现象,你能说出这个园中一共有多少株合根植物吗?
    输入格式
      第一行,两个整数m,n,用空格分开,表示格子的行数、列数(1<m,n<1000)。
      接下来一行,一个整数k,表示下面还有k行数据(0<k<100000)
      接下来k行,第行两个整数a,b,表示编号为a的小格子和编号为b的小格子合根了。

    格子的编号一行一行,从上到下,从左到右编号。
      比如:5 * 4 的小格子,编号:
      1 2 3 4
      5 6 7 8
      9 10 11 12
      13 14 15 16
      17 18 19 20
    样例输入
    5 4
    16
    2 3
    1 5
    5 9
    4 8
    7 8
    9 10
    10 11
    11 12
    10 14
    12 16
    14 18
    17 18
    15 19
    19 20
    9 13
    13 17
    样例输出
    5
    样例说明
      其合根情况参考下图

    在这里插入图片描述

    import java.util.Scanner;
    
    
    public class Main {
    	static int father[] = null;
    	public static void main(String[] args) {
    		
    		Scanner s = new Scanner(System.in);
    		int m = s.nextInt();
    		int n = s.nextInt();
    		int k = s.nextInt();
    		father = new int[m*n+1];
    		for(int i=1;i<=m*n;i++){
    			father[i] = i;
    		}
    		for(int i=0;i<k;i++){
    			int a = s.nextInt();
    			int b = s.nextInt();
    			join(a,b);
    		}
    		int cnt = 0;
    		for(int i=1;i<=m*n;i++){
    			if(father[i]==i){
    				cnt++;
    			}
    		}
    		System.out.println(cnt);
    		      
    	}
    	public static void join(int a,int b){
    		if(getTop(a) != getTop(b)){
    			father[getTop(a)]= getTop(b);
    		}
    	}
    	public static int getTop(int a){
    		if(father[a]==a){
    			return a;
    		}else{
    			father[a] = getTop(father[a]);
    			return father[a];
    		}
    	}
    
    }
    
    
    import java.util.Scanner;
    
    
    public class hegenzhiwu {
    	public static void main(String[] args) {
    		Scanner sc =new Scanner(System.in);
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int a,b,c,d;
    		int [] shu = new int [m*n+1];
    		int [][] num = new int [n][m];
    		int  k =sc.nextInt();
    		for (int i = 0; i < shu.length; i++) {
    			shu[i]=i;
    		}
    		for (int i = 0; i < k; i++) {
    			 c = sc.nextInt();
    			 d =sc.nextInt();
    			 a=Math.min(c, d);
    			 b=Math.max(c, d);
    			if(shu[b]==b){				
    			shu[b]=shu[a];
    			}
    			else{
    				shu[a]=shu[b];
    			}
    		}
    		int count = 0;
    		for (int i = 1; i < shu.length; i++) {
    			if(shu[i]==i){
    				count++;
    			}
    		}
    		System.out.println(count);
    	}
    
    }
    
    
  • 相关阅读:
    Oracle ORA07445 [evaopn3()+384] 错误 分析
    Openfiler iscsiadm: No portals found 解决方法
    Openfiler iscsiadm: No portals found 解决方法
    ORA00600 [kmgs_parameter_update_timeout_1], [27072] ORA27072 解决方法
    Oracle 安装 Error in writing to directory /tmp/OraInstall 错误 说明
    Oracle alert log ALTER SYSTEM SET service_names='','SYS$SYS.KUPC$C_...' SCOPE=MEMORY SID='' 说明
    Oracle latch:library cache 导致 数据库挂起 故障
    ORA600 [4194] 说明
    ORA00600:[32695], [hash aggregation can't be done] 解决方法
    Oracle 10g Rac root.sh Failure at final check of Oracle CRS stack 10 解决方法
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948963.html
Copyright © 2020-2023  润新知