\(\color{#0066ff}{ 题目描述 }\)

在一个偏远的小镇上,有一些落后的山村。山村之间通过一些道路来连接。当然有的山村可能不连通。

一年当中会发生很多大事,比如说有人提议要在山村\(i\)与\(j\)之间修建一条道路,也有人觉得山村\(i\)和\(j\)之间的道路需要被拆掉。

由于小镇的落后,镇长不会允许存在两个山村\(i,j\),他们存在超过一条路径到达对方。也就是说,假如在修建山村\(i,j\)之间的道路之前,它们已经连通了,那么这条道路就不会被修建。

但拆除道路就不一样了。假如有人建议拆除连接\(i,j\)的道路,并且\(i,j\)的确有道路相连的话,镇长就会把它拆掉。

除了道路的修建与拆迁外,热情的山村人也会到处拜访其他人。有的时候来自山村\(i\)的人会想到山村\(j\)玩。

但山村人都是不识路的,那怎么办?他们有一种奇怪的遍历方式。

设一次旅行的起点为S,终点为T,点u的边集为V(i),那么这个走路过程可以用下面的伪代码来表示。

function DFS(u)
if u==T then
finish search
flag[u]<-true
random shuffle the vertices order in V(u)
//here all permutations have equal probability to be chosen
for i in V(u) do
if flag[i]==false then
count++;
DFS(i);
count++;

最后count就是这次旅行所花时间。

很显然对于一次旅行,count可能有多种取值,那么对于这次旅行时间的评估,就是count的期望。

对于每次旅行,你都要告诉山村人他这次旅行时间的评估是多少。

一开始所有的山村之间都是没有道路连接的。


update:伪代码好难看,来个cpp......

int count=0;
void dfs(int u)
{
if(u==T)cout<<count,exit(0);
flag[u]=true;
random_shuffle(V[u],V[u]+len[u]);
for(i=0;i<len[u];++i)
if(!flag[V[i]])count++,dfs(V[i]);
count++;
}

\(\color{#0066ff}{输入格式}\)

第一行两个整数\(N,Q\),表示小镇上总共有\(N\)个山村,一年中发生了\(Q\)件大事。

接下来\(Q\)行,每行包括三个整数\(type,u,v\)。

  • 若\(type=0\),表示有人建议在\(u,v\)之间修建一条道路。
  • 若\(type=1\),表示有人建议拆除\(u,v\)之间的道路。
  • 若\(type=2\),表示山村人要进行一次\(u\)出发到\(v\)结束的旅行。

\(\color{#0066ff}{输出格式}\)

输出共Q行。

对于第i件大事,若\(type=0\)或\(1\),假如这件大事能完成,则输出OK,否则输出ILLEGAL。若\(type=2\),假如这次旅行能到达终点,则输出对应的时间评估,否则输出ILLEGAL。

对于每个时间评估,输出保留4位小数。

\(\color{#0066ff}{输入样例}\)

4 9
0 1 2
0 2 4
0 4 1
2 1 4
0 2 3
2 1 4
1 4 1
1 3 2
2 1 3

\(\color{#0066ff}{输出样例}\)

OK
OK
ILLEGAL
2.0000
OK
3.0000
ILLEGAL
OK
ILLEGAL

\(\color{#0066ff}{数据范围与提示}\)

对于\(100\%\)的数据,\(N≤100000,Q≤300000,1≤u,v≤N\)

\(\color{#0066ff}{ 题解 }\)

这些人走的方式比较nb

简单来说,对于一条路径\(s\to t\)

↑图片来自FlashHu

如果进了某棵子树,那么一定会走完,代价就是进去再回来的总步数

我们设在当前点走正确方向的概率为p

那么答案为\(\begin{aligned}\sum_{i=1}^n2ps_i+\sum_{i=1}^m\sum_{j=1}^n2pa_{ij}\end{aligned}\)

现在考虑p是啥

因为边的生成方式是排列,对于一个排列,有且仅有一个排列与其相反,如果有一个排列使得某条边在另一条边前面,那么必定存在一个排列使得在后面,因此概率\(p=\frac 1 2\)

于是。。。TM答案是整数啊

我们用LCT维护子树,一个是虚子树,一个是总共的

发现答案中是没有t的子树什么事的

所以最后让T到最上面,用T的tot减去T的虚子树部分再减去1(自己)即可

应该是有不合法数据吧。。。在cut的时候如果不判断x是否有右孩子会WA

#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e5 + 10;
struct LCT {
protected:
struct node {
node *ch[2], *fa;
int tot, siz, rev;
node(int tot = 1, int siz = 0, int rev = 0)
: tot(tot), siz(siz), rev(rev) { ch[0] = ch[1] = fa = NULL; }
void trn() { std::swap(ch[0], ch[1]), rev ^= 1; }
void upd() {
tot = siz + 1;
if(ch[0]) tot += ch[0]->tot;
if(ch[1]) tot += ch[1]->tot;
}
void dwn() {
if(!rev) return;
if(ch[0]) ch[0]->trn();
if(ch[1]) ch[1]->trn();
rev = 0;
}
bool ntr() { return fa && (fa->ch[0] == this || fa->ch[1] == this); }
bool isr() { return this == fa->ch[1]; }
}pool[maxn];
void rot(node *x) {
node *y = x->fa, *z = y->fa;
bool k = x->isr(); node *w = x->ch[!k];
if(y->ntr()) z->ch[y->isr()] = x;
(x->ch[!k] = y)->ch[k] = w;
(y->fa = x)->fa = z;
if(w) w->fa = y;
y->upd(), x->upd();
}
void splay(node *o) {
static node *st[maxn];
int top;
st[top = 1] = o;
while(st[top]->ntr()) st[top + 1] = st[top]->fa, top++;
while(top) st[top--]->dwn();
while(o->ntr()) {
if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa);
rot(o);
}
}
void access(node *x) {
for(node *y = NULL; x; x = (y = x)->fa) {
splay(x);
if(x->ch[1]) x->siz += x->ch[1]->tot;
x->ch[1] = y;
if(y) x->siz -= y->tot;
x->upd();
}
}
void makeroot(node *o) { access(o), splay(o), o->trn(); }
node *findroot(node *o) {
access(o), splay(o);
while(o->dwn(), o->ch[0]) o = o->ch[0];
return o;
}
public:
bool link(int l, int r) {
node *x = pool + l, *y = pool + r;
if(findroot(y) == findroot(x)) return false;
makeroot(x), access(y), splay(y);
(x->fa = y)->siz += x->tot;
y->upd();
return true;
}
bool cut(int l, int r) {
node *x = pool + l, *y = pool + r;
if(findroot(x) != findroot(y)) return false;
makeroot(x), access(y), splay(y);
if(y->ch[0] == x && !x->ch[1]) return y->ch[0] = x->fa = NULL, true;
return false;
}
int query(int l, int r) {
node *x = pool + l, *y = pool + r;
if(findroot(y) != findroot(x)) return -1;
makeroot(x), access(y), splay(y);
return y->tot - y->siz - 1;
}
}s;
int n, m;
int main() {
n = in(), m = in();
int p, x, y;
while(m --> 0) {
p = in(), x = in(), y = in();
if(p == 0) printf(s.link(x, y)? "OK\n" : "ILLEGAL\n"); if(p == 1) printf(s.cut(x, y)? "OK\n" : "ILLEGAL\n");
if(p == 2) {
x = s.query(x, y);
if(x == -1) printf("ILLEGAL\n");
else printf("%d.0000\n", x);
}
}
return 0;
}

U19464 山村游历(Wander) LCT维护子树大小的更多相关文章

  1. 洛谷U19464 山村游历(Wander)(LCT,Splay)

    洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...

  2. 洛谷U19464 山村游历(Wander)(LCT)

    洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...

  3. 洛谷.U19464.山村游行wander(LCT 伪期望)

    题目链接 题意: 森林,动态建边.删边,询问从S开始走到T的期望时间.走位: 每次人会随机地选一条未走过的边走,走到无路可走,再退回.这样直到终点T.走一条边.从一条边退回都花费时间1. 题目特点是走 ...

  4. P4219 [BJOI2014]大融合 LCT维护子树大小

    \(\color{#0066ff}{ 题目描述 }\) 小强要在\(N\)个孤立的星球上建立起一套通信系统.这套通信系统就是连接\(N\)个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一 ...

  5. LCT维护子树信息

    有些题目,在要求支持link-cut之外,还会在线询问某个子树的信息.LCT可以通过维护虚边信息完成这个操作. 对于LCT上每个节点,维护两个两sz和si,后者维护该点所有虚儿子的信息,前者维护该点的 ...

  6. 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息

    题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够联通的树上路过它的简单路径的数量 ...

  7. 【bzoj3510】首都 LCT维护子树信息(+启发式合并)

    题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...

  8. 【BZOJ3510】首都 LCT维护子树信息+启发式合并

    [BZOJ3510]首都 Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打 ...

  9. bzoj3510 首都 LCT 维护子树信息+树的重心

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3510 题解 首先每一个连通块的首都根据定义,显然就是直径. 然后考虑直径的几个性质: 定义:删 ...

随机推荐

  1. python使用pyodbc连接sql server 2008

    一.PyODBC的下载地址: http://code.google.com/p/pyodbc/ 二.测试语句 import pyodbccnxn = pyodbc.connect(DRIVER='{S ...

  2. Unencapsulate SVM root mirror (ZT)

    http://www.seedsofgenius.net/solaris/quick-reference-unencapsulate-svm-root-mirror Often times admin ...

  3. oracle——pl/sql 查询中文乱码

    1.查看服务器端编码select userenv('language') from dual;我实际查到的结果为:AMERICAN_AMERICA.AL32UTF82.执行语句 select * fr ...

  4. 问题:Oracle to_date;结果:oracle常用的时间格式转换

    oracle常用的时间格式转换 1:取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; T ...

  5. gridcontrol 添加行号

    private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndic ...

  6. Shiro权限框架简介

    http://blog.csdn.net/xiaoxian8023/article/details/17892041   Shiro权限框架简介 2014-01-05 23:51 3111人阅读 评论 ...

  7. python操作excel的读写

    1.下载xlrd和xlwt pip install xlwd pip install xlrd pip install xlutils 2.读写操作(已存在的excel) #-*- coding:ut ...

  8. android手机分辨率的一些说明

    Android上常见度量单位 px(像素):屏幕上的点,绝对长度,与硬件相关 in(英寸):长度单位 mm(毫米):长度单位 pt(磅):1/72英寸,point dp(与密度无关的像素):一种基于屏 ...

  9. Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练

    # coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...

  10. python3-字典的循环

    # Auther: Aaron Fan info = { 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex ...