ACM-数据结构 并查集
文章平均质量分 73
黄小豪被虐惨了
这个作者很懒,什么都没留下…
展开
-
hdu 1232
题目并查集水题,主要是熟悉三个函数代码如下:#includeusing namespace std;int pre[1005];int find(int x){ int r=x; while(pre[r]!=r) r=pre[r]; return r;}void Union(int x,int y){ int fx=find(x);原创 2013-07-22 01:17:01 · 513 阅读 · 0 评论 -
poj 1456
题目为了获取利润最大,所以贪心把所有的商品按照利润排序。接着通过并查集将已买的物品的父节点往前更新知道t=0,即,已经到了最前端代码如下:#include#include#includeusing namespace std;const int maxn=10010;struct Item{ int pro,end;}item[maxn];原创 2013-07-27 15:41:59 · 535 阅读 · 0 评论 -
hdu 1856
题目并查集。找到最大出现的那个点。对于每一个线段使得其权值加1最后统计根节点的权值最大的就是答案当n=0是,答案是1.特殊数据特判,否则wa另外并查集的查找操作,若按其他方式写的话,即超时。最后各种bug的加速情况下46msAC代码如下:#includeusing namespace std;#includeconst int maxn=100000原创 2013-07-27 00:18:09 · 557 阅读 · 0 评论 -
poj 2492 && poj 1703
poj 2492因为只有两种关系,所以val代码的与根节点的距离可以来模2对于根节点的更新,在题目的描述中加上自己的理解可以得出代码如下:#includeusing namespace std;#includeconst int maxn=100010;int pre[maxn],val[maxn];int find(int x){ if(pre[x]=原创 2013-07-26 15:35:57 · 581 阅读 · 0 评论 -
hdu 3038
题目和3047差不多。但是为什么是a--。不理解,不加这条就一直wa代码如下:#include#includeusing namespace std;const int maxn=200005;int pre[maxn],val[maxn];void init(int n){ for(int i=0;i<=n;i++){ pre[i原创 2013-07-27 09:39:02 · 722 阅读 · 0 评论 -
hdu 3172 && hdu 3047
题目并查集,大水题直接代码:#includeusing namespace std;#include#includeconst int maxn=100005;int pre[maxn],val[maxn];map m;inline int input(){ char c; int ret=0; c=getchar(); whi原创 2013-07-27 01:14:23 · 643 阅读 · 0 评论 -
poj 1988 &&poj 2524
poj 1988num是统计该堆中cube的数目ans是统计该cube下面的cube的数目代码如下:原创 2013-07-25 08:55:51 · 528 阅读 · 0 评论 -
poj 1182
题目带权的并查集。在网上看了别人的详细解答后,才略微明白过来http://blog.csdn.net/c0de4fun/article/details/7318642代码如下:#includeusing namespace std;#includeconst int maxn=50005;int pre[maxn],rank[maxn];void init()原创 2013-07-24 16:53:10 · 520 阅读 · 0 评论 -
hdu 3635
题目wa了n次,最后网上搜代码。要先进行移动的步数的更新,在路径压缩,否则就是wa代码如下;#include#includeusing namespace std;#define maxn 10010int pre[maxn],num[maxn],times[maxn];inline int find(int x){ if(x==pre[x]) ret原创 2013-07-24 11:31:43 · 537 阅读 · 0 评论 -
LA 3644
X-Plosives A secret service developed a new kind of explosive that attain its volatile property only when a specific association of products occurs. Each product is a mix of two different simple c原创 2013-07-24 00:14:59 · 569 阅读 · 0 评论 -
poj 1308
题目有两点1、只有1个根,可以通过并查集来判断是否是同意祖先2、入度为0的点只有一个,其他的入度均为1通过这两点来判断,输入输出略坑的感觉代码如下:#include#includeusing namespace std;#define maxn 100000+10int pre[maxn],indegree[maxn],num[maxn];void Ini原创 2013-07-23 23:59:08 · 515 阅读 · 0 评论 -
poj 1611
题目多了一个需要维护的值,即为更新同一祖先的数。代码如下:#include#include#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define maxn 30010int pre[maxn];int num[maxn];int find(int x){原创 2013-07-22 15:59:31 · 537 阅读 · 0 评论 -
hdu 1272
题目判断是否有两个点的祖先已经是同一个了。判断是否联通。学了一句代码来防爆栈#include#include#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define maxn 100005int pre[maxn];bool flag,vis[maxn];i原创 2013-07-22 15:40:24 · 585 阅读 · 0 评论 -
hdu 1213
题目与上一题一模一样代码如下:#include#includeusing namespace std;int pre[1005];int find(int x){ int r=x; while(pre[r]!=r) r=pre[r]; return r;}void Union(int x,int y){ int fx=fin原创 2013-07-22 01:25:30 · 537 阅读 · 0 评论 -
hdu 1198
题目看了别人的思路,只需要对于上以及左来合并。最后遍历一遍就好,裸的并查集#includeusing namespace std;#define maxn 110int pre[maxn*maxn];char map[maxn][maxn];int find(int x){ return pre[x]==x?x:(pre[x]=find(pre[x]));}原创 2013-07-22 11:13:49 · 500 阅读 · 0 评论 -
poj 2912
题目和那道食物链的题目差不多。公式的推导还是其他的情况感觉做这一类权值的并查集,Union函数中的通过已知的东西对于路径的更新很重要大部分和http://blog.csdn.net/hange_db/article/details/9529743#comments差不多膜拜荡神代码如下:#include#includeusing namespace std;#原创 2013-07-28 01:28:19 · 860 阅读 · 1 评论