并查集集问题

模板


import java.util.Scanner;

public class BING {
    static int fa[];
    public static void main(String[] args) {
          int n ,m,x,y;

        Scanner scanner =new Scanner(System.in);
       n=scanner.nextInt();
       m=scanner.nextInt();

        fa=new int[n+1];
        init(n);
        for (int i = 1; i <=m; i++) {
            //自身指向自己的节点
           x=scanner.nextInt();
           y=scanner.nextInt();
           union(x,y);
        }
        int q=scanner.nextInt();
        for (int i = 0; i < q; i++) {

            if (find(scanner.nextInt())!=find(scanner.nextInt())){
                System.out.println("NO");
            }else {
                System.out.println("YES");
            }
        }

    }
    //路径压缩的代码
    static void init(int n){
        for (int i = 1; i <=n; i++) {
            fa[i]=i;
        }
    }
    static int find(int i){
        if (fa[i]==i){ //如果父节点是自身的话,直接返回就行了
            return i;
        }
        else{
            fa[i]=find(fa[i]);//将i的父节点加入进来,进行递归
            return fa[i];//是i的父节点
        }
    }
    //通过这一段代码,将所有的节点都指向了一个父节点
   static void union(int i,int j){

        int i_fa=find(i);

        int j_fa=find(j);

         fa[i_fa]=j_fa;
    }
}

蓝桥杯题目


import java.util.Scanner;

public class 合根植物 {

    static int m,n;
     static  int f[] =new int[1000010];
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int x,y,line;
        m=scanner.nextInt();
        n=scanner.nextInt();
        line =scanner.nextInt(); //连线的个数
        init(m*n);
        //和跟植物

        for (int i = 0; i < line; i++) {
            x = scanner.nextInt();
            y = scanner.nextInt();
            unite(x, y); // 将连线两端的点进行联合
        }

        int ans=0;
        int [] a=new int[1000010]; //记录根节点的数组
        for (int i = 0; i 

国王的烦劳

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class 国王的烦劳 {
    static class Bridge{
        int x,y;
        int day;
        Bridge(int a,int b,int c){
            x=a;
            y=b;
            day=c;
        }
    }
    static final  int N=10010;
    static  final  int M=100010;
    static  Bridge[] bridge=new Bridge[M]; //存储所有的桥

    static int [] pre=new int[N]; //存储每个岛上的上级

    static void init(int n){
        for (int i = 1; i <= n; i++)
            pre[i] = i;
    }
    static int findPre(int n) {
        if (pre[n] == n) return n;
        else return pre[n] = findPre(pre[n]);
    }

    static boolean unite(int x, int y) {
        int rootX = findPre(x);
        int rootY = findPre(y);
        if (rootX != rootY) {
            pre[rootX] = rootY;
            return true;
        } else return false;
    }
    static class BridgeComparator implements Comparator {
        @Override
        public int compare(Bridge a, Bridge b) {
            return b.day - a.day;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        init(n);//n是岛的数量
        for (int i = 1; i <= m; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            int t = scanner.nextInt();
            bridge[i] = new Bridge(a, b, t);
        }
        //对象数组,根据天数进行排序

        Arrays.sort(bridge, 1, m + 1, new BridgeComparator());
        int ans = 0;
        int lastDay = 0; // lastDay用于探测一次某个桥的生命时限
        for (int i = 1; i <= m; i++) {
            boolean flag = unite(bridge[i].x, bridge[i].y); // 如果为真表示当前这两个岛未联通
          //这两个桥没有联通和,而且2这个桥的天数是第一次出现的
            if (flag && bridge[i].day != lastDay) { // 未连通,且此桥的天数是第一次出现,那么就增加了抗议的天数
                ans++;
                lastDay = bridge[i].day;
            }
        }
        System.out.println(ans);
    }


}

你可能感兴趣的:(java,算法,开发语言)