UVA 11987 - Almost Union-Find
第一次交TLE,说好的并查集昂。
好吧我改。求和、个数 在各个步骤独立算。。
还是TLE。
看来是方法太慢,就一个数组(fa),移动的话,移动跟结点要遍历一次 T T
嗯,那就多一个数组。
0.189S。
#include<cstdio>
const int MAXN=100000+10;
int fa[MAXN],num[MAXN],idx[MAXN],cnt,n,m;
long long sum[MAXN];
int find(const int & x) //查找父结点,顺带路径压缩
{
return fa[x]==x? x: fa[x]=find(fa[x]);
} inline void init()
{
for(int i=1;i<=n;i++)
{
fa[i]=idx[i]=sum[i]=i;
num[i]=1;
}
cnt=n;
} inline void Union()
{
int p,q;
scanf("%d%d",&p,&q);
int rootp=find(idx[p]); //找p的根节点
int rootq=find(idx[q]);
if(rootp!=rootq)
{
fa[rootp]=rootq;
sum[rootq]+=sum[rootp];
num[rootq]+=num[rootp];
}
} inline void update(const int &p,const int &rootp)
{
sum[rootp]-=p;
num[rootp]--;
idx[p]=++cnt;
sum[idx[p]]=p;
num[idx[p]]=1;
fa[idx[p]]=idx[p];
} inline void move()
{
int p,q;
scanf("%d%d",&p,&q);
int rootp=find(idx[p]); //找p的根节点
int rootq=find(idx[q]);
if(rootp!=rootq)
{
update(p,rootp);
fa[idx[p]]=rootq;
sum[rootq]+=p;
num[rootq]++;
}
} inline void print()
{
int p;
scanf("%d",&p);
int rootp=find(idx[p]);
printf("%d %lld\n",num[rootp],sum[rootp]);
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
while(m--)
{
int action;
scanf("%d",&action);
switch(action)
{
case 1:Union();break;
case 2:move();break;
case 3:print();break;
}
}
}
}
UVA 11987 - Almost Union-Find的更多相关文章
- UVA - 11987 Almost Union-Find[并查集 删除]
UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
- UVa 11987 Almost Union-Find(支持删除操作的并查集)
传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...
- UVA 11987 Almost Union-Find (并查集+删边)
开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...
- UVa 11987 Almost Union-Find (虚拟点)【并查集】
<题目链接> 题目大意: 刚开始,1到n个集合中分别对应着1~n这些元素,然后对这些集合进行三种操作: 输入 1 a b 把a,b所在的集合合并 输入 2 a b 把b从b所在的旧集合移到 ...
- UVA - 11987 Almost Union-Find(带删除的并查集)
I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something s ...
- UVA - 11987 Almost Union-Find 并查集的删除
Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...
- UVA 11987 Almost Union-Find 并查集单点修改
Almost Union-Find I hope you know the beautiful Union-Find structur ...
- 并查集(删除) UVA 11987 Almost Union-Find
题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...
随机推荐
- 运行maven项目出现的报错
java问题:严重: Error configuring application listener of class org.springframework.web.context.Cont 解决方案 ...
- html --- bootstrap 框架 (栅格系统布局)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 洛谷 P2242 公路维修问题
P2242 公路维修问题 题目描述 由于长期没有得到维修,A国的高速公路上出现了N个坑.为了尽快填补好这N个坑,A国决定对M处地段采取交通管制.为了求解方便,假设A国的高速公路只有一条,而且是笔直的. ...
- jquery表格简单插件
1.一直对jquery插件感觉非常神奇.今天动手写了一个超级简单的案例. 2.效果 3.体会 a.jquery插件编写能力. 须要具备一定js能力的编写.还有写css样式的运用:希望以后这方面会有提高 ...
- Swift具体解释之三----------函数(你想知道的都在这里)
函数(你想知道的都在这里) 注:本文为作者自己总结.过于基础的就不再赘述 ,都是亲自測试的结果.如有错误或者遗漏的地方.欢迎指正.一起学习. 1. 函数的简单定义和调用 简单的无參函数就不再赘述 , ...
- Property-属性动画
今天第一次接触到属性动画.参考着 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 的博客,自己学习下. 它的区别跟 ...
- 转:Java的一道面试题----静态变量初始化过程
public class Test{ private static Test tester = new Test(); //step 1 private static int count1; //st ...
- 使用Notepad++的XML Tools插件格式化XML文件
转自“”:https://blog.csdn.net/qq_36279445/article/details/79803310 1. 安装XML Tools插件 (1) 通过网址http://sour ...
- mahout算法库(四)
mahout算法库 分为三大块 1.聚类算法 2.协同过滤算法(一般用于推荐) 协同过滤算法也可以称为推荐算法!!! 3.分类算法 算法类 算法名 中文名 分类算法 Log ...
- bzoj3307雨天的尾巴(权值线段树合并/DSU on tree)
题目大意: 一颗树,想要在树链上添加同一物品,问最后每个点上哪个物品最多. 解题思路: 1.线段树合并 假如说物品数量少到可以暴力添加,且树点极少,我们怎么做. 首先在一个树节点上标记出哪些物品有多少 ...