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

 #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. 如何安装CocoaPods

    转自 http://www.99css.com/1321/ 在 iOS 项目开发中,经常会用到第三方的源代码,CocoaPods 就是为了方便管理这些源码的工具. 在官方教程里面,安装看起来非常简单 ...

  2. HTML5新增的一些属性和功能之一

    大致可以分为10个方面: HTML5表单元素和属性 表单2.0 视音频处理 canvas绘图 SVG绘图 地理定位 拖放技术 web work web storage web socket 一.新的i ...

  3. [原创作品] javascript 实现的web分页器原理

    很久没有写博客了,因为最近忙于一些杂七杂八的事情.不过,互联网的价值在于信息共享,因为共享,所以互联网才能飞快发展.博主建了一个技术共享qq群:164858883,因为目前人数还比较少,活跃度还不是很 ...

  4. Android_HTML解析器_jsoup

    jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. Jsou ...

  5. Smack+Openfire 接收和发送文件

    转载请注明出处:http://blog.csdn.net/steelychen/article/details/37958839 发送文件须要提供准确的接收放username称(例:user2@192 ...

  6. Oracle用户解锁的三种办法及默认的用户与密码

    ORA-28000: the account is locked-的解决办法 2009-11-11 18:51 ORA-28000: the account is locked 第1步:使用PL/SQ ...

  7. 设置单选的listView或者gridview

    主要是这个BeaseAdapter的方法notifyDataSetChanged()的使用;作用 :调用BaseAdapter中的getView();方法,刷新ListView中的数据.实现:1.在B ...

  8. javascript什么是函数

    函数是完成某个特定功能的一组词语.如没有函数,完成任务可能需要五行.十行.甚至更多的代码. 这是未满就可以把完成特定功能的代码块放到一个函数里,直接调用这个函数,就省重复输入大量代码的麻烦. 如何定义 ...

  9. angular 指令梳理 —— 前端校验

    angular js内置校验的扩展 校验成功则 scope.formName.$valid=true 校验失败  元素的class: ng-invalid 成功:.ng-valid /** * 校验指 ...

  10. 虚函数—c++的灵魂

    <note_content />   虚函数 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数 ...