BZOJ 3631 [JLOI2014]松鼠的新家

题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=3631

题意:略。

思路:树上差分裸题,要注意的就是a[2]a[n]最后糖果数要减一,[2,n-1]是因为打标记的时候多打了一次,a[n]减一是由题意得的。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define ALL(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
template <class T1, class T2>inline void gmax(T1 &a,T2 b){if (b>a) a=b;}
template <class T1, class T2>inline void gmin(T1 &a,T2 b){if (b<a) a=b;}
void up(int&x,int y){x+=y;if(x>=P)x-=P;}
const int N=300000+10;
int n,a[N],u,v,i,bel[N],dep[N],fa[N],sz[N],son[N],sum[N];
vector<int>G[N];
void dfs(int u,int f){
    dep[u]=f?dep[f]+1:0;
    fa[u]=f;
    sz[u]=1;
    son[u]=-1;
    for (int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if (v==f) continue;
        dfs(v,u);
        sz[u]+=sz[v];
        if (son[u]==-1||sz[v]>sz[son[u]]) son[u]=v;
    }
}
void dfs2(int u,int f){
    bel[u]=f;
    if (son[u]==-1) return;
    dfs2(son[u],f);
    for (int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if (v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}
int lca(int x,int y){
    for (;bel[x]!=bel[y];dep[bel[x]]>dep[bel[y]]?x=fa[bel[x]]:y=fa[bel[y]]);
    return dep[x]<dep[y]?x:y;
}
void dfs3(int u,int f){
    for (int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if (v==f) continue;
        dfs3(v,u);
        sum[u]+=sum[v];
    }
}
int main(){
    read(n);
    for (i=1;i<=n;i++) read(a[i]);
    for (i=1;i<n;i++){
        read(u),read(v);
        G[u].pb(v);
        G[v].pb(u);
    }
    dfs(1,0);
    dfs2(1,1);
    for (i=1;i<n;i++){
        int p=lca(a[i],a[i+1]);
        sum[a[i]]++,sum[a[i+1]]++,sum[p]--,sum[fa[p]]--;
    }
    dfs3(1,0);
    for (i=2;i<=n;i++) sum[a[i]]--;
    for (i=1;i<=n;i++) printf("%d\n",sum[i]);
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注