HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)
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.
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.
定位:中等题 LCT和树链剖分都可以做,简单起见就说说树链剖分,LCT类似。
首先注意到一条路径上是log n条重链和log n条轻边。
当我们给一个点的所有边上的邻居边打标记时,如果该点在重链上,会影响他的轻孩子们。(要特殊考虑链头和链尾之类的)。
那么我们可以给重链上的点打标记,表示这个点的轻孩子们被翻转了没。
那么一个轻边就可以直接询问出来。复杂度:O(n(logn)^2)。
这个官方题解不怎么详细,我说一下我的解法:
首先跟普通的树链剖分一样,先给每个结点编上一个编号,每条重链用一个线段树来维护(其实都放到同一个线段树中了,占用不同的位置)
现在用线段树维护3个值:sum代表这个区间的黑边总数,flip代表这个区间的边是否被翻转颜色,light代表这个区间是否被打上了翻转标记(后面再说这个是啥)
其中所有边的编号为其靠下方的结点的编号,也就是根的编号在边的维护中没有意义
其中light代表这个点作为操作②所影响的次数,即它的轻边(不考虑与父节点的边)被翻转次数的奇偶。
维护这些值,考虑重链上的重边,某条边是黑色当且仅当它在线段树中的flip是1(被翻转了奇数次)。而对于不在重链上的边,它是黑色当且仅当它在线段树中的flip(被翻转的次数奇偶性)和它的父亲被打上翻转标记的次数light的和是奇数(异或值为1)。
那么,对于操作①,在树链往上走的时候,把所有边都翻转一次即可,没有什么难度。
对于操作②,在树链往上走的时候,除了要给所有路径上的点打上翻转标记以外,还要:翻转重链上的孩纸边,因为它要翻转而不受翻转标记的影响;翻转重链的父边,它是轻边,会受到重链的父亲的翻转标记的影响,所以直接给翻转一次抵消这种影响。
对于操作③,在树链往上走的时候,统计重链上的黑边和sum,和不在重链上的黑边(上面有讲)。
然后这样就差写代码了。
#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();
}
}
}
HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)的更多相关文章
- hdu 4897 Little Devil I (树链剖分+线段树)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...
- HDU 4897 Little Devil I 树链剖分+线段树
Little Devil I Problem Description There is an old country and the king fell in love with a devil. T ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- hdu_4897_Little Devil I(树链剖分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意:有三种操作,1是在树上的两个节点之间的路径改变当前的颜色,2是改变树上有且只有一个端点在u ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- BZOJ3862Little Devil I——树链剖分+线段树
题目大意: 给一棵树,每条边可能是黑色或白色(起始都是白色),有三种操作: 1.将u到v路径上所有边颜色翻转(黑->白,白->黑) 2.将只有一个点在u到v路径上的边颜色翻转 3.查询u到 ...
- HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】ACdream 1429——Diversion——————【树链剖分】
Minimum Cut Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Tota ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
随机推荐
- WIN API 擦除所绘图像
COLORREF circle_color = RGB(0, 105, 255); //获取窗口DC HDC hdc = GetDC(hWnd_); //背景色透明 SetBkMode(hdc, TR ...
- BLE GATT规范中关于UUID与属性的详细解析
1. 角色 除了GAP定义了角色之外,BLE还定义了另外2种角色:GATT服务器和GATT客户端,它们完全独立于GAP的角色.提供数据的设备称为GATT服务器,访问GATT服务器而获得数据的设备称为G ...
- ubuntu Nodejs和npm的安装
cnpm install -g XXX errors : npm i --registry=https://registry.npm.taobao.org 标签: nodejsnpm 20 ...
- Memcached Java Client API详解
针对Memcached官方网站提供的java_memcached-release_2.0.1版本进行阅读分析,Memcached Java客户端lib库主要提供的调用类是SockIOPool和MemC ...
- MVC HTML辅助类常用方法记录
(1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(modelItem => item.Titl ...
- cordova插件iOS平台实战开发注意点
cordova插件是其设计理念的精髓部分,创建并使用自定义插件也是一件比较容易的事.但在这个过程中也容易进入一些误区或者有一些错误的理解,下面从笔者实际开发中遇到的问题出发,对其中的一些注意点和重要概 ...
- HIVE中的HQL操作
1.字段查询 select empno,ename from emp; 2.过滤where,limit,distinct select * from emp where sal >2500; s ...
- UIPickerView 循环滚动(一种假象)
因为网上没有查到相关方法,故而采用一种假象的方法来实现,选项循环滚动 - (void)viewDidLoad { [super viewDidLoad]; /** UIPickerView 选择器 * ...
- sqlserver 一个排序问题
当 应用select * into a from b order by b1,b2语句时,试图使a表中的物理顺序改变,是 不能够实现的 select * into 同时复制了b表的物理结构,所以a表中 ...
- Android Keycode详解
用JAVA写appium的testcase时,想用Android自带的物理返回键,网上找了下分享给大家. import io.appium.java_client.android.AndroidKey ...