【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 & ...
随机推荐
- c语言二维数组变色龙之死字的打印
1 #include <stdio.h> #include <stdlib.h> void main() { ][]= { {'#','#','#',' ','#','#',' ...
- mysql 从data文件恢复数据库
安装在D:\mysql\mysql-5.6.24-winx64下的mysql 由于系统坏了,移到另外一台机器上启动 步骤如下 1.复制以前的mysql安装文件及data文件下:2.全新安装mysql3 ...
- spring 通过工厂方法配置Bean
概要: 通过调用静态工厂方法创建Bean 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中.当client须要对象时,仅仅须要简单地调用静态方法,而不用关心创建对象地细节. 要声明通过静 ...
- 超长英文(代码)自动换行的样式(CSS)
如何想让一连串文字在显示可以自动换行,而不会把在代码中使用的容器撑开,则在文章的CSS样式处加上以下代码即可: table-layout: fixed; word-wrap:break-word;或者 ...
- css实现两端对齐~
今天做表单时遇到让上下两个字段对齐的情况,手机号码.用户名. 然后今天在网上找了找相关方法,发现确实是没有什么好的方法解决,特别是当需要兼容的时候.找到了两个我觉得相对还不错的方法: 方法一.是在司徒 ...
- OWIN启动项的检测
OWIN启动项的检测 通过以下方法设置启动项: 命名约定 Katana在命名空间内查找StartUp类 OwinStartup Attribute [assembly: OwinStartup(typ ...
- (转)怎样查看局域网中自己的IP地址和其他电脑的IP地址?
开始菜单->运行->打cmd,回车->再弹出的黑框里打ipconfig -all,回车显示的IP Address就是你的ip地址看局域网的电脑的ip用软件比较方便,比如p2p终结者, ...
- java web实现读取指定盘符下的图像(二)
之前写了一篇文章是关于如何读取指定盘符下的图片,虽然功能可以实现,但是使用的是I/O流的方式,效率不高.现在发现还有一个更好的办法,使用也更加的方便. 我们知道,当我们的图片是放在tomcat下web ...
- css实现超连接按钮形式显示
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Spring dbcp连接池简单配置 示例
一.配置db.properties属性文件 #database connection config connection.username=sa connection.password=sa conn ...