第一次交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. 前台ajax验证技术采用正则表达式

    var flag1,flag2,flag3,flag4,flag5,flag6=false; function val(obj) { var regBox = { /* regEmail : /^([ ...

  2. C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)

    1. 概要   本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...

  3. vue1

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Get Started with Git and Team Services

    https://www.visualstudio.com/en-us/docs/git/gitquickstart Visual Studio查看日志 LocalHistory和Incoming是拆开 ...

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

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

  6. command---调用指定的指令并执行

    command命令调用指定的指令并执行,命令执行时不查询shell函数.command命令只能够执行shell内部的命令. 语法 command(参数) 参数 指令:需要调用的指令及参数. 实例 使用 ...

  7. 洛谷 P1801 黑匣子_NOI导刊2010提高(06)(未完)

    P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...

  8. BZOJ 1027 JSOI2007 合金 计算几何+Floyd

    题目大意:给定一些合金,选择最少的合金,使这些合金能够按比例合成要求的合金 首先这题的想法特别奇异 看这题干怎么会想到计算几何 并且计算几何又怎么会跟Floyd挂边 好强大 首先因为a+b+c=1 所 ...

  9. POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)

    Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8132   Accepted: 1949 Desc ...

  10. ODBC总结

    引用头文件:sql.h.sqlext.h.sqltypes.h 添加库文件:odbc32.lib   odbccp32.lib 1.定义环境变量: SQLHENV henv =nullptr://环境 ...