hdu 并查集分类(待续)
hdu 1829 A Bug's Life
题目大意: 给你n个动物,输入m行a,b,表示a和b应该是异性的,要你判断是否有同性恋。
并查集中,1到n代表应性别,n+1到2n代表一个性别,合并一下,判断一下就好。
#include <cstdio> #include <map> #include <queue> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define mem0(x) memset(x,0,sizeof(x)) #define mem1(x) memset(x,-1,sizeof(x)) typedef long long LL; const int INF = 0x3f3f3f3f; + ; ]; void init(int n) { ;i<=n*;i++) pa[i] = i; } int uf_find(int x){return x == pa[x] ? x : (pa[x] = uf_find(pa[x]));} bool same(int x,int y){return uf_find(x) == uf_find(y);} void join(int x,int y) { int fx = uf_find(x);int fy = uf_find(y); if(fx != fy) pa[fx] = fy; } int main() { ; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(n); ; ;i<m;i++) { scanf("%d%d",&x,&y); if(same(x,y) || same(x+n,y+n)) flag = ; else join(x,y+n);join(x+n,y); } printf("Scenario #%d:\n",p++); if(flag) printf("No suspicious bugs found!\n"); else printf("Suspicious bugs found!\n"); puts(""); } ; }
hdu 1856 More is better
题目大意:合并一系列关系之后,问你,结点最多的那堆,有多少个结点,即 最大连通量的结点个数。
用一个sum数组就能解决问题了。
#include <cstdio> #include <map> #include <queue> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define mem0(x) memset(x,0,sizeof(x)) #define mem1(x) memset(x,-1,sizeof(x)) typedef long long LL; const int INF = 0x3f3f3f3f; + ; int pa[maxn]; int sum[maxn]; void init() { ;i<=maxn;i++) pa[i] = i,sum[i] = ; } int uf_find(int x){return x == pa[x] ? x : (pa[x] = uf_find(pa[x]));} bool same(int x,int y){return uf_find(x) == uf_find(y);} void join(int x,int y) { int fx = uf_find(x);int fy = uf_find(y); if(fx == fy) return ; if(sum[fy] > sum[fx]) { pa[fx] = fy; sum[fy] = sum[fy] + sum[fx]; } else { pa[fy] = fx; sum[fx] = sum[fx] + sum[fy]; } } int main() { int n,x,y; while(~scanf("%d",&n)) { init(); ;i<n;i++) { scanf("%d%d",&x,&y); join(x,y); } ; ;i<=maxn;i++) ans = max(ans,sum[i]); printf("%d\n",ans); } ; }
hdu 2473 Junk-Mail Filter(并查集的删除操作,虚拟父结点)
把1到n的父结点设为n+1到2n,即pa[i] = n + i;
n+1到2n+m按平常并查集的初始化。2n+1到2n+m的结点的设立的目的如代码所示。
删除的结点把他们的父结点从n+i指向cnt++。
注意一下pa数组要开2n+m大小。
#include <cstdio> #include <map> #include <set> #include <queue> #include <cstring> #include <algorithm> #include <iostream> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; #define mem0(x) memset(x,0,sizeof(x)) #define mem1(x) memset(x,-1,sizeof(x)) ;//数组要开 2*n+m char ch; int n,m,x,y,cnt,ans; int pa[maxn]; void init(int n,int m) { cnt = * n; ;i<n;i++) pa[i] = i + n; *n+m;i++) pa[i] = i; } int uf_find(int x){return x == pa[x] ? x : (pa[x] = uf_find(pa[x]));} void join(int x,int y) { int fx = uf_find(x);int fy = uf_find(y); if(fx != fy) pa[fx] = fy; } int main() { ; while(~scanf("%d%d",&n,&m)) { && m == ) break; init(n,m); ;i<m;i++) { getchar(); scanf("%c",&ch); if(ch == 'M') scanf("%d%d",&x,&y),join(x,y); else if(ch == 'S') scanf("%d",&x),pa[x] = cnt++; } set<int>s; ;i<n;i++) s.insert(uf_find(i)); printf("Case #%d: %d\n",p++,s.size()); } ; }
hdu 3038 How Many Answers Are Wrong(带权并查集)
题目大意:一行给出三个整数 x y sum 代表从x到y这段 数值之和为sum,让你判断
怎么用并查集来做呢?转换成前缀和。
rrank[i] 代表着 i到它父结点之间所有结点的和。
if(fx == fy) return ((rrank[y] - rrank[x]) == sum) ? true :false; pa[fy] = fx; rrank[fy] = rrank[x] - rrank[y] + sum; return true;
高亮的代码,可以由rrank[fy] = rrank[x] - rrank[y] + sum;这个等式可以自己画个条条,摸索一下。rrank[x]代表x到fx的和,rrank[y]代表y到fy的和,rrank[x]+sum就代表了y到fx的和,那么
rrank[fy](我们的目的是把rrank[fy]更新成fy到fx的和)就是y到fx的和减掉y到fy的距离,即rrank[x]+sum再减掉rrank[y]
if(x == pa[x]) return x; int fa = pa[x]; pa[x] = uf_find(pa[x]); rrank[x] += rrank[fa]; return pa[x];
高亮的两句话的顺序弄清楚为什么,谁先谁后,就能对这道题的解决思路有了很清晰的了解,这是非常关键的部分。同时也是对递归的一个更深刻的理解。23333我一开始还纳闷怎么是这个顺序后来才想明白。
#include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; ; int pa[maxn],rrank[maxn]; void init(int n) { ;i<=n;i++) pa[i] = i,rrank[i] = ; } int uf_find(int x) { if(x == pa[x]) return x; int fa = pa[x]; pa[x] = uf_find(pa[x]); rrank[x] += rrank[fa]; return pa[x]; } bool join(int x,int y,int sum) { int fx = uf_find(x);int fy = uf_find(y); if(fx == fy) return ((rrank[y] - rrank[x]) == sum) ? true :false; pa[fy] = fx; rrank[fy] = rrank[x] - rrank[y] + sum;//rrank[fy] + rrank[y] = rrank[x] + sum 画个图就出来了 return true; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { init(n); ; ;i<m;i++) { scanf("%d%d%d",&x,&y,&sum); ,y,sum) ) ans++; } printf("%d\n",ans); } ; }
hdu 并查集分类(待续)的更多相关文章
- CCCC L2-010. 排座位【并查集/分类讨论】
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- 【BZOJ-2503】相框 并查集 + 分类讨论
2503: 相框 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 71 Solved: 31[Submit][Status][Discuss] Desc ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- poj1417 true liars(并查集 + DP)详解
这个题做了两天了.首先用并查集分类是明白的, 不过判断是否情况唯一刚开始用的是搜索.总是超时. 后来看别人的结题报告, 才恍然大悟判断唯一得用DP. 题目大意: 一共有p1+p2个人,分成两组,一组p ...
- HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)
http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
随机推荐
- EF查询视图只得到一条记录
1.出错结果:数据库表视图有多条数据,在使用EF框架进行查询时却只得到一条数据(注:拦截EF得到的sql语句在数据库进行查询并没有任务问题). 2.出错原因:该视图中没有ID或者主键,EF查询时进行反 ...
- 【python】dict4ini和xmltodict模块用途
dict4ini模块:可以读写配置文件 xmltodict模块:将xml和json互相转换 https://pypi.python.org/pypi/xmltodict
- HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场
题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...
- The certificate used to sign "XXX" has either expired or has been revoked
在Xcode真机调试开发过程中,无论是使用个人证书或者是企业证书,经常会遇到这样的问题:The certificate used to sign "XXX" has either ...
- Oracle基础函数
--1,大小写控制函数 SELECT LOWER('Hello World') 转小写, UPPER('Hello World') 转大写, INITCAP('hello world') 首字母大写 ...
- 月考(cogs 1176)
[题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间学校就此事做了全校通报. 现已知在当天校会时间有总共N个同学听到了有关Bugall的处分决定. B ...
- VAssistX的VA Snippet Editor的类注释和函数注释
title:类注释shortcut:=== /******************************************************** [DateTime]:$YEAR$.$M ...
- 求sqrt()底层效率问题(二分/牛顿迭代)
偶然看见一段求根的神代码,于是就有了这篇博客: 对于求根问题,通常我们可以调用sqrt库函数,不过知其然需知其所以然,我们看一下求根的方法: 比较简单方法就是二分咯: 代码: #include< ...
- UITableView和UICollectionView的方法学习一
参考资料 UITableView UICollectionView UICollectionViewDataSource UICollectionViewDelegate UICollectionVi ...
- NYOJ题目889求距离
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsYAAAJ2CAIAAADTwNOXAAAgAElEQVR4nO3dPVLrSteG4W8S5B4IsQ