码迷,mamicode.com
首页 > 其他好文 > 详细

SPOJ 375

时间:2017-12-31 10:58:03      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:div   set   bubuko   mes   define   ring   post   logs   you   

默默一看提交时间 ……
这是我以前的代码风格……
技术分享图片

#include <cstdio>  
#include <cstring>  
#include <vector>  
#include <algorithm>  
#define N 10005
#define lson(x) ((x<<1))  
#define rson(x) ((x<<1)+1) 
#define Del(a,b) memset(a,b,sizeof(a))
using namespace std;  

int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; 
int num;  
vector<int> v[N];
  
struct tree {  
    int x,y,val;  
    void read()
    {  
        scanf("%d%d%d",&x,&y,&val);  
    }  
}e[N];
  
void dfs1(int u, int f, int d) 
{  
    dep[u] = d;  
    siz[u] = 1;  
    son[u] = 0;  
    fa[u] = f;  
    for (int i = 0; i < v[u].size(); i++) 
    {  
        int ff = v[u][i];  
        if (ff == f) continue;  
        dfs1(ff, u, d + 1);  
        siz[u] += siz[ff];  
        if (siz[son[u]] < siz[ff])  
            son[u] = ff;  
    }  
}  
void dfs2(int u, int tp) {  
    top[u] = tp;  
    id[u] = ++num;  
    if (son[u]) dfs2(son[u], tp);  
    for (int i = 0; i < v[u].size(); i++) {  
        int ff = v[u][i];  
        if (ff == fa[u] || ff == son[u]) continue;  
        dfs2(ff, ff);  
    }  
}  
 
struct Tree  
{  
    int l,r,val;  
};  
Tree tree[4*N];  

void pushup(int x) 
{  
    tree[x].val = max(tree[lson(x)].val, tree[rson(x)].val);  
}  
  
void build(int l,int r,int v)  
{  
    tree[v].l=l;  
    tree[v].r=r;  
    if(l==r)  
    {  
        tree[v].val=val[l];  
        return ;  
    }  
    int mid=(l+r)>>1;  
    build(l,mid,v*2);  
    build(mid+1,r,v*2+1);  
    pushup(v);  
}  

void update(int o,int v,int val)  
{  
    if(tree[o].l==tree[o].r)  
    {  
        tree[o].val = val;  
        return ;  
    }  
    int mid = (tree[o].l+tree[o].r)/2;  
    if(v<=mid)  
        update(o*2,v,val);  
    else  
        update(o*2+1,v,val);  
    pushup(o);  
}
  
int query(int x,int l, int r)  
{  
    if (tree[x].l >= l && tree[x].r <= r) 
    {  
        return tree[x].val;  
    }  
    int mid = (tree[x].l + tree[x].r) / 2;  
    int ans = 0;  
    if (l <= mid) ans = max(ans, query(lson(x),l,r));  
    if (r > mid) ans = max(ans, query(rson(x),l,r));  
    return ans;  
}  
  
int Yougth(int u, int v) 
{  
    int tp1=top[u],tp2=top[v];  
    int ans=0;  
    while (tp1!=tp2) 
    {  
        if (dep[tp1] < dep[tp2]) 
        {  
            swap(tp1, tp2);  
            swap(u,v);  
        }  
        ans=max(query(1,id[tp1],id[u]),ans);  
        u=fa[tp1];  
        tp1=top[u];  
    }  
    if(u==v)return ans;  
    if(dep[u]>dep[v])swap(u, v);  
    ans=max(query(1,id[son[u]],id[v]),ans);  
    return ans;  
}
  
void Clear(int n)  
{  
    for(int i=1;i<=n;i++)  
        v[i].clear();  
}  

int main()  
{    
    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        int n;  
        scanf("%d",&n); 
        for(int i=1;i<n;i++)  
        {  
            e[i].read();
            v[e[i].x].push_back(e[i].y);
            v[e[i].y].push_back(e[i].x);  
        }  
        num = 0;  
        dfs1(1,0,1);  
        dfs2(1,1);  
        for (int i = 1; i < n; i++) 
        {  
            if (dep[e[i].x] < dep[e[i].y]) swap(e[i].x, e[i].y);  
            val[id[e[i].x]]=e[i].val;  
        }  
        build(1,num,1);//构造线段树 
        char s[200];  
        while(~scanf("%s",&s)&&s[0]!='D')  
        {  
            int x,y;  
            scanf("%d%d",&x,&y);  
            if(s[0]=='Q')  
                printf("%d\n",Yougth(x,y));
            if (s[0] == 'C')  
                update(1,id[e[x].x],y); 
        }  
        Clear(n);
    }  
    return 0;  
}

SPOJ 375

标签:div   set   bubuko   mes   define   ring   post   logs   you   

原文地址:https://www.cnblogs.com/qdscwyy/p/8156722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!