省队集训 Day7 选点游戏
【题目大意】
维护一个$n$个点的图,$m$个操作,支持两个操作:
1. 连接$(u, v)$这条边;
2. 询问$u$所在的联通块中,能选出的最大合法的点数。
一个方案是合法的,当且仅当对于所有被选择的点,他们都没有直接通过一条边相连。
$n \leq 2*10^5, m\leq 8*10^5$
【题解】
考虑用LCT来维护这个图。
对于实边的Splay,维护$h_it_j$,其中$i, j \in \{0, 1\}$,表示前面选择$i$,后面选择$j$。(选0表示不选,选1表示选)
这个是可以合并信息的。
考虑虚边,对于每个虚边连到的实边的点,维护$l_i$,其中$i \in \{0,1\}$,分别表示$l_i$必须为0,以及$l_i$可以为0的值。
那么$l_i$可以通过连的虚边的值转移来。
那么连到的实边的点的val就可以用$l_i$表示。
在access的时候和link的时候(link可以用两个makeroot来做)支持实边虚边转换即可。
# include <stdio.h>
# include <assert.h>
# include <iostream>
# include <string.h>
# include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef long double ld; inline int getint() {
int x = ; char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) x = (x<<) + (x<<) + ch - '', ch = getchar();
return x;
} const int N = 2e5 + , M = 5e5 + , inf = 1e9; int n, m; struct node {
int h0t0, h0t1, h1t0, h1t1;
node () {}
node (int h0t0, int h0t1, int h1t0, int h1t1) : h0t0(h0t0), h0t1(h0t1), h1t0(h1t0), h1t1(h1t1) {}
inline friend node operator + (node a, node b) {
// a tail with b head
node p;
p.h0t0 = max(a.h0t1 + b.h0t0, a.h0t0 + max(b.h1t0, b.h0t0));
p.h0t1 = max(a.h0t1 + b.h0t1, a.h0t0 + max(b.h1t1, b.h0t1));
p.h1t0 = max(a.h1t1 + b.h0t0, a.h1t0 + max(b.h1t0, b.h0t0));
p.h1t1 = max(a.h1t1 + b.h0t1, a.h1t0 + max(b.h1t1, b.h0t1));
return p;
}
inline int gans() {
return max(max(h0t0, h0t1), max(h1t0, h1t1));
}
inline void prt() {
printf("{%d %d %d %d}", h0t0, h0t1 == -inf ? - : h0t1, h1t0 == -inf ? - : h1t0, h1t1);
}
}; struct LCT {
node p[N], val[N]; int l0[N], l1[N];
int ch[N][], fa[N];
bool rev[N]; # define ls ch[x][]
# define rs ch[x][] inline void up(int x) {
if(!x) return ;
// ...
p[x] = val[x];
if(ls) p[x] = p[ls] + p[x];
if(rs) p[x] = p[x] + p[rs];
} inline void pushrev(int x) {
if(!x) return ;
swap(ch[x][], ch[x][]);
// ...
swap(p[x].h1t0, p[x].h0t1);
rev[x] ^= ;
} inline void down(int x) {
if(!x) return ;
if(rev[x]) {
pushrev(ls);
pushrev(rs);
rev[x] ^= ;
}
} # undef ls
# undef rs inline bool isrt(int x) {
return ch[fa[x]][] != x && ch[fa[x]][] != x;
} inline void rotate(int x) {
int y = fa[x], z = fa[y], ls = ch[y][] == x, rs = ls ^ ;
if(!isrt(y)) ch[z][ch[z][] == y] = x;
fa[ch[x][rs]] = y, fa[y] = x, fa[x] = z;
ch[y][ls] = ch[x][rs]; ch[x][rs] = y;
up(y); up(x);
} int st[N];
inline void splay(int x) {
int stn = , tx = x;
while(!isrt(tx)) st[++stn] = tx, tx = fa[tx];
st[++stn] = tx;
for (int i=stn; i; --i) down(st[i]);
while(!isrt(x)) {
int y = fa[x], z = fa[y];
if(!isrt(y)) {
if((ch[z][] == y) ^ (ch[y][] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
} inline void del(int y, int x) {
int p0 = max(p[x].h0t0, p[x].h0t1), p1 = max(p[x].h1t0, p[x].h1t1);
l0[y] += p0;
l1[y] += max(p0, p1);
}
inline void ins(int y, int x) {
int p0 = max(p[x].h0t0, p[x].h0t1), p1 = max(p[x].h1t0, p[x].h1t1);
l0[y] -= p0;
l1[y] -= max(p0, p1);
} inline int access(int x) {
int t = ;
for (; x; t = x, x = fa[x]) {
splay(x);
// ...
if(ch[x][]) del(x, ch[x][]);
if(t) ins(x, t);
val[x].h0t0 = l1[x];
val[x].h1t1 = l0[x] + ;
ch[x][] = t;
up(x);
}
return t;
} inline void makeroot(int x) {
access(x); splay(x); pushrev(x);
} inline void link(int x, int y) {
makeroot(x); makeroot(y);
fa[x] = y;
// ...
int p0 = max(p[x].h0t0, p[x].h0t1), p1 = max(p[x].h1t0, p[x].h1t1);
l0[y] += p0;
l1[y] += max(p0, p1);
val[y].h0t0 = l1[y];
val[y].h1t1 = l0[y] + ;
up(y);
} inline void debug() {
for (int x=; x<=n; ++x) {
printf("x = %d,fa = %d,ls = %d,rs = %d, p = ", x, fa[x], ch[x][], ch[x][]);
p[x].prt();
printf(", val = ");
val[x].prt();
printf(", l0 = %d,l1 = %d\n", l0[x], l1[x]);
}
}
}T; int main() {
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
n = getint();
for (int x=; x<=n; ++x) {
T.val[x] = T.p[x] = node(, -inf, -inf, );
T.ch[x][] = T.ch[x][] = T.fa[x] = ; T.rev[x] = ;
T.l0[x] = T.l1[x] = ;
}
register int Q = getint(), op, u, v;
while(Q--) {
// T.debug();
op = getint(), u = getint();
if(op == ) {
T.makeroot(u);
printf("%d\n", T.p[u].gans());
// cout << ... << endl;
} else {
v = getint();
T.link(u, v);
}
}
return ;
}
省队集训 Day7 选点游戏的更多相关文章
- HN2018省队集训
HN2018省队集训 Day1 今天的题目来自于雅礼的高二学长\(dy0607\). 压缩包下载 密码: 27n7 流水账 震惊!穿着该校校服竟然在四大名校畅通无阻?霸主地位已定? \(7:10\)从 ...
- JS省队集训记
不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...
- 「2017 山东三轮集训 Day7 解题报告
「2017 山东三轮集训 Day7」Easy 练习一下动态点分 每个点开一个线段树维护子树到它的距离 然后随便查询一下就可以了 注意线段树开大点... Code: #include <cstdi ...
- [2018HN省队集训D9T1] circle
[2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...
- 【LOJ6077】「2017 山东一轮集训 Day7」逆序对 生成函数+组合数+DP
[LOJ6077]「2017 山东一轮集训 Day7」逆序对 题目描述 给定 n,k ,请求出长度为 n的逆序对数恰好为 k 的排列的个数.答案对 109+7 取模. 对于一个长度为 n 的排列 p ...
- [2018HN省队集训D8T1] 杀毒软件
[2018HN省队集训D8T1] 杀毒软件 题意 给定一个 \(m\) 个01串的字典以及一个长度为 \(n\) 的 01? 序列. 对这个序列进行 \(q\) 次操作, 修改某个位置的字符情况以及查 ...
- [2018HN省队集训D8T3] 水果拼盘
[2018HN省队集训D8T3] 水果拼盘 题意 给定 \(n\) 个集合, 每个集合包含 \([1,m]\) 中的一些整数, 在这些集合中随机选取 \(k\) 个集合, 求这 \(k\) 个集合的并 ...
- [2018HN省队集训D6T2] girls
[2018HN省队集训D6T2] girls 题意 给定一张 \(n\) 个点 \(m\) 条边的无向图, 求选三个不同结点并使它们两两不邻接的所有方案的权值和 \(\bmod 2^{64}\) 的值 ...
- [Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform
[Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform 题意 给定一个小写字母构成的字符串, 每个字符有一个非负权值. 输出所有满足权值和等于这个子串在所有本质 ...
随机推荐
- 【week2】四人小组项目(WBS、NABCD)
项目选题:东北师范大学论坛 小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 本周任务:要求给出需求概述.功能列表.痛点或亮点.NABCD及WBS模型在此项目中的应用. 作为东北师范大学 ...
- win7 php连接远程oracle
<?php /* 先下载oracle客户端 下载地址 http://www.oracle.com/technetwork/topics/winx64soft-089540.html 下载如下三个 ...
- tomcat8配置管理员后仍然报403
tomcat8配置管理员后仍然报403 修改conf/tomcat-users.xml <role rolename="manager"/> <role ro ...
- [计算机网络] TCP的拥塞控制
引言 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就是防止过多 ...
- [计算机网络-应用层] DNS:因特网的目录服务
我们知道有两种方式可以识别主机:通过主机名或者IP地址.人们喜欢便于记忆的主机名标识,而路由器则喜欢定长的.有着层次结构的IP地址.为了折中这些不同的偏好,我们需要一种能进行主机名到IP地址转换的目录 ...
- TScreen 类
TScreen表示应用程序运行时屏幕的状态. 类关系 TObject->TPersistent->TComponent TScreen引进具有表示下列各种情况的属性 什么窗体和数据模块已经 ...
- poj3164-Command Network
给出平面上一些点,和连接它们的带权有向边,求把所有点连起来的最小总权值. 分析 由于这里边是有向的(unidirectional),所以这是经典的最小树形图问题,可以说是最小树形图的模板题. 代码 这 ...
- Luogu1731 NOI1999生日蛋糕(搜索)
非常经典的剪枝题然而一直没有写.感觉自己连普及组水平都没有了. 1.半径和高枚举范围满足加上后总体积不超过n且剩下每层还能放. 2.半径从大到小枚举,因为体积正比于半径平方而面积正比于半径,大的半径更 ...
- Android SDK Manager下载,解决方案
一.Windows 平台 在C:\Windows\System32\drivers\etc\hosts文件.添加一行:74.125.237.1 dl-ssl.google.com 二.Li ...
- cmder 添加到右键菜单
管理员权限打开cmde 输入: cmder /register all 回车,OK