![](https://img-blog.csdnimg.cn/20190918140012416.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
并查集
并查集
QingQingDE23
这个作者很懒,什么都没留下…
展开
-
AcWing 239. 奇偶游戏
①并查集做法:多维护一个d数组,d[i]标识从root到i节点1的个数,之后读入每个问题,对于每个节点进行离散化存储,对已有关系的节点,判断节点之间的1的数量和原来维持的数量是否冲突,如果冲突就输出答案,否则继续。②扩展域解法:在原离散化之后的a的基础上,再加上一个B表示a的偶数情况(b同理),之后用根据关系将同为偶数时加在一个集合内,如果发现奇偶性冲突时就记录答案,退出循环,输出。原创 2022-09-09 18:42:29 · 875 阅读 · 0 评论 -
AcWing 238. 银河英雄传说
并查集,多维护一个i到p[i]的距离数组,注意数组合并时的操作。原创 2022-09-08 11:19:21 · 66 阅读 · 0 评论 -
AcWing 237. 程序自动分析
【代码】AcWing 237. 程序自动分析。原创 2022-09-08 09:39:39 · 56 阅读 · 0 评论 -
AcWing 1252. 搭配购买
又顺带复习了一下01背包问题。并查集+01背包问题。原创 2022-09-07 22:22:58 · 83 阅读 · 0 评论 -
AcWing 1250. 格子游戏
并查集模板题,注意二维坐标转化为一维坐标的巧妙用法。原创 2022-09-07 21:57:09 · 663 阅读 · 0 评论 -
牛客 食物链 题解
①注意%3的巧妙运用②判断元素到根节点的距离#include#includeusing namespace std;const int N = 50010;int father[N], d[N] = {0}, n, k, rv = 0;int findfather(int x){if(x != father[x]){int t = findfather(father[x]);d[x] += d[father[x]];father[x] = t;}return father[x];原创 2021-08-04 23:18:03 · 114 阅读 · 0 评论 -
并查集 模板
#include#includeusing namespace std;const int N = 1005;int father[N];int findfather(int a){if(a == father[a]) return a;else{int f = findfather(father[a]);father[a] = f;return f;}}void Union(int a, int b){int a1 = findfather(a);int b1 = find原创 2021-08-04 19:01:53 · 46 阅读 · 0 评论