HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)

时间:2024-01-06 22:44:26
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897
Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

The devil likes to make thing in chaos. This kingdom’s road system is like simply a tree(connected graph without cycle). A road has a color of black or white. The devil often wants to make some change of this system.

In details, we call a path on the tree from a to b consists of vertices lie on the shortest simple path between a and b. And we say an edge is on the path if both its two endpoints is in the path, and an edge is adjacent to the path if exactly one endpoint of it is in the path.

Sometimes the devil will ask you to reverse every edge’s color on a path or adjacent to a path.

The king’s daughter, WJMZBMR, is also a cute loli, she is surprised by her father’s lolicon-like behavior. As she is concerned about the road-system’s status, sometimes she will ask you to tell there is how many black edge on a path.

Initially, every edges is white.

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains an integer n, which is the size of the tree. The vertices be indexed from 1.
On the next n-1 lines, each line contains two integers a,b, denoting there is an edge between a and b. 
The next line contains an integer Q, denoting the number of the operations.
On the next Q lines, each line contains three integers t,a,b. t=1 means we reverse every edge’s color on path a to b. t=2 means we reverse every edge’s color adjacent to path a to b. t=3 means we query about the number of black edge on path a to b.

T<=5.
n,Q<=10^5.
Please use scanf,printf instead of cin,cout,because of huge input.

Output
For each t=3 operation, output the answer in one line.
题目大意:给一棵树,每条边可以是黑色或者白色,一开始所有边都是白色的。现在有三种操作:①把 a 和 b 之间的简单路径上的所有边的颜色翻转。②把与 a 和 b 之间的简单路径相邻的所有边的颜色翻转。③询问 a 和 b 之间的简单路径上有多少条黑边。
思路:先贴一段CLJ的官方题解:

定位:中等题 LCT和树链剖分都可以做,简单起见就说说树链剖分,LCT类似。

首先注意到一条路径上是log n条重链和log n条轻边。

当我们给一个点的所有边上的邻居边打标记时,如果该点在重链上,会影响他的轻孩子们。(要特殊考虑链头和链尾之类的)。

那么我们可以给重链上的点打标记,表示这个点的轻孩子们被翻转了没。

那么一个轻边就可以直接询问出来。复杂度:O(n(logn)^2)。

这个官方题解不怎么详细,我说一下我的解法:

首先跟普通的树链剖分一样,先给每个结点编上一个编号,每条重链用一个线段树来维护(其实都放到同一个线段树中了,占用不同的位置)

现在用线段树维护3个值:sum代表这个区间的黑边总数,flip代表这个区间的边是否被翻转颜色,light代表这个区间是否被打上了翻转标记(后面再说这个是啥)

其中所有边的编号为其靠下方的结点的编号,也就是根的编号在边的维护中没有意义

其中light代表这个点作为操作②所影响的次数,即它的轻边(不考虑与父节点的边)被翻转次数的奇偶。

维护这些值,考虑重链上的重边,某条边是黑色当且仅当它在线段树中的flip是1(被翻转了奇数次)。而对于不在重链上的边,它是黑色当且仅当它在线段树中的flip(被翻转的次数奇偶性)和它的父亲被打上翻转标记的次数light的和是奇数(异或值为1)。

那么,对于操作①,在树链往上走的时候,把所有边都翻转一次即可,没有什么难度。

对于操作②,在树链往上走的时候,除了要给所有路径上的点打上翻转标记以外,还要:翻转重链上的孩纸边,因为它要翻转而不受翻转标记的影响;翻转重链的父边,它是轻边,会受到重链的父亲的翻转标记的影响,所以直接给翻转一次抵消这种影响。

对于操作③,在树链往上走的时候,统计重链上的黑边和sum,和不在重链上的黑边(上面有讲)。

然后这样就差写代码了。

