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).给出值 ...
随机推荐
- 【转】OpenGL基础图形编程(二)
原文:http://blog.chinaunix.net/uid-20638550-id-1909184.html 分类: 十一.位图与图像 11.1.位图 11.1.1 位图(Bitmap)与字符 ...
- 《JavaScript核心概念》基础部分重点摘录
注:<JavaScript核心概念>适合深入了解JavaScript,比我买的<JavaScript框架设计>语言和内容丰富多了(可能是我水平尚浅吧). 1. 作用域 var ...
- 通过mysql写入一句话木马
USE mysql;# MySQL 返回的查询结果为空(即零行). # MySQL 返回的查询结果为空(即零行). CREATE TABLE a( cmd1 text NOT NULL );# MyS ...
- UVa 10029 hash + dp
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- lightoj 1018 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...
- Google视频搜索
本博文的主要内容有 .Google视频搜索的介绍 .Google视频搜索之一:普通搜索 .Google视频搜索之二:高级搜索 1.Google视频搜索的介绍 https://zh.wiki ...
- 【长篇高能】ReactiveCocoa 和 MVVM 入门
翻译自ReactiveCocoa and MVVM, an Introduction. 文中引用的 Gist 可能无法显示.为了和谐社会, 请科学上网. MVC 任何一个正经开发过一阵子软件的人都熟悉 ...
- winform 跨窗体给控件传值 分类: WinForm 2014-08-02 16:33 195人阅读 评论(0) 收藏
两个窗体 FormA,FormB; FormA窗体中有一文本框控件:TextBox; FormB窗体中有一变量:txtJSJ 目的:把变量赋值给文本框 实现: 设置TextBox属性: Modifie ...
- 【Android - 框架】之ORMLite的使用
Android中有很多操作SQLite数据库的框架,现在最常见.最常用的是ORMLite和GreenDAO.ORMLite相比与GreenDAO来说是一个轻量级的框架,而且学习成本相对较低.所以这个帖 ...
- 关于win7右下角显示“音频服务未运行”的解决方法
今天打开电脑发现右下角的的小喇叭多了个叉叉,显示“音频服务未运行”,百度了一下,解决方法还是挺多的,一下是百度到的解决方法,希望可以帮到出现这个问题的朋友们. 解决方法:(转载的) 1.Windows ...