3631: [JLOI2014]松鼠的新家
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1941 Solved: 956
[ Submit][ Status][ Discuss]
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
1 4 5 3 2
1 2
2 4
2 3
4 5
Sample Output
1
2
1
2
1
2
1
2
1
HINT
2<= n <=300000
Source
#include<bits/stdc++.h>
#define ll long long
const int INF = 0x7fffffff;
const double eps = 1e-5;
const int maxn = 300005;
using namespace std;
int n,a[maxn],ans[maxn];
int cnt,head[maxn],next[maxn<<1],to[maxn<<1];
int sz,now,size[maxn],deep[maxn],fa[maxn],son[maxn],top[maxn],pid[maxn<<1],idp[maxn<<1];
int f[maxn<<2];
int read()
{
int x = 0 , f = 1; char ch = getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f*=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();}
return x * f;
}
void add(int u,int v)
{
to[++cnt] = v ; next[cnt] = head[u]; head[u] = cnt;
to[++cnt] = u ; next[cnt] = head[v]; head[v] = cnt;
}
inline void dfs1(int x,int f)
{
size[x] = 1 ;
for(int i = head[x] ; i ; i = next[i])
{
if(to[i] != f)
{
fa[to[i]] = x;
deep[to[i]] = deep[x] + 1;
dfs1(to[i],x);
if(size[to[i]] > size[son[x]]) son[x] = to[i];
size[x]+=size[to[i]];
}
}
}
inline void dfs2(int x,int chain)
{
pid[x]=++now;idp[now]=x;top[x]=chain;
if(son[x]) dfs2(son[x],chain);
for(int i = head[x]; i; i = next[i])
{
if(to[i]!=fa[x]&&to[i]!=son[x])
dfs2(to[i],to[i]);
}
}
void lazy(int pos)
{
f[pos<<1] += f[pos];
f[pos<<1|1] += f[pos];
f[pos] = 0;
}
inline void change(int pos,int l,int r,int x,int y)
{
if(x <= l && r <= y)
{
f[pos]++; return;
}
if(f[pos]) lazy(pos);
int mid = (l + r) >> 1;
if(y <= mid)
change(pos<<1,l,mid,x,y);
else if(x > mid)
change(pos<<1|1,mid+1,r,x,y);
else
change(pos<<1,l,mid,x,y),change(pos<<1|1,mid+1,r,x,y);
}
void ask(int pos,int l,int r)
{
if(l == r)
{
ans[idp[l]] = f[pos]; return;
}
if(f[pos]) lazy(pos);
int mid = (l + r) >> 1;
ask(pos<<1,l,mid);
ask(pos<<1|1,mid+1,r);
}
void work(int x,int y)
{
int f1 = top[x], f2 = top[y];
while(f1 != f2)
{
if(deep[f1]<deep[f2])
swap(x,y),swap(f1,f2);
change(1,1,n,pid[f1],pid[x]);
x = fa[f1];
f1= top[x];
}
if(deep[x] > deep[y])
swap(x,y);
change(1,1,n,pid[x],pid[y]);
}
int main()
{
n = read();
for(int i = 1 ; i <= n; i++)
a[i] = read();
for(int i = 1,x,y; i < n; i++)
x = read() , y = read() , add(x,y);
deep[1]=1;
dfs1(1,0);
dfs2(1,1);
for(int i=2;i<=n;++i)
work(a[i],a[i-1]);
ask(1,1,n);
for(int i=1;i<=n;i++)
printf("%d\n",i==a[1]?ans[i]:ans[i]-1); //除了第一个点其他每个点都有多算
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
#define inf 1000000000
#define ll long long
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int bin[20];
int n,cnt;
int fa[300005][20];
int a[300005],f[300005],last[300005],deep[300005];
struct edge{int to,next;}e[600005];
void insert(int u,int v)
{
e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
}
void dfs(int x)
{
for(int i=1;i<=18;i++)
if(deep[x]>=bin[i])
fa[x][i]=fa[fa[x][i-1]][i-1];
else break;
for(int i=last[x];i;i=e[i].next)
if(e[i].to!=fa[x][0])
{
deep[e[i].to]=deep[x]+1;
fa[e[i].to][0]=x;
dfs(e[i].to);
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y])swap(x,y);
int t=deep[x]-deep[y];
for(int i=0;i<=18;i++)
if(bin[i]&t)x=fa[x][i];
for(int i=18;i>=0;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
if(x==y)return x;
return fa[x][0];
}
void dp(int x)
{
for(int i=last[x];i;i=e[i].next)
if(e[i].to!=fa[x][0])
{
dp(e[i].to);
f[x]+=f[e[i].to];
}
}
void solve()
{
dfs(a[1]);
for(int i=1;i<n;i++)
{
int t=lca(a[i],a[i+1]);
f[a[i]]++;f[a[i+1]]++;
f[t]--;f[fa[t][0]]--;
}
dp(a[1]);
for(int i=2;i<=n;i++)f[a[i]]--;
for(int i=1;i<=n;i++)printf("%d\n",f[i]);
}
int main()
{
bin[0]=1;for(int i=1;i<20;i++)bin[i]=bin[i-1]<<1;
n=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<n;i++)
{
int u=read(),v=read();
insert(u,v);
}
solve();
return 0;
}