第一次交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. RadioButton的check改变的时候

    https://stackoverflow.com/questions/8095256/asp-net-radio-button-change You'll need to specify the a ...

  2. 分享一个关于js原型链的理解

    http://www.cnblogs.com/wyaocn/p/5815761.html

  3. shell 日期转换

    1.字符串转换为时间戳可以这样做: date -d "2010-10-18 00:00:00" +%s 输出形如: 1287331200 其中,-d参数表示显示指定的字符串所表示的 ...

  4. Java基础学习总结(21)——常用正则表达式列表

    很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,包括校验数字.字符.一些特殊的需求等等.给自己留个 ...

  5. 洛谷 P2867 [USACO06NOV]大广场Big Square

    P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...

  6. Linux搭建aspx.net环境之:CentOs 7 安装 Mono 和 Jexus 步骤记录

    1 因为163没有CentOs7的镜像.所以没有加这个 wget  http://mirrors.163.com/.help/CentOS6-Base-163.repo cd /etc/yum.rep ...

  7. js32---CommonUtil.js

    // BH 命名空间 namespace var BH = {} ; BH.Interface = function(name,methods){ //Interface是类.方法的名字,以后用BH. ...

  8. IE中实现placeholder

    简介:IE本身不支持Placeholder这种先进的特性,但是我们又必须且仅仅支持IE,所以网上找了一个支持placeholder的方法 考虑版权,以及知识产权原因,只放链接: http://blog ...

  9. sql之group by的用法

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  10. eclipse中修改了代码编译后执行不起作用

    在网上试了好多方法都没解决 后来,删了eclipse里的服务器,重新创建了一下,重新运行自己的工程发现修改的代码起作用了.