并查集系列题解
来源:互联网 发布:适合win10的ps软件 编辑:程序博客网 时间:2024/05/17 20:29
CodeForces 217A
题意:给一系列的点,每个点以x,y轴方向进行延伸,使得一些点成为一个集合,然后求出至少需要添加几个点使得所有的点都在一个集合里面;
思路:用并查集求出现有的形成了多少个集合,然后答案就是集合数 - 1;
这里的finds函数相比以前我写的比较精简一点;
#include<bits/stdc++.h>using namespace std;const int maxn = 1000 + 10;struct Node{ int x; int y;}a[maxn];int father[maxn];//int finds(int x)//{// int r = x;// while(father[r] != r)// {// r = father[r];// }// int j = x,i ;// while( j != r)// {// i = father[j];// father[j] = r;// j = i;// }// return r;//}int finds(int x){ return father[x] != x ? father[x] = finds(father[x]) : x;}void unions(int x,int y){ int rx = finds(x); int ry = finds(y); if(rx != ry) { father[rx] = ry; }}int n;void Init(){ for(int i = 1; i <= n ; i ++) { father[i] = i; }}int main(){ while( ~ scanf("%d",&n) ) { Init(); for(int i = 1; i <= n ; i ++) scanf("%d%d",&a[i].x ,&a[i].y); for(int i = 1; i <= n ; i ++) { for(int j = 1; j < i ; j ++) { if((a[i].x == a[j].x || a[i].y == a[j].y) && (finds(i) != finds(j)) ) { unions(i,j); } } } set<int>s; for(int i = 1; i <= n ; i ++) { s.insert(finds(i)); } cout << s.size() - 1 << endl; } return 0;}
附上最近写的一些水题:
CodeForces 733D
题意:给出一系列的长方体的长宽高,求出用一个还是用两个长方体能够构成的球体的体积最大;注意一点:两个的话记得长宽高中有两个相等的时候才能用在一起;
思路:对两个分析:叠加只有叠加最小的边才有意义,所以先对每个长方体长宽高进行从大到小排一下序,之后再进行结构体排序,之后相邻的判断一下能不能一起构成长方体以形成更大的球;
#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll maxn = 100000 + 10;struct Node{ ll poll; ll x; ll y; ll z;}num[maxn];bool cmp1(const ll a,const ll b){ return a > b;}bool cmp2(Node a,Node b){ return a.x > b.x ||(a.x == b.x && a.y > b.y);}int main(){ ll n; while( ~ scanf("%I64d",&n) ) { ll a[3]; for(ll i = 1; i <= n ; i ++) { scanf("%I64d%I64d%I64d",&a[0],&a[1],&a[2]); sort(a,a + 3, cmp1); num[i].poll = i; num[i].x = a[0]; num[i].y = a[1]; num[i].z = a[2]; } sort(num + 1,num +n + 1 , cmp2); ll temps = 1; for(ll i = 1; i <= n ; i ++) { if(num[i].z > num[temps].z) temps =i; } ll ans = num[temps].z; ll k ; for(ll i = 1; i < n ; i ++) { if(num[i].x == num[i + 1].x && num[i].y == num[i+ 1].y){ ll temp = min(num[i].y , num[i].z + num[i + 1].z); if(temp > ans) { ans = temp; k = i; } } } if(ans == num[temps].z){ cout << 1 << endl << num[temps].poll << endl; continue; } cout << 2<< endl; cout << num[k].poll << " " << num[k + 1].poll << endl; } return 0;}
0 0
- 并查集系列题解
- 并查集题解
- 并查集(题解)
- luoguP3367 并查集 题解
- 并查集系列
- 并查集及例题题解
- 并查集及例题题解
- 【题解】 Luogu P2024 食物链 (并查集)
- poj_1611_并查集 题解记录
- 算法原理系列:并查集
- 【POI】【POI2012】【Tour de Byteotia】【题解】【并查集】
- 【BZOJ】【P3673】【可持久化并查集】【题解】
- HDU 1035 Robot Motion Union Find 并查集题解
- 【BZOJ】【P1529】【POI2005】【ska Piggy banks】【题解】【并查集】
- 【日常学习】【并查集+map】codevs2639 约会计划题解
- POJ 2524 Ubiquitous Religions 详细题解(并查集)
- HDU 1272详细题解(并查集)
- 【基础练习】【并查集】codevs2796 最小完全图题解
- android studio 查找源码使用 jdk.table.xml
- 【手把手教你树莓派3 (五)】DHT11传感器
- 排序的几种方法
- 搭建React Native环境的几个Tips
- 选项卡
- 并查集系列题解
- awk参数详解
- Math223RectangleArea
- 【NOIP2008双栈排序】题目说什么,我做什么!
- 大型网站架构之分布式消息队列
- bzoj 2006: [NOI2010]超级钢琴 (st表+优先队列)
- QT---FTP练习,连接FTP服务器,浏览其中的文件,下载到本地目录中
- 第十周项目三改进小学生考试系统
- 排序的几种方法