什么也不用说,并查集裸题,直接盲敲即可。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric>
using namespace std; int father[]; int getFather (int x) {
if (x != father[x]) {
father[x] = getFather(father[x]);
}
return father[x];
} void Union (int p, int q) {
int x = getFather (p);
int y = getFather (q);
if (x != y) {
father[y] = x;
}
} void Init (int n) {
for (int i = ; i <= n; ++ i) {
father[i] = i;
}
} int main () {
int T; cin >> T;
while (T --) {
int n, op_n;
cin >> n >> op_n;
Init(n);
for (int i = ; i < op_n; ++ i) {
int x, y; cin >> x >> y;
Union(x, y);
}
int cnt = ;
for (int i = ; i <= n; ++ i) {
if (father[i] == i) {
cnt ++;
}
}
cout << cnt << endl;
}
return ;
}
 import java.util.*;
import java.io.*;
import java.math.*; class DisjointSet{
public static int MAXX = 1005;
public int ans = 0;
public int father[] = new int[MAXX];
public int vis[] = new int[MAXX]; public DisjointSet(){
this.ans = 0;
} public int GetAns(){
return ans;
}
public int GetFather(int x){
if(father[x] != 0){
return GetFather(father[x]);
}
else{
return x;
}
} public void Union(int x, int y){
int fx = GetFather(x);
int fy = GetFather(y);
if(fx != fy){
father[fy] = fx;
ans++;
}
}
} public class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int l = in.nextInt();
int a, b, n, opn;
for(int i = 0; i < l; i++){
n = in.nextInt();
DisjointSet D = new DisjointSet();
opn = in.nextInt();
for(int j = 0; j < opn; j++){
a = in.nextInt();
b = in.nextInt();
D.Union(a, b);
}
System.out.println(n - D.GetAns());
}
}
}

【HDU1231】How Many Tables(并查集基础题)的更多相关文章

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

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

  2. 【HDU1232】畅通工程(并查集基础题)

    裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  3. 【HDU1856】More is better(并查集基础题)

    裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...

  4. 【HDU1272】小希的迷宫(并查集基础题)

    仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...

  5. 【HDU2120】Ice_cream's world I(并查集基础题)

    查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...

  6. 【HDU1325】Is It A Tree?(并查集基础题)

    有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...

  7. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  8. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  9. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

随机推荐

  1. poj 2976 Dropping tests (二分搜索之最大化平均值之01分数规划)

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

  2. c++ 依据输入动态声明数组(一维,二维)

    较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #includ ...

  3. Android开发有用的站点

    在github上面找到一个个人认为比較好的站点,好在能够方便下载开发工具.我的AndroidStudio就是在上面下载的.安装了一直在使用.该 网址主要收集整理Android开发所需的Android ...

  4. nyoj201 作业题

    作业题 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 3   描述 小白同学这学期有一门课程叫做<数值计算方法>,这是一门有效使用数字计算机求数学问题近似解的方 ...

  5. Hexo博客搭建图文教程

    准备 你需要准备好以下软件: Node.js环境 Git Windows 配置Node.js环境 下载Node.js安装文件: Windows Installer 32-bit Windows Ins ...

  6. webpack的配置及使用

    webpack 安装 命令行输入 npm install webpack 配置文件 webpack.config.js moudule.exports = { //Import 入口文件 entry: ...

  7. iphone开发 IOS 组织架构图

    转载自 :http://blog.csdn.net/mashi321323/article/details/18267719   登录|注册     mashi321323的专栏       目录视图 ...

  8. .NET基础拾遗(5)反射2

    本篇是学习反射的一个应用小场景而做的学习笔记,主要是一个小的总结,并对各个步骤的记录,以便将来回顾. 一.基础框架-敏捷基础版本 这里假定我们要开发一个记事本,选择Windows Form技术开发,界 ...

  9. 变量a的定义

    1.一个整型数 int a; 2.一个指向整型数的指针 int *a; 3.一个指向指针的指针,它指向的指针是指向一个整形数 int **a; 4.一个有10个整型数的数组 ]; 5.一个有10个指针 ...

  10. c++11-bind的用法

    bind函数 在c++11之前,要绑定某个函数.函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st.bind2nd.fun_ptr.mem_fun和mem_fun_ref等.在c+ ...