题目链接
QTREE - Query on a tree
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
又重新温习了下树链剖分。。。
知识点博客:传送门
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=10000+100;
int head[maxn];
struct edge
{
int from,to,next;
}e[maxn*2]; //
struct node
{
int l,r,mx;
}seg[maxn*4];
struct Edge
{
int u,v,w;
}edge[maxn];
int tol=0;
void add(int u,int v)
{
e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
}
int pos;//线段树总区间大小
int fa[maxn],top[maxn],son[maxn],num[maxn],p[maxn],dep[maxn];
//top[v]表示v所在的重链的顶端节点,fa[v]表示v的父亲节点,num[v]表示以v为根的子树的节点数,p[v]表示v与其父亲节点的连边在线段树中的位置,son[v]为v的重儿子,dep为深度
void build(int i,int l,int r)
{
seg[i].l=l,seg[i].r=r,seg[i].mx=0;
if(l==r) return;
int m=(l+r)/2;
build(i*2,l,m),build(i*2+1,m+1,r);
}
void dfs(int u,int f,int d)
{
num[u]=1;
fa[u]=f;
dep[u]=d;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v==f) continue;
dfs(v,u,d+1);
if(son[u]==0||num[v]>num[son[u]])
son[u]=v;
num[u]+=num[v];
}
}
void dfs2(int u,int sp)
{
top[u]=sp;
if(son[u])
{
p[u]=pos++;
dfs2(son[u],sp);
}
else
{
p[u]=pos++;
return;
}
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v!=son[u]&&v!=fa[u])
dfs2(v,v);
}
}
void update(int i,int pos,int v)
{
if(seg[i].l==seg[i].r)
{
seg[i].mx=v;
return;
}
int m=(seg[i].l+seg[i].r)/2;
if(pos<=m) update(i*2,pos,v);
else update(i*2+1,pos,v);
seg[i].mx=max(seg[i*2].mx,seg[i*2+1].mx);
}
int query(int i,int l,int r)
{
if(seg[i].l==l&&seg[i].r==r)
{
return seg[i].mx;
}
int m=(seg[i].l+seg[i].r)/2;
if(r<=m) return query(i*2,l,r);
else if(l>m) return query(i*2+1,l,r);
else return max(query(i*2,l,m),query(i*2+1,m+1,r));
}
int find(int u,int v)
{
int f1=top[u],f2=top[v];
int ans=0;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(f1,f2);
swap(u,v);
}
ans=max(ans,query(1,p[f1],p[u]));
u=fa[f1],f1=top[u];
}
if(u==v) return ans;
if(dep[u]>dep[v]) swap(u,v);
return max(ans,query(1,p[son[u]],p[v]));
}
void init()
{
tol=0;pos=1;
memset(head,0,sizeof(head));
memset(fa,0,sizeof(fa));
memset(son,0,sizeof(son));
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
init();
int n;
scanf("%d",&n);
rep(i,1,n)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
add(edge[i].u,edge[i].v);
add(edge[i].v,edge[i].u);
}
dfs(1,0,0);
dfs2(1,1);
build(1,1,pos);
rep(i,1,n)
{
if(dep[edge[i].u]>dep[edge[i].v])
swap(edge[i].u,edge[i].v);
update(1,p[edge[i].v],edge[i].w);
}
char op[10];
while(~scanf("%s",op))
{
if(op[0]=='D') break;
int u,v;
scanf("%d%d",&u,&v);
if(op[0]=='Q')
printf("%d\n",find(u,v));
else
{
update(1,p[edge[u].v],v);
}
}
}
return 0;
}