【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 =============================以下是最小生成树+并 ...
随机推荐
- Django Shell 如何对数据库进行测试
Django 如何对数据库进行测试 Python manager.py shell Import django django.setup() from my_app.models import * 开 ...
- 使用linkedhashmap实现LRU(最近最少使用缓存算法)
import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends Link ...
- vue组件通信之父组件主动获取子组件数据和方法
ref 可以用来获取到dom节点,如果在组件中应用,也可以用来获取子组件的数据和方法. 比如,我定义了一个home组件,一个head组件,home组件中引用head组件. 此时,home组件是head ...
- Java Jar Manifest
JAR文件可以可选地在META-INF目录中包含名为MANIFEST.MF的Manifest文件.Manifest文件包含有关JAR文件及其条目的信息.Manifest文件可以包含有关JAR文件的CL ...
- mybatis执行test07测试类却显示test05测试类调用的sql语句出错
1.测试类 @Test public void test07() { IStudentDao studentDao = new IStudentDaoImpl(); Student student = ...
- B. Light bulbs(2019 ICPC上海站)
There are NN light bulbs indexed from 00 to N-1N−1. Initially, all of them are off. A FLIP operation ...
- <爬虫>相关的知识
1.概念.工具和HTTP 什么是爬虫 模拟客户端发送网络请求,获取响应,按照规则提取数据 爬虫的数据去哪了 展示到网页上(百度新闻,今日头条) 进行分析,从数据中寻找规律(指数网站:百度指数) 需要的 ...
- Samza系统架构
- Spring入门(四)Spring-test模块
自动化转配bean的测试案例分析 package soundsystem; import static org.junit.Assert.*; import org.junit.Rule; impor ...
- 从零开始搭建系统1.2——Nginx安装及配置
一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...