[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=1180

[算法]

动态树维护森林连通性

时间复杂度 : O(NlogN ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull; int n;
int val[MAXN]; struct Link_Cut_Tree
{
struct Node
{
int father , son[] , value , cnt;
bool tag;
} a[MAXN];
inline void init()
{
for (int i = ; i <= n; i++)
{
a[i].value = a[i].cnt = val[i];
a[i].tag = false;
a[i].father = a[i].son[] = a[i].son[] = ;
}
}
inline void update(int x)
{
a[x].cnt = a[a[x].son[]].cnt + a[a[x].son[]].cnt + a[x].value;
}
inline void pushdown(int x)
{
if (a[x].tag)
{
swap(a[x].son[] , a[x].son[]);
a[a[x].son[]].tag ^= ;
a[a[x].son[]].tag ^= ;
a[x].tag = false;
}
}
inline bool get(int x)
{
pushdown(a[x].father);
return a[a[x].father].son[] == x;
}
inline bool nroot(int x)
{
return a[a[x].father].son[] == x | a[a[x].father].son[] == x;
}
inline void rotate(int x)
{
int f = a[x].father , g = a[f].father;
int tmpx = get(x) , tmpf = get(f);
int w = a[x].son[tmpx ^ ];
if (nroot(f)) a[g].son[tmpf] = x;
a[x].son[tmpx ^ ] = f;
a[f].son[tmpx] = w;
if (w) a[w].father = f;
a[f].father = x;
a[x].father = g;
update(f);
}
inline void splay(int x)
{
int y = x , z = ;
static int st[MAXN];
st[++z] = y;
while (nroot(y)) y = st[++z] = a[y].father;
while (z) pushdown(st[z--]);
while (nroot(x))
{
int y = a[x].father , z = a[y].father;
if (nroot(y))
rotate((a[z].son[] == y) ^ (a[y].son[] == x) ? x : y);
rotate(x);
}
update(x);
}
inline void access(int x)
{
for (int y = ; x; x = a[y = x].father)
{
splay(x);
a[x].son[] = y;
update(x);
}
}
inline void make_root(int x)
{
access(x);
splay(x);
a[x].tag ^= ;
pushdown(x);
}
inline int find_root(int x)
{
access(x);
splay(x);
while (a[x].son[])
{
pushdown(x);
x = a[x].son[];
}
return x;
}
inline void link(int x , int y)
{
make_root(x);
if (find_root(y) != x) a[x].father = y;
}
inline void split(int x , int y)
{
make_root(x);
access(y);
splay(y);
}
inline int query(int x , int y)
{
split(x , y);
return a[y].cnt;
}
inline bool connected(int x , int y)
{
return (find_root(x) == find_root(y));
}
inline void modify(int u , int x)
{
splay(u);
a[u].value = x;
update(u);
}
} LCT; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
int main()
{ scanf("%d" , &n);
for (int i = ; i <= n; i++) scanf("%d" , &val[i]);
LCT.init();
int q;
scanf("%d" , &q);
while (q--)
{
char type[];
scanf("%s" , type);
if (type[] == 'b')
{
int u , v;
scanf("%d%d" , &u , &v);
if (LCT.connected(u , v))
{
printf("no\n");
} else
{
printf("yes\n");
LCT.link(u , v);
}
} else if (type[] == 'p')
{
int u , x;
scanf("%d%d" , &u , &x);
LCT.modify(u , x);
} else
{
int u , v;
scanf("%d%d" , &u , &v);
if (!LCT.connected(u , v)) printf("impossible\n");
else printf("%d\n" , LCT.query(u , v));
}
} return ; }

[CROATIAN2009] OTOCI的更多相关文章

  1. BZOJ 1180: [CROATIAN2009]OTOCI [LCT]

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 961  Solved: 594[Submit][S ...

  2. BZOJ 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 989  Solved: 611[Submit][S ...

  3. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

  4. 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1032  Solved: 638[Submit][ ...

  5. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

  6. BZOJ1180: [CROATIAN2009]OTOCI

    传送门 一遍AC,开心! $Link-Cut-Tree$最后一题 //BZOJ 1180 //by Cydiater //2016.9.18 #include <iostream> #in ...

  7. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  8. BZOJ2843极地旅行社&BZOJ1180[CROATIAN2009]OTOCI——LCT

    题目描述 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作:  1.bridge A B:询问结点A与结点B是否连通. 如果是则输出“no”.否则输出“yes”,并且在 ...

  9. BZOJ1180 [CROATIAN2009]OTOCI LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1180 本题和BZOJ2843一样. BZOJ2843 极地旅行社 LCT 题意概括 有n座岛 每座 ...

  10. 【刷题】BZOJ 1180 [CROATIAN2009]OTOCI

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通. 如果是则输出"no&quo ...

随机推荐

  1. host dig nslookup bind

    这三个工具包含在yum install bind-utils -y dig -t mx|ns|A baidu.com qq.com dig -x 113,11.2.11 http://www.cnbl ...

  2. C++ ADO 连接 mysql

    1.安装mysql-5.0.22-win32,mysql-connector-odbc-5.1.12-win32.msi    然后:开始菜单->设置->控制面板->管理工具-> ...

  3. C++11 并发指南四(<future> 详解三 std::future & std::shared_future)(转)

    上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...

  4. Django-缓存的配置

    缓存的介绍 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次的的后台操作,都会消耗 ...

  5. 汇率换算自然语言理解功能JAVA DEMO

    >>>>>>>>>>>>>>>>>>>>>>>> 欢迎转 ...

  6. 下载与安装---tensorflow on linux

    http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/os_setup.html 你可以使用我们提供的 Pip, Docker, ...

  7. 横向卷轴(side-scrolling)地图的canvas实现

    标题有点小题大作了,实际上是实现一张看起来连续的运动背景图片. 效果如下:   // 实现原理: 图片1与图片2是两张首尾衔接的图片,图片1以一定速度移出canvas,图片2慢慢移进canvas,当图 ...

  8. 神经网络实现Discuz验证码识别

    最近自己尝试了网上的验证码识别代码项目,该小项目见以下链接: https://cuijiahua.com/blog/2018/01/dl_5.html 数据也就用了作者上传的60000张Discuz验 ...

  9. 安卓版本6.0打开uiautomator报错

    可能是appium打开了,被占用:或者是找不到手机

  10. iOS用户是否打开APP通知开关跳转到系统的设置界面

    1.检测用户是否打开推送通知  /** 系统通知是否打开 @return 是否打开 */ //检测通知是否打开iOS8以后有所变化 所以需要适配iOS7 + (BOOL)openThePushNoti ...