375. Query on a treeProblem code: QTREE |
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.
We will ask you to perfrom some instructions of the following form:
- CHANGE i ti : change the cost of the i-th edge to ti
or - QUERY a b : ask for the maximum edge cost on the path from node a to node b
Input
The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.
For each test case:
- In the first line there is an integer N (N <= 10000),
- In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
- The next lines contain instructions "CHANGE i ti" or "QUERY a b",
- The end of each test case is signified by the string "DONE".
There is one blank line between successive tests.
Output
For each "QUERY" operation, write one integer representing its result.
Example
Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3
解题报告: 漆子超论文第三题。树链剖分入门题吧。将一条链的询问转化成log n个连续的线段树询问,询问的总复杂度即为 log n * log n。修改的复杂度为log n。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
#include <cassert>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define travel(e, u) for(int e = u, v = vv[u]; e; e = nxt[e], v = vv[e])
#define bit(n) (1LL<<(n))
#define clr(a, b) memset((a), b, sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM
work();
return 0;
}
void nextInt(int & x)
{
char ch;
while(ch = getchar(), isdigit(ch) == false);
x = 0;
while(x = 10 * x + ch - '0', ch = getchar(), isdigit(ch) == true);
}
/***************************************************************************************/
int n;
int ans;
const int maxv = 11111;
int edge[maxv], ecnt;
int nxt[maxv * 2], vv[maxv * 2], ww[maxv * 2], eid[maxv * 2];
int siz[maxv], dad[maxv], son[maxv], top[maxv], dep[maxv];
int pos[maxv], idx, node[maxv], cost[maxv];
#define rt 1,10000,1
#define lson l, m, pos<<1
#define rson m+1, r, pos<<1|1
#define ls pos<<1
#define rs pos<<1|1
#define mid ((l+r)/2)
int real[maxv];
int ma[maxv<<2];
void build(int l, int r, int pos)
{
if(l==r)
{
real[l] = pos;
return;
}
int m = mid;
build(lson), build(rson);
}
void update(int v, int pos)
{
ma[pos] = v;
while(pos > 1)
{
pos >>= 1;
ma[pos] = max(ma[ls], ma[rs]);
}
}
int qry(int L, int R, int l, int r, int pos)
{
if(L<=l && r<=R)
return ma[pos];
int m = mid;
int ret = 0;
if(L <= m) ret = max(ret, qry(L, R, lson));
if(m < R) ret = max(ret, qry(L, R, rson));
return ret;
}
void init()
{
ecnt = 2;
clr(edge, 0);
/// Segment Tree
clr(ma, 0);
}
void addEdge(int u, int v, int w, int id, int first[])
{
nxt[ecnt] = first[u], vv[ecnt] = v, ww[ecnt] = w, eid[ecnt] = id, first[u] = ecnt++;
}
void dfsSize(int u)
{
siz[u] = 1;
son[u] = 0;
travel(e, edge[u]) if(v != dad[u])
{
node[eid[e]] = v;
cost[v] = ww[e];
dep[v] = dep[u] + 1;
dad[v] = u;
dfsSize(v), siz[u] += siz[v];
if(siz[v] > siz[son[u]]) son[u] = v;
}
}
int tt;
void height(int u)
{
top[u] = tt;
pos[u] = ++idx;
update(cost[u], real[idx]);
if(son[u]) height(son[u]);
travel(e, edge[u]) if(v != dad[u] && v != son[u])
tt = v, height(v);
}
void calc(int u, int v)
{
while(top[u] != top[v])
{
if(dep[top[u]] > dep[top[v]]) swap(u, v);
ans = max(ans, qry(pos[top[v]], pos[v], rt));
v = dad[top[v]];
}
if(dep[u] > dep[v]) swap(u, v);
if(u != v) ans = max(ans, qry(pos[u]+1, pos[v], rt));
}
void work()
{
build(rt);
int T;
scanf("%d", &T);
fff(cas, 1, T)
{
init();
scanf("%d", &n);
fff(i, 1, n-1)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w, i, edge);
addEdge(v, u, w, i, edge);
}
dfsSize(1);
tt = 1, idx = 0, height(1);
char op[10];
while(scanf("%s", op) == 1)
{
if(op[0] == 'D') break;
if(op[0] == 'Q')
{
int u, v;
scanf("%d%d", &u, &v);
ans = 0;
calc(u, v);
printf("%d\n", ans);
}
else
{
int p, v;
scanf("%d%d", &p, &v);
update(v, real[pos[node[p]]]);
}
}
}
}

本文深入探讨了树链剖分技术及其与线段树的结合应用,通过实例展示了如何利用这些算法高效解决特定类型的树形问题。详细介绍了算法原理、实现细节以及复杂度分析,旨在提升读者在处理复杂树形结构问题时的技能。
1009

被折叠的 条评论
为什么被折叠?



