[One question per day] Niu Ke: Calculate Moments (and collect exercise 01)

Title description ( portal )

Suppose there are N users, some of whom are friends and some are not. A and B are friends, B and C are friends, so ABC is a circle of friends, please count the number of circles of a given friend relationship.
Given an N * N matrix M, it represents the friend relationship between users. If M[i][j] = 1, it means that the i-th and j-th persons are known to be friends with each other, otherwise it is not known. You must output the total number of known Moments in all users.

输入描述:
第一行输入为表示用户数N
第2到1+N行表示朋友之间关系,如果值为1表示有朋友关系


输出描述:
输出朋友圈数

Example 1

输入
3
1,1,0
1,1,0
0,0,1
输出
2

说明
第0个用户和第1个用户组成一个朋友圈,第2个用户组成一个朋友圈

Example 2

输入
3
1,1,0
1,1,1
0,1,1
输出
1
说明
第012个用户组成了一个朋友圈

Problem solving ideas & code implementation

I studied and checked the collection yesterday, and today I will contact you by hitting the iron while it is hot.
I won’t say more about the combined search. You can view: [High-level data structure] Detailed explanation of combined search

import java.util.Scanner;

/**
 * @ClassName Test
 * @Description :TODO
 * @Author Josvin
 * @Date 2021/01/15/21:19
 */
public class Test {
    
    
    public static int count ;
    private static int[] id;
    private static int[] size;
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int num = Integer.parseInt(s);
        int[][] M = new int[num][num];
        //牛客上练习,对输入输出的处理要一定熟练,自己感觉处理的不是特别好,如果有更好的方法可以交流交流
        for (int i = 0; i < num; i++) {
    
    
            String[] str = scanner.nextLine().split(",");
            for (int j = 0; j < num; j++) {
    
    
                M[i][j] = Integer.parseInt(str[j]);
            }
        }
        Union_Find(M); //调用并查集
        System.out.println(count);

    }
    private static void Union_Find(int[][] M) {
    
    
        int N = M.length;// M的长度也就是总共的人数,也就是上个博客提到的集合总数
        id = new int[N];// id 定义一个集合,大小为 N,也就是总共有N个人
        count = N;// 朋友圈的数目,刚开始每个人就是一个朋友圈,初始化为N
        size = new int[N];// 记录每个朋友圈的大小,在合并时用
        // 初始化id数组,每个人都是一个朋友圈,将置为不同的数即可
        // size 刚开始每个人都是一个朋友圈,他们的大小也就是1
        for (int i = 0; i < N; i++) {
    
    
            id[i] = i;
            size[i] = 1;
        }
        //合并,将M数组元素,为1的,合并两者i和j
        for (int i = 0; i < N; i++) {
    
    
            for (int j = 0; j < N; j++) {
    
    
                if (M[i][j] == 1) {
    
    
                    Union(i,j);
                }
            }
        }
    }
    // 合并
    private static void Union(int i, int j) {
    
    
        int root1 = Find(i);
        int root2 = Find(j);
        if (root1 == root2) {
    
    
            return;
        }
        if (size[i] > size[j]) {
    
    
            id[root1] = root2;
            size[root2] += size[root1];
        } else {
    
    
            id[root2] = root1;
            size[root1] += size[root2];
        }
        count--;
    }
    // 查找
    private static int Find(int p) {
    
    
        if (p != id[p]) {
    
    
            id[p] = Find(id[p]);
        }
        return id[p];

    }
}

Guess you like

Origin blog.csdn.net/weixin_45532227/article/details/112689400