第一次交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的更多相关文章

  1. UVA - 11987 Almost Union-Find[并查集 删除]

    UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...

  2. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  3. UVa 11987 Almost Union-Find(支持删除操作的并查集)

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

  4. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  5. UVa 11987 Almost Union-Find (虚拟点)【并查集】

    <题目链接> 题目大意: 刚开始,1到n个集合中分别对应着1~n这些元素,然后对这些集合进行三种操作: 输入 1 a b 把a,b所在的集合合并 输入 2 a b 把b从b所在的旧集合移到 ...

  6. UVA - 11987 Almost Union-Find(带删除的并查集)

    I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something s ...

  7. UVA - 11987 Almost Union-Find 并查集的删除

    Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...

  8. UVA 11987 Almost Union-Find 并查集单点修改

                                     Almost Union-Find I hope you know the beautiful Union-Find structur ...

  9. 并查集(删除) UVA 11987 Almost Union-Find

    题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...

随机推荐

  1. Linux获取进程中变量

    列出所有进程 #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> ...

  2. Android 关于::app:clean :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE,引用jar冲突问题

    错误提示: Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTest ...

  3. [ Java ] [ Spring ] [ Spring MVC ] Tutorial

    中文 Spring 教學 http://hatemegalaxy.blogspot.tw/2014/09/spring-framework-useful-tutorials-for.html 英文 S ...

  4. Kinect 开发 —— 进阶指引 (下)

    运动识别 利用运动识别(motion detection)来进行近景识别是最有意思的一种方式.实现运动识别的基本原理是设置一个起始的基准RGB图像,然后将从摄像头获取的每一帧影像和这个基准图像进行比较 ...

  5. PHP foreach遍历数组之如何判断当前值已经是数组的最后一个

    先给出foreach的两种语法格式 1,foreach (array_expression as $value) statement 2,foreach (array_expression as $k ...

  6. px、em、rem、vw、vh、vm、rpx这些单位的

    px是像素 em是参考父元素的font-size的倍数 rem是参考根元素的font-size 常用于响应式,一般会让html的font-size:625%,body的大小为.16rem.这样1rem ...

  7. Android eclipse 提示java代码 快捷键

    1.提示java代码能够用ALT+/ 键就能够了(前提是你要把你须要的类或方法的首字母打出来).我添加的部分:仅仅要输入sysout,然后alt+/键就能够输出System.out.prinln(), ...

  8. [Python] List & Object spread in Python

    def myfunc(x, y, z): print(x, y, z) tuple_vec = (, , ) dict_vec = {, , } >>> myfunc(*tuple_ ...

  9. 项目EasyUi和JS中遇到的问题总汇

    近期因为项目用到EasyUi,曾经仅仅是听过,可是没有详细用过.仅仅能一边学一边做.如今将做的过程中遇到的一些难点总结例如以下,以备后用: EsayUi使用: Json格式:key:value,key ...

  10. 关于EJB--实体Bean的BMP和CMP选择

    EJB有两种主要类型BMP(Bean managed persistence )和CMP(Container managed persistence ),这两种类型各有优缺点. BMP是在Bean中完 ...