G - Ice_cream's world I (并查集)
InputIn the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between A and B has a wall(A and B are distinct). Terminate by end of file. OutputOutput the maximum number of ACMers who will be awarded.
One answer one line.
Sample Input
8 10
0 1
1 2
1 3
2 4
3 4
0 5
5 6
6 7
3 6
4 7
Sample Output
3
AC代码
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int maxn=; int n,m;
int father[maxn]; void makeSet(){
for(int i=;i<n;i++){
father[i]=i;
}
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
makeSet();
int ans=;
int a,b;
while(m--){
scanf("%d%d",&a,&b);
int fx=findSet(a);
int fy=findSet(b);
if(fx==fy)
ans++;
else
father[fx]=fy;
}
printf("%d\n",ans);
}
return ;
}
这有一个并查集详解,讲得很好
G - Ice_cream's world I (并查集)的更多相关文章
- G - 小希的迷宫(并查集)
1今天准备复习三道以前做过的题呢,结果只看了一道,也因为交题的时候没把测试时候为了方便而改的数字改过来而wrong answer,浪费了好长时间,啊啊啊~~ 不过这道题应该是掌握了,嘿嘿…… Desc ...
- BZOJ4602:[SDOI2016]齿轮(并查集)
Description 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y.即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为v ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- Ice_cream's world I(并查集成环)
Problem Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...
- Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)
题目链接 2016 ACM-ICPC EC-Final Problem G 题意 给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- luogu4269 Snow Boots G (并查集)
对于某个靴子,如果0代表某个格能走,1代表不能走,那么只要连续的1的个数的最大值>=靴子的步长,那这个靴子就不能用. 那么只要对靴子和格子都按深度排个序,然后从大到小来扫一遍(靴子越来越浅,能走 ...
- [USACO18FEB] Snow Boots G (离线+并查集)
题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...
随机推荐
- cluster DNS
[root@mhc1 dns]# pwd/root/test/k8s/kubernetes/cluster/addons/dns [root@mhc1 dns]# export DNS_SERVER_ ...
- LevelDB 读取记录
[LevelDB 读取记录] LevelDb是针对大规模Key/Value数据的单机存储库,从应用的角度来看,LevelDb就是一个存储工具.而作为称职的存储工具,常见的调用接口无非是新增KV,删除K ...
- s 销售视图数据
INSERT,需将公司ID匹对到以下EXCEL表 INSERT,需将公司ID匹对到以下EXCEL表 [Public] ConnectString=host="siebel://10.10.0 ...
- PLSQL Developer工具的使用
1.运行 2.字体大小 导入csv文件. 任何工具都有失败的可能, 这个时候,也失败了. 当然还有另一种可能,文件被人为改过了,括号改过了,就即使使用下面的kettle工具也没用了.这时可以导出文件对 ...
- 使用MySQLMTOP监控MySQL性能
一.服务器角色 服务器角色 172.18.35.29 10.160.22.14 (MySQL Master) 10.160.22.47 (MySQL Slave) 监控点 YES NO NO 被监控点 ...
- FW:程序在内存的划分(转)
一.预备知识—程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...
- Path Creation and Path Painting
[Path Creation and Path Painting] Path creation and path painting are separate tasks. First you crea ...
- wCF 问题收集页
1.设置最大序列化集合元素个数 http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.datacontractser ...
- UVa 1151 Buy or Build (最小生成树+二进制法暴力求解)
题意:给定n个点,你的任务是让它们都连通.你可以新建一些边,费用等于两点距离的平方(当然越小越好),另外还有几种“套餐”,可以购买,你购买的话,那么有些边就可以连接起来, 每个“套餐”,也是要花费的, ...
- vue2.0 #$emit,$on的使用
首先实例化: bus.js import Vue from 'Vue' export default new Vue() 组件1, import bus from '../../assets/js/b ...