【题目大意】

维护一个$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 选点游戏的更多相关文章

  1. HN2018省队集训

    HN2018省队集训 Day1 今天的题目来自于雅礼的高二学长\(dy0607\). 压缩包下载 密码: 27n7 流水账 震惊!穿着该校校服竟然在四大名校畅通无阻?霸主地位已定? \(7:10\)从 ...

  2. JS省队集训记

    不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...

  3. 「2017 山东三轮集训 Day7 解题报告

    「2017 山东三轮集训 Day7」Easy 练习一下动态点分 每个点开一个线段树维护子树到它的距离 然后随便查询一下就可以了 注意线段树开大点... Code: #include <cstdi ...

  4. [2018HN省队集训D9T1] circle

    [2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...

  5. 【LOJ6077】「2017 山东一轮集训 Day7」逆序对 生成函数+组合数+DP

    [LOJ6077]「2017 山东一轮集训 Day7」逆序对 题目描述 给定 n,k ,请求出长度为 n的逆序对数恰好为 k 的排列的个数.答案对 109+7 取模. 对于一个长度为 n 的排列 p ...

  6. [2018HN省队集训D8T1] 杀毒软件

    [2018HN省队集训D8T1] 杀毒软件 题意 给定一个 \(m\) 个01串的字典以及一个长度为 \(n\) 的 01? 序列. 对这个序列进行 \(q\) 次操作, 修改某个位置的字符情况以及查 ...

  7. [2018HN省队集训D8T3] 水果拼盘

    [2018HN省队集训D8T3] 水果拼盘 题意 给定 \(n\) 个集合, 每个集合包含 \([1,m]\) 中的一些整数, 在这些集合中随机选取 \(k\) 个集合, 求这 \(k\) 个集合的并 ...

  8. [2018HN省队集训D6T2] girls

    [2018HN省队集训D6T2] girls 题意 给定一张 \(n\) 个点 \(m\) 条边的无向图, 求选三个不同结点并使它们两两不邻接的所有方案的权值和 \(\bmod 2^{64}\) 的值 ...

  9. [Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform

    [Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform 题意 给定一个小写字母构成的字符串, 每个字符有一个非负权值. 输出所有满足权值和等于这个子串在所有本质 ...

随机推荐

  1. Hadoop 版本 生态圈 MapReduce模型

    忘的差不多了, 先补概念, 然后开始搭建集群实战 ... . 一 Hadoop版本 和 生态圈 1. Hadoop版本 (1) Apache Hadoop版本介绍 Apache的开源项目开发流程 : ...

  2. 第二次作业 编程题 PAT 1001A+B Format

    Github的object-oriented仓库:1001.A+BFormat(20) 1.解题的思路过程 在之前学习C语言时曾经碰到过类似的将数字转换成字符输出的情况,这道题目要求输出的数字每三个间 ...

  3. LintCode-50.数组剔除元素后的乘积

    数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出A=[1 ...

  4. windows与linux下执行.class(包含main方法)

    来源:http://blog.csdn.net/hanqunfeng/article/details/4327325 一般来说,执行一个java文件采用执行jar包的方式最为方便(java -jar ...

  5. solr 学习之solrJ

    solrJ是访问Solr服务的JAVA客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过solrJ的API接口操作Solr服务. <!-- https://mvnreposi ...

  6. matlab函数列表(A~Z)【转】

    A a abs 绝对值.模.字符的ASCII码值acos 反余弦acosh 反双曲余弦acot 反余切acoth 反双曲余切acsc 反余割acsch 反双曲余割align 启动图形对象几何位置排列工 ...

  7. Luogu1053 NOIP2005篝火晚会

    首先造出所要求的得到的环.如果将位置一一对应上,答案就是不在所要求位置的人数.因为显然这是个下界,并且脑补一下能构造出方案达到这个下界. 剩下的问题是找到一种对应方案使错位数最少.可以暴力旋转这个环, ...

  8. [洛谷P4512]【模板】多项式除法

    题目大意:给定一个$n$次多项式$F(x)$和一个$m$次多项式$G(x)$,请求出多项式$Q(x),R(x)$,满足: 1. $Q(x)$次数为$n-m$,$R(x)$次数小于$m$2. $F(x) ...

  9. Redux的State不应该全部放在Store里

    使用了redux管理应用的状态,应用的状态不应该全部放在Store里面. 前端状态主要有一下两种: 1. Domain data 2. UI State 1. Domain data 来自于服务端对领 ...

  10. React的this.props.children

    this.props用来获取组件从外部传入的属性,但是this.props.children比较特殊,它是由React给添加上的,表示组件的所有子节点.this.props.children可以用来读 ...