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

 #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. hdu 5159 Card (期望)

    Problem Description There are x cards on the desk, they are numbered from 1 to x. The score of the c ...

  2. java 自定义鼠标图标

    由于截图截不了,所以看不了图.源码如下: import java.awt.Cursor; import java.awt.Image; import java.awt.Point; import ja ...

  3. linux core dump学习

    1. core dump是什么? core dump又叫核心转储,当操作系统收到特定的signal时, 会生成某个进程的core dump文件.这样程序员可以根据 已经生成的core dump文件来d ...

  4. jsp filter登录限制过滤器

    http://www.cnblogs.com/hemingwang0902/archive/2012/01/09/2316956.html UserFilter.java package filter ...

  5. UITableView滑动按钮的操作

    一.开题  首先先创建工程, 创建工程的步骤就不一一介绍了, 前面有提过, 接下来是要在VC上创建一个tableview当然你也可以选择一个类继承于UITableView两者都可以, 这要看个人喜欢了 ...

  6. MySQL复制协议

    http://hamilton.duapp.com/detail?articleId=27

  7. UITableView 总结

    http://my.oschina.net/iq19900204/blog/292125

  8. html5新特性--音频视频,拖放

    1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...

  9. [总结]Map: C++ V.S. Java

    整理一下Map在Java 和 C++的基本操作,欢迎大家一起交流学习. 附: 对于在C++中,选用map 还是 unordered_map,可以参考这篇讨论.相对简单粗暴的结论是,unordered_ ...

  10. 阿里云Centos7使用yum安装MySQL5.6的正确姿势

    阿里云Centos7使用yum安装MySQL5.6 阿里云Centos7使用yum安装MySQL5.6 前言:由于某些不可抗力,我要在自己的阿里云服务器上搭建hadoop+hive+mysql+tom ...