代码(2515MS):
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXV = ;
const int MAXE = MAXV << ;
const int MAXT = MAXV << ; int head[MAXV], ecnt;
int to[MAXE], next[MAXE];
int n, m, T;
//Graph
void initGraph() {
memset(head + , -, n * sizeof(int));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
}
//Segment Tree
#define ll (x << 1)
#define rr (ll | 1)
#define mid ((l + r) >> 1)
int sum[MAXT], flip[MAXT];
int light[MAXT];//轻边是否翻转 void initSegmentTree() {
memset(sum + , , * n * sizeof(int));
memset(flip + , , * n * sizeof(int));
memset(light + , , * n * sizeof(int));
} void pushdown(int x, int l, int r) {
if(flip[x]) {
flip[ll] ^= ;
sum[ll] = (mid - l + ) - sum[ll];
flip[rr] ^= ;
sum[rr] = (r - mid) - sum[rr];
flip[x] = ;
}
if(light[x]) {
light[ll] ^= ;
light[rr] ^= ;
light[x] = ;
}
} void maintain(int x) {
sum[x] = sum[ll] + sum[rr];
} void modifyFlip(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
flip[x] ^= ;
sum[x] = (r - l + ) - sum[x];
} else {
pushdown(x, l, r);
if(a <= mid) modifyFlip(ll, l, mid, a, b);
if(mid < b) modifyFlip(rr, mid + , r, a, b);
maintain(x);
}
} void modifyLight(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
light[x] ^= ;
} else {
pushdown(x, l, r);
if(a <= mid) modifyLight(ll, l, mid, a, b);
if(mid < b) modifyLight(rr, mid + , r, a, b);
}
} int queryFlip(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
return sum[x];
} else {
int res = ;
pushdown(x, l, r);
if(a <= mid) res += queryFlip(ll, l, mid, a, b);
if(mid < b) res += queryFlip(rr, mid + , r, a, b);
return res;
}
} int queryLight(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
return light[x];
} else {
int res = ;
pushdown(x, l, r);
if(a <= mid) res += queryLight(ll, l, mid, a, b);
if(mid < b) res += queryLight(rr, mid + , r, a, b);
return res;
}
}
//树链剖分
int fa[MAXV], size[MAXV], son[MAXV], top[MAXV], tid[MAXV], dep[MAXV];
int dfs_clock; void dfs_size(int u, int f, int depth) {
fa[u] = f; dep[u] = depth;
size[u] = ; son[u] = ;
int maxsize = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs_size(v, u, depth + );
size[u] += size[v];
if(size[v] > maxsize) {
son[u] = v;
maxsize = size[v];
}
}
} void dfs_heavy_edge(int u, int ancestor) {
tid[u] = ++dfs_clock; top[u] = ancestor;
if(son[u]) dfs_heavy_edge(son[u], ancestor);
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == fa[u] || v == son[u]) continue;
dfs_heavy_edge(v, v);
}
} void modifyFlip(int a, int b) {
while(top[a] != top[b]) {
if(dep[top[a]] < dep[top[b]]) swap(a, b);
modifyFlip(, , n, tid[top[a]], tid[a]);
a = fa[top[a]];
}
if(a != b) {
if(dep[a] < dep[b]) swap(a, b);
modifyFlip(, , n, tid[son[b]], tid[a]);
}
} void modifyLight(int a, int b) {
while(top[a] != top[b]) {
if(dep[top[a]] < dep[top[b]]) swap(a, b);
modifyLight(, , n, tid[top[a]], tid[a]);
if(son[a]) modifyFlip(, , n, tid[son[a]], tid[son[a]]);
modifyFlip(, , n, tid[top[a]], tid[top[a]]);
a = fa[top[a]];
}
if(dep[a] < dep[b]) swap(a, b);
modifyLight(, , n, tid[b], tid[a]);
if(fa[b]) modifyFlip(, , n, tid[b], tid[b]);
if(son[a]) modifyFlip(, , n, tid[son[a]], tid[son[a]]);
} int query(int a, int b) {
int res = ;
while(top[a] != top[b]) {
if(dep[top[a]] < dep[top[b]]) swap(a, b);
if(a != top[a]) res += queryFlip(, , n, tid[son[top[a]]], tid[a]);
res += queryFlip(, , n, tid[top[a]], tid[top[a]]) ^ queryLight(, , n, tid[fa[top[a]]], tid[fa[top[a]]]);
a = fa[top[a]];
}
if(a != b) {
if(dep[a] < dep[b]) swap(a, b);
res += queryFlip(, , n, tid[son[b]], tid[a]);
}
return res;
} void ask() {
int a, b;
while(cin>>a>>b && a + b) {
cout<<query(a, b)<<endl;
}
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
initGraph();
for(int i = , a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
add_edge(a, b);
}
initSegmentTree();
dfs_clock = ;
dfs_size(, , );
dfs_heavy_edge(, );
scanf("%d", &m);
for(int i = , op, a, b; i < m; ++i) {
scanf("%d%d%d", &op, &a, &b);
if(op == ) modifyFlip(a, b);
if(op == ) modifyLight(a, b);
if(op == ) printf("%d\n", query(a, b));
//if(i == 1) ask();
}
}
}