【HDUOJ】1213 How many tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几张桌子。
题解:啊。裸题。直接输入join一下,然后最后统计同父亲有多少个就行。
代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1e6+; int f[maxn]; void init(int n){
for(int i = ; i <= n ;i++){
f[i] = i;
}
}
int find(int x){
if( x != f[x]){
f[x] = find(f[x]);
}
return f[x];
}
void join(int a,int b){
int x = find(a);
int y = find(b);
if(x!=y)
f[x]=y;
} bool judge(int x,int y){
x=find(x);
y=find(y);
if(x!=y) return true;
return false;
} int n,m;
int main(){
int T;
cin>>T;
while(T--){
int cnt = ;
cin>>n>>m;
init(n);
int a,b;
for(int i = ; i < m ;i++){
cin>>a>>b;
join(a,b);
}
for(int i = ; i <= n ;i++){
if(f[i] == i){
cnt++;
}
}
cout<<cnt<<endl;
} return ;
}
【HDUOJ】1213 How many tables的更多相关文章
- 【整理】mysql中information_schema.tables字段说明
[整理]mysql中information_schema.tables字段说明 2016-05-04 16:47:50| 分类: 默认分类|举报|字号 订阅 下载LOFTER我的照片书 | ...
- 【leetcode】1213.Intersection of Three Sorted Arrays
题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...
- 【SQL】175. Combine Two Tables
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- 【HDUOJ】几道递推DP
就不写题解了.很基础的递推. HDU2084数塔 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 代码: #include <iostre ...
- 【HDUOJ】1257 最少拦截系统
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257 题意:经典题. 题解:最长上升子序列. 代码: #include <iostream> ...
- 【HDUOJ】4280 Island Transport
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:有n个岛屿,m条无向路,每个路给出最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到 ...
- [原]Water Water Union-Find Set & Min-Spanning Tree Problems' Set~Orz【updating...】
[HDU] 1213 - How Many Tables [基础并查集,求父节点个数] 1856 -More is better [基础并查集,注意内存,HDU数据水了,不用离散化,注意路径压缩的方式 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- 关闭windows的DEP
1.与开启dep时一样,按组合键win+r打开运行窗口,输入cmd并按回车,如图所示: 2.调出命令提示符窗口后,输入bcdedit.exe/set {current} nx AlwaysOff ...
- Java-Class-C:java.util.BigDecimal
ylbtech-Java-Class-C:java.util.BigDecimal 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 1. /* * Copyright (c) 19 ...
- maven surefire入门
一.maven常用命令: mvn compile mvn install mvn test mvn clean mvn dependency:resolve -X #查看完整的debug信息!!! ...
- node 创建静态web服务器(下)(处理异步获取数据的两种方式)
接上一章. 上一章我们说创建的静态web服务器只能识别html,css,js文件,功能较为单一,且图片格式为text/html,这是不合理的. 本章,我们将解决该问题. 这里,我们先准备好一个json ...
- CSS中block,inline和block-inline的区别(转载)
http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level e ...
- JAVA集合--Collection接口
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog 在概述里面也说过:Collection是jav ...
- python--常用模块:collections 、time、random
一.collections 模块 1:nametuple 生成可以用名字访问内容的元祖 from collections import namedtuple point=namedtuple('p ...
- ant design 两个tabs如何同时切换
假设界面上有两个地方用到了同一个tabs,但是切换其中一个tabs,另一个tabs并不会同时切换,因为只是在其中一个tabs上调用了onChange,所以需要用到activeKey动态地设置tabs的 ...
- 网络编程之 TCP-UDP的详细介绍
一.TCP协议 1. TCP协议的特点 1.TCP是面向连接的运输层协议.这就意味着,在使用该协议之前,必须建立TCP连接.在传输数据完毕后,必须释放已经建立的TCP连接. 2.每一条TCP连接只能有 ...
- python——pandas基础
参考: 实验楼:https://www.shiyanlou.com/courses/1091/learning/?id=6138 <利用python进行数据分析> pandas简介 Pan ...