随笔分类 -  并查集

zoj 3261 Connections in Galaxy War
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261先把不需要destroy的边连上 然后逆序寻找答案 遇到destroy 的边就连上过程中用并查集维护答案代码:#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<set>#include<map>#incl 阅读全文
posted @ 2013-03-07 21:36 夜-> 阅读(164) 评论(0) 推荐(0) 编辑
poj 1182 食物链
摘要:http://poj.org/problem?id=1182并查集的灵活应用代码:/*可以这样理解并查集是由很多树组成的,这些树不断的合并下面代码f[]仍然代表此节点所属的树根而d[]表示此点到父节点的差值,但是每次求fx()(树根)时,节点都更新指向树根假如比父节点大1 则此点可以吃掉父节点假如比父节点大2 则此点可以被父节点吃掉假如和父节点相等,则属于同类这里所指的大是针对当前节点对父节点而言的,2比1大1 0比2大1(循环)由于更新完都指向树根了,所以就有了相同的参照物根据不同的点和树根的关系,可以推算出一棵树任意两点之间的关系问题就在于如何维护树的合并,f[]还是按照原来的方法,d.. 阅读全文
posted @ 2012-12-15 21:31 夜-> 阅读(198) 评论(1) 推荐(0) 编辑
poj 1611 The Suspects
摘要:http://poj.org/problem?id=1611简单并查集代码:import java.util.*;import java.math.BigInteger;import java.math.BigDecimal;;interface def{ public static final int N=30005;}public class Main implements def{ int [] f = new int[N]; int fx(int x){ if(f[x]!=x){ f[x]=fx(f[x]); } return f[x]; } public static vo... 阅读全文
posted @ 2012-12-08 13:26 夜-> 阅读(125) 评论(0) 推荐(0) 编辑
sdut 2494 Minimum Spanning Tree?
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2494按求最小生成树的步骤 先按边的长度进行排序我们把每一个边权相等的一个段看做一个单位那我们一个单位一个单位的求假如求到第k个单位了 无论前面k-1个单位怎么取舍 结果都是前面出现的所有端点都在一个联通块里面无论度第k个单位怎么取舍前k个单位出现的端点也都在一个联通块里(求第k+1个联通块时)所有按照排好的顺序 每个单位是相互独立的对一到了第k个单位时 先把这个单位的所有边先搜一遍 如果哪个边的两个端点不在一个联通块内 则说明它是有资格成为某 阅读全文
posted @ 2012-12-01 18:08 夜-> 阅读(159) 评论(0) 推荐(0) 编辑
1320. Graph Decomposition
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1320ural 的题目倒是挺好的 不过题意一直让人匪夷所思呀 很难懂题意:给一个图 依次从里面拿走两个相邻的边 问可不可以最后全拿完并查集 很巧妙 我承认我看了解题报告 自己思维还是不够呀每个连同块 里面边的个数是偶数就可以 奇数就不可以代码:#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include< 阅读全文
posted @ 2012-10-24 16:55 夜-> 阅读(191) 评论(0) 推荐(0) 编辑
zoj 3659 Conquer a New Region
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882原来一个排序加并查集就可以呀 思维还是不够强呀这里没有环,所以并查集并不是用了处理环 而是指出当前集合中最优选择点当两个集合合并时 将两个集合的最优选择点 比较择优更新代码及其注释:#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>#include<map>#include<cm 阅读全文
posted @ 2012-10-22 19:18 夜-> 阅读(181) 评论(0) 推荐(0) 编辑
1272. Non-Yekaterinburg Subway
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1272#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<vector>using namespace std;const int INF=0x3f3f3f3f;const int N=10005;int f[N];int findx(int x){ if(f[x]!=x) f[x]=findx(f[x]); return f[x];}i 阅读全文
posted @ 2012-10-19 21:30 夜-> 阅读(460) 评论(0) 推荐(0) 编辑
hdu 4297 One and One Story
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4297此题目中的图是一个特殊的森林 特殊在于它的树都有一个环 这个环内点包括树根基本思路:先用并查集 对图进行处理1 建立完整森林2 构成环的边不加入森林3 记录每个环上有多少点 每个环上点属于第几个环然后处理 couples不在一个树上的不可达同一个 room 内 特别处理将 couples 储存然后LCA 这里LCA 与原始LCA 有不同如果最近公共祖先不在环上 直接求出 如果在环上 求其分别是环上哪一个点过来的最后就好处理了对不同方案进行选优代码及其注释:#include <iostream> 阅读全文
posted @ 2012-09-19 20:49 夜-> 阅读(253) 评论(0) 推荐(0) 编辑
poj 2513 Colored Sticks
摘要:http://poj.org/problem?id=2513和离散数学有关 欧拉回路问题同一颜色的标记为同一点 同一stick 的两端为相连状态数学什么的 最不擅长了 看了别人的解析两个限制条件1 图为联通 可用并查集判断2 度 (出度+入度)数为基数的点为0 或者为 2 输入时相加不满足上述两个条件中的任何一个 都Impossible存颜色和查询要用字典树 用map超时代码及其注释:#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include< 阅读全文
posted @ 2012-08-11 17:57 夜-> 阅读(114) 评论(0) 推荐(0) 编辑
poj 2492 A Bug's Life
摘要:http://poj.org/problem?id=2492和poj 1703 一样不再多说代码:#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<algorithm>#include<set>using namespace std;const int N=100010;int f[N];int same[N];int findf(int 阅读全文
posted @ 2012-07-16 20:29 夜-> 阅读(156) 评论(0) 推荐(0) 编辑
poj 1703 Find them,Catch them
摘要:http://poj.org/problem?id=1703题目大意:有一些罪犯,分两伙 每伙至少一个两种操作D a b:a和b不是一伙的A a b:a和b 之间的关系是什么思路:并查集,先把可以确定关系的罪犯放在一个集里 再多一个数组表示此节点和他指向的上一个节点是否一样(是否同伙)代码及其注释:#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<algor 阅读全文
posted @ 2012-07-16 20:02 夜-> 阅读(161) 评论(0) 推荐(0) 编辑
poj 1330 Nearest Common Ancestors
摘要:http://poj.org/problem?id=1330最近公共父结点离线算法 LCA用并查集 和 dfs每搜到一个点 先让其父结点等于自己继续往下搜 这时如果询问已搜过的点 则两点之间的最近公共父结点就是 已搜过的点的最上父结点若没搜过 就继续深搜所用相连的点都搜完后 让此点的父结点为上一层结点#include<iostream>#include<cstring>using namespace std;const int N=10005;struct node{ struct tt *next;}mem[N];struct tt{ struct tt *next; 阅读全文
posted @ 2012-05-29 21:25 夜-> 阅读(136) 评论(0) 推荐(0) 编辑
poj 2524 Ubiquitous Religions
摘要:http://poj.org/problem?id=2524#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<algorithm>using namespace std;int f[50001];int ans;inline int find(int i){ if(f[i]!=i) { f[i]=find(f[i]); } return f[i];}int main(){ int n,m,i,j; int l=0; while(c 阅读全文
posted @ 2012-02-22 18:55 夜-> 阅读(142) 评论(0) 推荐(0) 编辑
1709. Penguin-Avia
摘要:此题使用并查集将多余的航班删去(有环时) 少的航班加上 使得全图联通答案不唯一 所以自己写的和题中实例输出不一样也不一定错还有就是花费最大会超过 long 所以直接用 64位http://acm.timus.ru/problem.aspx?space=1&num=1709#include<iostream>#include<string.h>#include<algorithm>#include<stdio.h>using namespace std;char ans[101][101];int f[101];inline int fin 阅读全文
posted @ 2011-11-24 16:23 夜-> 阅读(685) 评论(0) 推荐(0) 编辑
hdu 1598 find the most comfortable road
摘要:这题刚开始没想到要用并查集 结果没做出来最后堕落了 查了一下别人的答案才知道的先把边排序 由于边不多所以可以枚举任意两边和他们中间的边(排序后的)如果使得两地点连接 则求出最大差 所以符合的情况的最大差中最小的便是答案http://acm.hdu.edu.cn/showproblem.php?pid=1598#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;struct 阅读全文
posted @ 2011-11-23 16:17 夜-> 阅读(151) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示