【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可。
#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(并查集基础题)的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- 【HDU1856】More is better(并查集基础题)
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- 【HDU2120】Ice_cream's world I(并查集基础题)
查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...
- 【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
随机推荐
- [置顶] 使用struts拦截器+注解实现网络安全要求中的日志审计功能
J2EE项目中出于安全的角度考虑,用户行为审计日志功能必不可少,通过本demo可以实现如下功能: 1.项目中记录审计日志的方法. 2.struts拦截器的基本配置和使用方法. 3.struts拦截器中 ...
- Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询
HQL查询方式 这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的语法差不太多.条件查询.分页查询.连接查询.嵌套查询,写起来与SQL语法基本一致,唯一不同的就是把表名换成了 ...
- qt tablewidget中单个和批量删除代码如下(部分)截图如下
def coltable(self):#行删除 row=self.downwidget.currentRow() select=self.downwidget.isItemSelected ...
- PHP设计模式笔记三:三种基本设计模式(工厂模式、单例模式、注册树模式) -- Rango韩老师 http://www.imooc.com/learn/236
一.工厂设计模式 index.php $db = IMooc\Factory::createDatabase(); 使用工厂类的静态方法直接创建一个dababase对象,当类名发生修改时,在工厂里修改 ...
- js方法调用
<!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...
- DEV GridControl 鼠标单击事件
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { ...
- get请求与post请求
1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...
- C# 获取远程xml文件
/// <summary> /// 加载远程XML文档 /// </summary> /// <param name="URL"></pa ...
- Oracle内链接+外连接详解
inner join(内连接) 内连接也称为等同连接,返回的结果集是两个表中所有相匹配的数据,而舍弃不匹配的数据.也就是说,在这种查询中,DBMS只返回来自源表中的相关的行,即查询的结果表包含的两源表 ...
- python字符串的encode和decode
原文 decode的作用是将其他编码的字符串转换成unicode编码. str1.decode('gb2312') #表示将gb2312编码的字符串转换成unicode编码 encode的作用是将un ...