HDU 5293 Tree chain problem (树链剖分+LCA+树形DP)

#include
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define debug cout<<"YES\n";
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair
#define piii pair
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
const int mod=201314;
const int maxn=1e5+100;
const int lz=0;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);}

int n,m;
///链向星
struct e{
    int u,nxt;
}edge[maxn<<1];
int head[maxn],cnt;
void addedge(int x,int y){
    edge[cnt]=e{y,head[x]};
    head[x]=cnt++;
}
///树链剖分
int dep[maxn],fa[maxn][20],son[maxn],sz[maxn];
void dfs1(int u,int f){
    dep[u]=dep[f]+1,sz[u]=1;
    fa[u][0]=f;rep(i,1,20) fa[u][i]=fa[fa[u][i-1]][i-1];
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].u;
        if(v!=f){
            dfs1(v,u);
            sz[u]+=sz[v];
            if(sz[v]>sz[son[u]]) son[u]=v;
        }
    }
}
int top[maxn],dfn[maxn],tot=0;
void dfs2(int u,int f){
    top[u]=f,dfn[u]=++tot;
    if(son[u]) dfs2(son[u],f);
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].u;
        if(v!=fa[u][0]&&v!=son[u])
            dfs2(v,v);
    }
}
///LCA
int LCA(int x,int y){
    if(dep[x]=0;i--) if(dep[x]>=dep[y]+(1<=0;i--) if(fa[x][i]!=fa[y][i])
        x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
///线段树
    int bit[maxn];
    void add(int x,int v){
        for(;x<=n;bit[x]+=v,x+=x&-x);
    }
    int sum(int x){
        int ret=0;
        for(;x>0;ret+=bit[x],x-=x&-x);
        return ret;
    }
    int qy(int l,int r){
        return sum(r)-sum(l-1);
    }
int ans[maxn];///统计答案
int tx[maxn],ty[maxn],tz[maxn];
vector q[maxn];
int Q(int x,int y){
    int ret=0;
    while(top[x]^top[y]){
        if(dep[top[x]]dep[y]) swap(x,y);
    ret+=qy(dfn[x],dfn[y]);
    return ret;
}
void solve(int u,int f){
    int tmp=0;
    for(int i=head[u];~i;i=edge[i].nxt){
        int v=edge[i].u;
        if(v!=f){
            solve(v,u);
            tmp+=ans[v];
            ans[u]+=ans[v];
        }
    }
    rep(i,0,q[u].size()){
        int x=tx[q[u][i]],y=ty[q[u][i]];
        ans[u]=max(ans[u],tmp+Q(x,y)+tz[q[u][i]]);
    }
    add(dfn[u],tmp-ans[u]);
}
int main(){
    /*
    freopen("d://in.txt","r",stdin);
    freopen("d://out.txt","w",stdout);*/
    int t;sc(t);
    while(t--){
        sc(n),sc(m);
        ///初始化
        mst(head,-1),tot=cnt=0;
        rep(i,1,n){
            int x,y;
            scc(x,y);
            addedge(x,y);
            addedge(y,x);
        }
        rep(i,1,n+1){
            q[i].clear();
            ans[i]=son[i]=0;
        }
        dfs1(1,0),dfs2(1,1);
        rep(i,0,m){
            sccc(tx[i],ty[i],tz[i]);
            int lca=LCA(tx[i],ty[i]);
            ///cout<

 

你可能感兴趣的:(HDU习题集,LCA/树链剖分,动态规划之树形DP)