poj 3237 Tree(最近公共祖先)

Tree
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 2802 Accepted: 762

Description

You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i vChange the weight of the ith edge to v
NEGATE a bNegate the weight of every edge on the path from a to b
QUERY a bFind the maximum weight of edges on the path from a to b

Input

The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.

Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N − 1 lines each contains three integers ab and c, describing an edge connecting nodes a and b with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE” ends the test case.

Output

For each “QUERY” instruction, output the result on a separate line.

Sample Input

1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Sample Output

1
3

Source


题意:对一棵树做3个操作
题解:爬坡算法...求最近公共祖先.. 水过...罪过...

#include<stdio.h>
#include<string.h>
int head[10005],fath[10005];
int wei[10005],deep[10005];
int id[10005],vis[10005],all;
struct edge
{
    int data,next,len,id;
} p[20005];
int MAX(int a,int b){ return a>b?a:b; }
void add(int x,int y,int len,int id)
{
    p[all].next=head[x];
    p[all].data=y;
    p[all].len=len;
    p[all].id=id;
    head[x]=all++;
}
void dfs(int x,int d)
{
    int i;

    deep[x]=d;  vis[x]=1;
    for(i=head[x]; i!=-1; i=p[i].next)
    {
        if(vis[p[i].data]) continue;
        wei[p[i].data]=p[i].len;
        fath[p[i].data]=x;
        id[p[i].id]=p[i].data;
        dfs(p[i].data,d+1);
    }
}
void Q(int x,int y)
{
    int res=-0xffffff;

    if(deep[x]<deep[y])
    {
        x=x+y;
        y=x-y;
        x=x-y;
    }
    while(deep[x]>deep[y])
    {
        res=MAX(wei[x],res);
        x=fath[x];
    }
    while(x!=y)
    {
        res=MAX(MAX(res,wei[x]),wei[y]);
        x=fath[x],y=fath[y];
    }
    printf("%d\n",res);
}
void N(int x,int y)
{
    if(deep[x]<deep[y])
    {
        x=x+y;
        y=x-y;
        x=x-y;
    }
    while(deep[x]>deep[y])
    {
        wei[x]=-wei[x];
        x=fath[x];
    }
    while(x!=y)
    {
        wei[x]=-wei[x],wei[y]=-wei[y];
        x=fath[x],y=fath[y];
    }
}
int main()
{
    int t,n,i,x,y,z;
    char s[10];

    //freopen("t.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(head,-1,sizeof(head));
        for(all=i=0; i<n-1; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z,i+1),add(y,x,z,i+1);
        }
        memset(vis,0,sizeof(vis));
        dfs(1,0);
        while(scanf("%s",s),s[0]!='D')
        {
            scanf("%d%d",&x,&y);
            if(s[0]=='C') wei[id[x]]=y;
            else if(s[0]=='Q') Q(x,y);
            else N(x,y);
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值