hdu 3804 树链剖分+线段树

来源:互联网 发布:安有说人主而不得者乎 编辑:程序博客网 时间:2024/05/16 07:00

Query on a tree

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 618    Accepted Submission(s): 144


Problem Description
  There are some queries on a tree which has n nodes. Every query is described as two integers (X, Y).For each query, you should find the maximum weight of the edges in set E, which satisfies the following two conditions.
1) The edge must on the path from node X to node 1.
2) The edge’s weight should be lower or equal to Y.
  Now give you the tree and queries. Can you find out the answer for each query?
 

Input
  The first line of the input is an integer T, indicating the number of test cases. For each case, the first line contains an integer n indicates the number of nodes in the tree. Then n-1 lines follows, each line contains three integers X, Y, W indicate an edge between node X and node Y whose value is W. Then one line has one integer Q indicates the number of queries. In the next Q lines, each line contains two integers X and Y as said above.
 

Output
  For each test case, you should output Q lines. If no edge satisfy the conditions described above,just output “-1” for this query. Otherwise output the answer for this query.
 

Sample Input
131 2 72 3 543 103 73 63 4
 

Sample Output
775-1
Hint
2<=n<=10^52<=Q<=10^51<=W,Y<=10^9The data is guaranteed that your program will overflow if you use recursion.
 

Author
Edward_mj
 

Source
ACM-DIY Group Contest 2011 Spring

//题意:给出一棵树,Q个询问x,w,x到1的路径上不超过w的最长边是多少。//思路:先按询问的权值从小到大排序,然后n-1条边也按权值从小到大排序,对于每个询问,//把小于等于当前权值的边加到树上,然后求当前树的最大值。。。#pragma comment(linker, "/STACK:10240000000000,10240000000000")#include<stdio.h>#include<algorithm>#include<string.h>#define N 200005#define inf 0x3fffffffusing namespace std;struct line{    int x,y,w,id,sum;}ed[N],b[N];struct node{    int u,v,w,next;}bian[N*2];struct pp{    int x,y,ma;}a[N*3];int sz[N],head[N],top[N],son[N],id,e,ti[N],father[N],dep[N];void add(int u,int v,int w){    bian[e].u=u;    bian[e].v=v;    bian[e].next=head[u];    head[u]=e++;}int max(int a,int b){    return a>b?a:b;}void dfs1(int u,int fa){    int i,v;    sz[u]=1; dep[u]=dep[fa]+1; father[u]=fa; son[u]=0;    for(i=head[u];i!=-1;i=bian[i].next)    {        v=bian[i].v;        if(v==fa)  continue;        dfs1(v,u);        sz[u]+=sz[v];        if(sz[son[u]]<sz[v])            son[u]=v;    }}void dfs2(int u,int fa){    int i,v;    top[u]=fa;    ti[u]=id++;    if(son[u]!=0)        dfs2(son[u],fa);    for(i=head[u];i!=-1;i=bian[i].next)    {        v=bian[i].v;        if(v==son[u]||v==father[u])            continue;        dfs2(v,v);    }}void build(int t,int x,int y){    a[t].x=x; a[t].y=y; a[t].ma=-inf;    if(x==y)  return;    int mid=(x+y)>>1,temp=t<<1;    build(temp,x,mid);    build(temp+1,mid+1,y);}void update(int t,int x,int w){    if(a[t].x==a[t].y)    {        a[t].ma=w;        return;    }    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;    if(x<=mid)        update(temp,x,w);    else        update(temp+1,x,w);    a[t].ma=max(a[temp].ma,a[temp+1].ma);}int query(int t,int x,int y){    if(a[t].x==x&&a[t].y==y)        return a[t].ma;    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;    if(y<=mid)        return query(temp,x,y);    else if(x>mid)        return query(temp+1,x,y);    else        return max(query(temp,x,mid),query(temp+1,mid+1,y));}int lca(int x){    int y=1,ans=-1;    while(top[x]!=top[y])    {        if(dep[top[x]]<dep[top[y]])            swap(x,y);        ans=max(ans,query(1,ti[top[x]],ti[x]));        x=father[top[x]];    }    if(dep[x]>dep[y])        swap(x,y);    if(x!=y)        ans=max(ans,query(1,ti[x]+1,ti[y]));    return ans;}bool cmp1(line a,line b){    return a.w<b.w;}bool cmp2(line a,line b){    return a.y<b.y;}bool cmp3(line a,line b){    return a.id<b.id;}int main(){    int t,i,q,j,n,ans;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        e=0;        memset(head,-1,sizeof(head));        for(i=0;i<n-1;i++)        {            scanf("%d%d%d",&ed[i].x,&ed[i].y,&ed[i].w);            add(ed[i].x,ed[i].y,ed[i].w);            add(ed[i].y,ed[i].x,ed[i].w);        }        sort(ed,ed+n-1,cmp1);        dep[1]=0; id=1; sz[0]=0;        dfs1(1,1);   dfs2(1,1);        build(1,1,n);        //for(i=1;i<=n;i++)          // printf("i=%d size=%d top=%d father=%d son=%d ti=%d dep=%d\n",i,sz[i],top[i],father[i],son[i],ti[i],dep[i]);        scanf("%d",&q);        for(i=0;i<q;i++)        {            scanf("%d%d",&b[i].x,&b[i].y);            b[i].id=i;        }        sort(b,b+q,cmp2);        j=0;        for(i=0;i<q;i++)        {            while(b[i].y>=ed[j].w&&j<n-1)            {             if(dep[ed[j].x]<dep[ed[j].y])                swap(ed[j].x,ed[j].y);             update(1,ti[ed[j].x],ed[j].w);             j++;            }            ans=lca(b[i].x);            b[i].sum=ans;        }        sort(b,b+q,cmp3);        for(i=0;i<q;i++)           printf("%d\n",b[i].sum);    }    return 0;}


0 0
原创粉丝点击