codeforces 165D.Beard Graph 解题报告
题意:
给一棵树,树的每条边有一种颜色,黑色或白色,一开始所有边均为黑色,有两个操作:
操作1:将第i条边变成白色或将第i条边变成黑色。
操作2 :询问u,v两点之间仅经过黑色变的最短距离。
树链剖分+树状数组
学习树链剖分:
/*
树链剖分:
划分轻重链,效果是将一颗树变成了若干段连续的区间。
向上记录边权
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; const int MAX = ;
//子树结点数,所在链链头,同链儿子,结点深度,节点在区间的位置,父节点
int siz[MAX], top[MAX], son[MAX], dep[MAX], w[MAX], fa[MAX];
//链式前向星记录一颗树
struct Edge {
int v, ne, id;
} edge[MAX << ]; int head[MAX], cnt, num;
int vis[MAX], pos[MAX];;
//第一次dfs,提取基本信息,划分轻重链
void Dfs1 (int u, int v) {
fa[v] = u, dep[v] = dep[u] + ;
siz[v] = ;
vis[v] = ;
int tem = , p = -;
for (int i = head[v]; i != ; i = edge[i].ne) {
int kid = edge[i].v;
if (!vis[kid]) {
Dfs1 (v, kid);
siz[u] += siz[kid];
if (tem < siz[kid]) tem = siz[kid], p = kid;
}
}
son[v] = p;
}
//第二次DFS,将重链映射到区间
void Dfs2 (int h, int v) {
top[v] = h;
w[v] = ++num;
vis[v] = ;
if (son[v] != -) Dfs2 (h, son[v]);
for (int i = head[v]; i != ; i = edge[i].ne) {
int kid = edge[i].v;
if (son[v] != kid && !vis[kid])
Dfs2 (kid, kid);
pos[edge[i].id] = w[kid]; //边映射到下结点
}
} void addE (int u, int v, int num) {
edge[++cnt].v = v, edge[cnt].id = num;
edge[cnt].ne = head[u], head[u] = cnt;
} int A[MAX]; //pos为边的新编号
void add (int x, int k) {
for (; x <= num; x += x & -x) A[x] += k;
}
int getsum (int x) {
int s = ;
for (; x > ; x -= x & -x) s += A[x];
return s;
}
void modify (int x, int k) {
x = pos[x];
int sta = getsum (x) - getsum (x - );
if (sta == k) return;
else add (x, k - sta);
} void query (int u, int v) {
int s = , f1 = top[u], f2 = top[v], len = ;
while (f1 != f2) {
if (dep[f1] < dep[f2]) {
int y = w[v], x = w[f2];
if (f2 != v) {
if (s += getsum (y) - getsum (x) ) break;
v = f2;
len += y - x;
}
if (s += getsum (x) - getsum (x - ) ) break;
v = fa[v], f2 = top[v];
len++;
}
else {
int y = w[u], x = w[f1];
if (f1 != u) {
if (s += getsum (y) - getsum (x) ) break;
u = f1;
len += y - x;
}
if (s += getsum (x) - getsum (x - ) ) break;
u = fa[u], f1 = top[u];
len++;
}
}
int y = w[v], x = w[u];
s += getsum (max (y, x) ) - getsum (min (y, x) );
len += abs (y - x);
printf ("%d\n", s == ? len : -);
}
int n, u, v, m, t;
int main() {
scanf ("%d", &n);
for (int i = ; i < n; i++) {
scanf ("%d %d", &u, &v);
addE (u, v, i), addE (v, u, i);
}
Dfs1 (, );
memset (vis, , sizeof vis);
Dfs2 (, );
scanf ("%d", &m);
int k, l, r,tol=;
for (int i = ; i <= m; i++) {
scanf ("%d", &k);
if (k != ) {
scanf ("%d", &t);
modify (t, k == ? : );
}
if (k == ) {
scanf ("%d %d", &l, &r);
query (l, r);
} }
}
codeforces 165D.Beard Graph 解题报告的更多相关文章
- Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- 【LeetCode】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- codeforces 591A. Wizards' Duel 解题报告
题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- codeforces 581C. Developing Skills 解题报告
题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...
随机推荐
- OA系统配置文件
第一章 web.xml配置文件解读 1. web.xml文件解读 lemon OA系统的核心配置文件都放在spring目录下的具有applicationContext的前缀文件.Classpath后有 ...
- windows下protobuf jar包的编译
0.如果你不想手动编译生成,请直接跳到最后下载附件. 1.下载protobuf release版本:https://github.com/google/protobuf/releases,protoc ...
- [Git] Github上传新repository后自动合并
原因是新repository中有个与老repository一模一样的名字为".git"的隐藏文件夹,删去后即可: 将整个工程直接复制粘贴出此错误...好蠢: Github控制项目的 ...
- win2008 ent r2 开启端口
你好.win2008r2 ent 可以用以下命令行来实现,当然也可以用防火墙来配置 比如打开8080端口方法如下: netsh firewall add portopening TCP 8080 My ...
- jQuery选择器总结 转
jQuery选择器总结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- JVM内存管理和JVM垃圾回收机制
JVM内存管理和JVM垃圾回收机制(1) 这里向大家描述一下JVM学习笔记之JVM内存管理和JVM垃圾回收的概念,JVM内存结构由堆.栈.本地方法栈.方法区等部分组成,另外JVM分别对新生代和旧生代采 ...
- linux —— shell 编程(文本处理)
导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 基本文本处理 流编辑器sed aw ...
- [置顶] UITableViewCell
UITableViewCellStyle: 四种Cell类型. UITableViewCellSeparatorStyle 分割线类型.(group三种,plain两种) UITableViewCel ...
- LINUX 内核调试基础+编程基础
http://blog.chinaunix.net/uid-20564848-id-73208.html 内核文档:[root@localhost Documentation]# pwd /usr/s ...
- 11月15日jquery学习笔记
1.属性 jQuery对象是类数组,拥有length属性和介于0~length-1之间的数值属性,可以用toArray()方法将jQuery对象转化为真实数组. selector属性是创建jQuery ...