-----------刷点水题练习java-------------

题意:给定N点,M边的无向图,问有多少个连通块。

思路:可以搜索;  可以并查集。这里用并查集练习java的数组使用,ans=N,合并一个连通块ans--; 以及函数的调用:

经验1:C++声明数组是int fa[1024];而java则是int[] fa=new int[1024];

经验2:不加static是非静态函数,访问需要new出该类的对象来调用,加上static是静态函数 可直接访问或者通过类名访问。

import java.util.Scanner;

/*
@author nimphy
@create 2019-11-04-16:14
about:
*/
public class Main {
    static int[] fa = new int[1010];

    public static void main(String[] args) {
        Scanner scan;
        int N, M, ans, x, y, T;
        scan = new Scanner(System.in);
        T = scan.nextInt();
        while (T-- > 0) {
            N = scan.nextInt();
            M = scan.nextInt();
            ans = N;
            for (int i = 1; i <= N; i++) fa[i] = i;
            for (int i = 1; i <= M; i++) {
                x = scan.nextInt();
                y = scan.nextInt();
                x = find(x);
                y = find(y);
                if (x != y) {
                    fa[x] = y;
                    ans--;
                }
            }
            System.out.println(ans);
        }
    }

    static int find(int x) {
        if (x == fa[x]) return x;
        return fa[x] = find(fa[x]);
    }
}

HDU1213:How Many Tables(并查集入门)的更多相关文章

  1. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

  3. hdu1272并查集入门

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. 并查集入门--畅通工程(HDU1232)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    M ...

  5. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  6. C - How Many Tables 并查集

    Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...

  7. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...

  8. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  9. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

随机推荐

  1. Spring Boot 2.2.1 正式发布,需特别注意这个注解的使用!

    Spring Boot 2.2.1 已于2019年11月7日正式发布. 该版本内容包含110项修复.改进和依赖升级. 如果开发者要从Spring Boot 2.2.0升级到2.2.1的话,这里要特别注 ...

  2. Linux学习笔记-第16天 这些个配置参数好饶阿

    原理是懂了,但是配置参数好多阿,难道这些都要记么...呃

  3. css3之水波效果

    这些效果可谓多种多样,当然用canvas.svg也都能实现奈何对这些有不熟悉(尴尬),不过咱们用css来写貌似也没想象中的那么难吧. 一  悬浮球水波效果 效果图 css .container { w ...

  4. 内核发送uevent的API,用户空间解析uevent(转)

    #include <stdio.h>#include <string.h>#include <sys/types.h>#include <unistd.h&g ...

  5. SiIsEnterpriseFunctionsRestrictedOnOpenSource

    src/Cedar/Server.c SiIsEnterpriseFunctionsRestrictedOnOpenSource()

  6. tornado的使用-日志篇

    tornado的使用-日志篇

  7. TP框架where条件和whereOr条件同时使用

    前言:where里面的条件是 && 的关系,whereOr里面的条件是 | | 的关系, 想要得到的效果: 1.筛选出is_deleted字段为0(未删除)的公告 2.筛选出全部状态为 ...

  8. Python保存json文件并格式化

    使用json.dump()的时候设置一下indent参数的值就好了.比如json.dump(json_dict, f, indent=4), ensure_ascii=False,写入中文

  9. Kubernetes service 代理模式

    Kubernetes service 代理模式 底层流量转发与负载均衡实现:• Iptables(默认)• IPVS IPVS 了解代理模式之IPVS工作原理LVS 基于 IPVS内核调度模块实现的负 ...

  10. 【转载】百度百科:FusionCube超融合

    [转载]百度百科:FusionCube超融合 华为FusionCube融合基础设施一体机(Huawei FusionCube Converged Infrastructure)是华为公司IT产品线云计 ...