当前位置:网站首页>POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
2022-07-28 04:55:00 【gongyuandaye】
The question : Given a tree containing n Spanning tree of nodes , share q operations , Divided into two :
0 c: Seek slave position s To c Distance of , then s become c
1 a b: The first a The weight of the edge becomes b
Answer key : The tree chain splits
0 The operation is an interval query on the tree .
Because the problem is edge weight and spanning tree , Tree chain dissection can only solve point weight , Then we can turn into some power . That is, for the edge <u, v>, Assign the weight of the point far from the root node as the edge weight .
So when doing tree sectioning , subtract lca Just point the right .
such 1 Operation is a single point update .
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int MAXN = 1e5 + 5;
int n, q, s, x[MAXN], y[MAXN], w[MAXN];
int sum[MAXN << 2], add[MAXN << 2];
int head[MAXN << 1], pre[MAXN], id[MAXN];
int siz[MAXN], son[MAXN], fa[MAXN];
int dep[MAXN], top[MAXN];
ll delta[MAXN];
struct edge {
int to;
int next;
}e[MAXN << 1];
int tot, sign;
/*------------ Preparation stage --------------*/
void init() {
memset(head, -1, sizeof(head));
memset(id, 0, sizeof(id));
memset(siz, 0, sizeof(siz));
memset(son, 0, sizeof(son));
memset(fa, 0, sizeof(fa));
memset(dep, 0, sizeof(dep));
memset(top, 0, sizeof(top));
memset(delta, 0, sizeof(delta));
tot = sign = 0;
}
void addedge(int u, int v) {
e[tot].to = v, e[tot].next = head[u], head[u] = tot++;
}
/*--------------dfs-----------------*/
void dfs1(int u, int f) {
//1 0
dep[u] = dep[f] + 1;
fa[u] = f;
siz[u] = 1;
for (int i = head[u]; i != -1; i = e[i].next) {
int to = e[i].to;
if (to == f) continue;
dfs1(to, u);
siz[u] += siz[to];
if (siz[to] > siz[son[u]]) son[u] = to;
}
}
void dfs2(int u, int Top) {
//1 1
id[u] = ++sign;
pre[sign] = u;
top[u] = Top;
if (son[u]) dfs2(son[u], Top);
for (int i = head[u]; i != -1; i = e[i].next) {
int to = e[i].to;
if (to == son[u] || to == fa[u]) continue;
dfs2(to, to);
}
}
/*-------------- Tree array --------------*/
int tree[MAXN];
void update(int x, int num) {
for (; x <= n; x += x & -x)
tree[x] += num;;
}
int su(int x) {
int answer = 0;
for (; x > 0; x -= x & -x)
answer += tree[x];
return answer;
}
/*------------ The tree chain splits -------------*/
int getSum(int x, int y) {
// Inquire about x To y All nodes on the path and
ll res = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
res += su(id[x]) - su(id[top[x]] - 1);
x = fa[top[x]];
}
if (id[x] > id[y]) swap(x, y);
res += su(id[y]) - su(id[x]); // subtract lca
return res;
}
int main() {
init();
scanf("%d%d%d", &n, &q, &s);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x[i], &y[i], &w[i]);
addedge(x[i], y[i]);
addedge(y[i], x[i]);
}
dfs1(1, 0);
dfs2(1, 1);
for (int i = 1; i < n; i++) {
if (fa[x[i]] == y[i]) swap(x[i], y[i]);
update(id[y[i]], w[i]);
}
int op, a, b;
while (q--) {
scanf("%d", &op);
if (op == 0) {
scanf("%d", &a);
printf("%d\n", getSum(s, a));
s = a;
}
else {
scanf("%d%d", &a, &b);
update(id[y[a]], b - w[a]);
w[a] = b;
}
}
return 0;
}
边栏推荐
- 你必需要了解的saas架构设计?
- After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
- Use and expansion of fault tolerance and fusing
- Redis配置文件详解/参数详解及淘汰策略
- [Oracle] 083 wrong question set
- 数据安全逐步落地,必须紧盯泄露源头
- HDU 1435 stable match
- Redis type
- Odoo action analysis (action.client, action.act_window, action.server)
- Look at the experience of n-year software testing summarized by people who came over the test
猜你喜欢

Automated test tool playwright (quick start)

机器人教育在STEM课程中的设计研究

What should testers know about login security?

猿辅导技术进化论:助力教与学 构想未来学校

What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?

Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance

01 node express system framework construction (express generator)
![Geely AI interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]](/img/18/27a86595eb3a7d30df359d6b2b8d8c.png)
Geely AI interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]

Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling

你必需要了解的saas架构设计?
随机推荐
【Oracle】083错题集
np. unravel_ Index() finds the index value of an element (or group of elements) of the array after being pulled into one dimension. The corresponding index value in the original dimension (or specify
启发国内学子学习少儿机器人编程教育
[Sylar] framework -chapter24- support business modularization
Cmake usage base summary
Machine learning and deep learning -- normalization processing
With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?
What should testers know about login security?
What tools do software testers need to know?
Dynamic SQL and paging
Introduction to testcafe
Wang Shuang assembly language detailed learning notes 3: registers (memory access)
Basic knowledge of network security - password (I)
Domain name (subdomain name) collection method of Web penetration
(克隆虚拟机步骤)
HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
【CPU占用高】software_reporter_tool.exe
System clock failure of database fault tolerance
Look at the experience of n-year software testing summarized by people who came over the test
[每日一氵]上古年代的 Visual Studio2015 安装