Query on a tree
5000ms
262144KB
This problem will be judged on SPOJ. Original ID:
QTREE
64-bit integer IO format: %lld Java class name: Main
64-bit integer IO format: %lld Java class name: Main
Font Size:
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
Tags ( Click to see )
代码:
#include <bits/stdc++.h>
#define lc(o) (o<<1)
#define rc(o) (o<<1|1)
using namespace std;
const int maxn = 1e4 + 10;
struct Edge{
int from,to,dist;
Edge(int u,int v,int w):from(u),to(v),dist(w){}
};
vector<int> g[maxn];
vector<Edge> edges;
void AddEdge(int u,int v,int w)
{
int m = edges.size();
g[u].push_back(m);
g[v].push_back(m);
edges.push_back(Edge(u,v,w));
}
int siz[maxn],son[maxn],fa[maxn],dep[maxn];
void pre(int u,int p)
{
dep[u] = dep[p] + 1;
siz[u] = 1;
fa[u] = p;
son[u] = 0;
for(vector<int>::iterator it = g[u].begin(); it != g[u].end(); ++it) {
Edge & e = edges[*it];
int v = e.to;
if(v == u) v = e.from;
if(v==p)continue;
pre(v,u);
siz[u] += siz[v];
if(siz[son[u]] < siz[v]) son[u] = v;
}
}
int root[maxn],pos[maxn],seg[maxn<<2],tot;
void built(int u,int t)
{
root[u] = t;
pos[u] = ++tot;
if(siz[u] == 1) return;
built(son[u],t);
for(vector<int>::iterator it = g[u].begin(); it != g[u].end(); ++it) {
Edge & e = edges[*it];
int v = e.to;
if(v == u) v = e.from;
if(v==fa[u]||v==son[u])continue;
built(v,v);
}
}
int ql,qr,val;
void Modify(int o,int L,int R)
{
if(L==R) {
seg[o] = val;
return;
}
int M = (L+R)>>1;
if(ql<=M) Modify(lc(o),L,M);
else Modify(rc(o),M+1,R);
seg[o] = max(seg[lc(o)],seg[rc(o)]);
}
int Query(int o,int L,int R)
{
if(ql<=L&&qr>=R) return seg[o];
int M = (L+R)>>1;
int ans = 0;
if(ql <= M) ans = Query(lc(o),L,M);
if(qr > M) ans = max(ans,Query(rc(o),M+1,R));
return ans;
}
int ask(int u,int v)
{
int ans = 0;
while(root[u] != root[v]) {
if(dep[root[u]] < dep[root[v]]) swap(u,v);
ql = pos[root[u]];
qr = pos[u];
if(ql <= qr) ans = max(ans,Query(1,1,tot));
u = fa[root[u]];
}
if(u==v)return ans;
ql = pos[u];
qr = pos[v];
if(ql > qr) swap(ql,qr);
ans = max(ans,Query(1,1,tot));
return ans;
}
int main()
{
int T;scanf("%d",&T);
siz[0] = 0;
while(T--) {
int n;scanf("%d",&n);
edges.clear();
edges.push_back(Edge(0,0,0));
for(int i = 1; i <= n; ++i) g[i].clear();
for(int i = 1; i < n; ++i) {
int u,v,w;scanf("%d%d%d",&u,&v,&w);
AddEdge(u,v,w);
}
pre(1,0);
tot = 0;
built(1,1);
memset(seg,0,sizeof(*seg)*(4*n));
for(int i = 1; i < n; ++i) {
Edge & e = edges[i-1];
if(dep[e.from] > dep[e.to]) swap(e.from,e.to);
val = e.dist;
ql = e.to;
Modify(1,1,tot);
}
char op[10];
int u,v;
while(scanf("%s",op) == 1)
{
if(op[0] == 'D')break;
scanf("%d%d",&u,&v);
if(op[0] == 'Q')
printf("%d\n",ask(u,v));
else {
ql = pos[edges[u-1].to];
val = v;
Modify(1,1,tot);
}
}
}
return 0;
}