spoj 375. Query on a tree 【树链剖分--插点问线 】

题目:spoj 375. Query on a tree

题意:题意很清晰,就是给你一颗树,每两点之间有权值,然后改变一些权值,问一条路径上的最大值。

分析:入门题目,直接套树链模板

AC代码;

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int N = 10010;
#define Del(a,b) memset(a,b,sizeof(a))
struct Node
{
    int b,next;
};
Node e[N*2];
int tree[N];
int zzz,n,z,edge,root,a,b,c;
int d[N][3];
int first[N],dep[N],w[N];  //dep 深度,w线段树中位置
int fa[N],top[N],son[N],siz[N];
//fa表示父亲 top:所在重链的顶端节点 son:与v在同一重链上v的儿子节点
void add_Node(int a,int b,int c)
{
    e[++edge].b = b;
    e[edge].next = first[a];
    first[a] = edge;
}
void dfs(int x)
{
    siz[x] = 1;son[x] = 0;
    for(int i=first[x];i>0;i = e[i].next)
        if(e[i].b!=fa[x])
        {
            fa[e[i].b] = x;
            dep[e[i].b] = dep[x] + 1;
            dfs(e[i].b);
            if(siz[e[i].b]>siz[son[x]])//保存重儿子
                son[x] = e[i].b;
            siz[x] += siz[e[i].b];
        }
}
void build_tree(int v,int tp) //节点 重链顶端节点
{
    w[v] = ++z;top[v] = tp;
    if(son[v]!=0)
        build_tree(son[v],top[v]);
    for(int i = first[v];i>0;i = e[i].next)
    {
        if(e[i].b!=son[v] && e[i].b!=fa[v])
            build_tree(e[i].b,e[i].b);
    }
}
//1,1,z,w[d[i][1]],d[i][2]
void update(int root,int l,int r,int loc,int x)
{
    if(loc>r || l>loc)
        return ;
    if(l == r)
    {
        tree[root] = x;
        return ;
    }
    int mid = (l+r)/2,ls = root * 2,rs = ls+1;
    update(ls,l,mid,loc,x);
    update(rs,mid+1,r,loc,x);
    tree[root] = max(tree[ls] ,tree[rs]);
}
int maxi(int root, int lo, int hi, int l, int r)
{
     if (l > hi || r < lo) return 0;
     if (l <= lo && hi <= r) return tree[root];
     int mid = (lo + hi) / 2, ls = root * 2, rs = ls + 1;
     return max(maxi(ls, lo, mid, l, r), maxi(rs, mid+1, hi, l, r));
}

inline int find(int va, int vb)
{
     int f1 = top[va], f2 = top[vb], tmp = 0;
     while (f1 != f2)
     {
         //printf("va:%d vb:%d f1:%d f2:%d \n",va,vb,f1,f2);
           if (dep[f1] < dep[f2])
           { swap(f1, f2); swap(va, vb); }
           tmp = max(tmp, maxi(1, 1, z, w[f1], w[va]));
           //printf("%d\n",tmp);
           va = fa[f1]; f1 = top[va];
     }
     if (va == vb) return tmp;
     if (dep[va] > dep[vb]) swap(va, vb);
     return max(tmp, maxi(1, 1, z, w[son[va]], w[vb]));  //
}
void init()
{
     scanf("%d", &n);
     root = (n + 1) / 2;
     fa[root] = z = dep[root] = edge = 0;
     memset(siz, 0, sizeof(siz));
     memset(first, 0, sizeof(first));
     memset(tree, 0, sizeof(tree));
     for (int i = 1; i < n; i++)
     {
         scanf("%d%d%d", &a, &b, &c);
         d[i][0] = a; d[i][1] = b; d[i][2] = c;
         add_Node(a, b, c);
         add_Node(b, a, c);
     }
     dfs(root);
     build_tree(root, root);    //
     for (int i = 1; i < n; i++)
     {
         if (dep[d[i][0]] > dep[d[i][1]]) swap(d[i][0], d[i][1]);
         update(1, 1, z, w[d[i][1]], d[i][2]);
     }
}
char ch[100];
void work()
{
     while(~scanf("%s",ch) && ch[0]!='D')
     {
         scanf("%d%d", &a, &b);
         if (ch[0] == 'Q')
            printf("%d\n", find(a, b));
        else
            update(1, 1, z, w[d[a][1]], b);
     }
}

int main()
{
    freopen("Input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        work();
    }
    return 0;
}
时间: 2024-08-29 03:05:19

spoj 375. Query on a tree 【树链剖分--插点问线 】的相关文章

spoj 375 Query on a tree (树链剖分)

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 Q

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

SPOJ QTREE Query on a tree ——树链剖分 线段树

[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20005 int T,n,fr[maxn],h[maxn],to[maxn],ne[maxn]

SPOJ QTREE Query on a tree --树链剖分

题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. 询问的时候,在从u找到v的过程中顺便查询到此为止的最大值即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath&

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags 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

spoj 375 QTREE - Query on a tree 树链剖分

题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set&g

spoj Query on a tree(树链剖分模板题)

375. Query on a tree Problem 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

SPOJ 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 tior QUERY a b : ask fo

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 f