BZOJ3631: [JLOI2014]松鼠的新家

3631: [JLOI2014]松鼠的新家

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 495  Solved: 248
[Submit][Status]

Description

松 鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的。天哪,他 居然真的住在“树”上。松鼠想邀请****前来参观,并且还指定一份参观指南,他希望**能够按照他的指南顺序,先去a1,再去a2,……,最后到an, 去参观新家。
可是这样会导致**重复走很多房间,懒惰的**不听地推辞。可是松鼠告诉他,每走到一个房间,他就可以从房间拿一块糖果吃。**是个馋家伙,立马就答应了。
现在松鼠希望知道为了保证**有糖果吃,他需要在每一个房间各放至少多少个糖果。因为松鼠参观指南上的最后一个房间an是餐厅,餐厅里他准备了丰盛的大餐,所以当**在参观的最后到达餐厅时就不需要再拿糖果吃了。

Input

第一行一个整数n,表示房间个数
第二行n个整数,依次描述a1-an
接下来n-1行,每行两个整数x,y,表示标号x和y的两个房间之间有树枝相连。

Output

一共n行,第i行输出标号为i的房间至少需要放多少个糖果,才能让**有糖果吃。

Sample Input

5
1 4 5 3 2
1 2
2 4
2 3
4 5

Sample Output

1
2
1
2
1

HINT

2<= n <=300000

题解:

为什么省选会出裸树链剖分。。。

现在看起来连线段树也不用打了,类似于 雨天的尾巴 打标记就可以了233

线段树会T掉一些分?

代码:

  1 #include<cstdio>
  2 
  3 #include<cstdlib>
  4 
  5 #include<cmath>
  6 
  7 #include<cstring>
  8 
  9 #include<algorithm>
 10 
 11 #include<iostream>
 12 
 13 #include<vector>
 14 
 15 #include<map>
 16 
 17 #include<set>
 18 
 19 #include<queue>
 20 
 21 #include<string>
 22 
 23 #define inf 1000000000
 24 
 25 #define maxn 500000+5
 26 
 27 #define maxm 2500000+5
 28 
 29 #define eps 1e-10
 30 
 31 #define ll long long
 32 
 33 #define pa pair<int,int>
 34 
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36 
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38 
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40 
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42 
 43 #define mod 1000000007
 44 
 45 using namespace std;
 46 
 47 inline int read()
 48 
 49 {
 50 
 51     int x=0,f=1;char ch=getchar();
 52 
 53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 54 
 55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 56 
 57     return x*f;
 58 
 59 }
 60 int n,m,q,tot,sum,head[maxn],top[maxn],id[maxn],son[maxn],dep[maxn],s[maxn],fa[maxn],ans[maxn];
 61 int a[maxn],b[maxn][2],c[maxn];
 62 struct edge{int go,next;}e[2*maxn];
 63 inline void insert(int x,int y)
 64 {
 65     e[++tot]=(edge){y,head[x]};head[x]=tot;
 66     e[++tot]=(edge){x,head[y]};head[y]=tot;
 67 }
 68 inline void dfs(int x)
 69 {
 70     s[x]=1;
 71     for(int i=head[x],y;i;i=e[i].next)
 72         if(!dep[y=e[i].go])
 73         {
 74             dep[y]=dep[x]+1;fa[y]=x;
 75             dfs(y);
 76             s[x]+=s[y];
 77             if(s[y]>s[son[x]])son[x]=y;
 78         }
 79 }
 80 inline void dfs2(int x,int chain)
 81 {
 82     id[x]=++m;top[x]=chain;
 83     if(son[x])dfs2(son[x],chain);
 84     for(int i=head[x];i;i=e[i].next)if(e[i].go!=son[x]&&e[i].go!=fa[x])dfs2(e[i].go,e[i].go);
 85 }
 86 inline void dfs3(int x)
 87 {
 88     sum+=b[x][0];
 89     ans[x]=sum;
 90     sum-=b[x][1];
 91     if(son[x])dfs3(son[x]);
 92 }
 93 
 94 int main()
 95 
 96 {
 97 
 98     freopen("input.txt","r",stdin);
 99 
100     freopen("output.txt","w",stdout);
101 
102     n=read();
103     for1(i,n)a[i]=read();
104     for2(i,2,n)c[a[i]]++;
105     for1(i,n-1)insert(read(),read());
106     dep[1]=1;dfs(1);dfs2(1,1);
107     for1(i,n-1)
108     {
109         int x=a[i],y=a[i+1];
110         while(top[x]!=top[y])
111         {
112             if(dep[top[x]]<dep[top[y]])swap(x,y);
113             b[top[x]][0]++;
114             b[x][1]++;
115             x=fa[top[x]];
116         }
117         if(dep[x]>dep[y])swap(x,y);
118         b[x][0]++;
119         b[y][1]++;
120     }
121     for1(i,n)if(top[i]==i)dfs3(i);
122     for1(i,n)printf("%d\n",ans[i]-c[i]);
123 
124     return 0;
125 
126 }  
View Code

 

 

 

 

posted @ 2014-12-04 13:14  ZYF-ZYF  Views(369)  Comments(0Edit  收藏  举报