洛谷 P2982 [USACO10FEB]慢下来Slowing down
2016-09-27 15:48
309 查看
每天Farmer John的N头奶牛(1 <= N <= 100000,编号1…N)从粮仓走向他的自己的牧场。牧场构成了一棵树,粮仓在1号牧场。恰好有N-1条道路直接连接着牧场,使得牧场之间都恰好有一条路径相连。第i条路连接着A_i,B_i,(1 <= A_i <= N; 1 <= B_i <= N)。 奶牛们每人有一个私人牧场P_i (1 <= P_i <= N)。粮仓的门每次只能让一只奶牛离开。耐心的奶牛们会等到他们的前面的朋友们到达了自己的私人牧场后才离开。首先奶牛1离开,前往P_1;然后是奶牛2,以此类推。
当奶牛i走向牧场P_i时候,他可能会经过正在吃草的同伴旁。当路过已经有奶牛的牧场时,奶牛i会放慢自己的速度,防止打扰他的朋友。
FJ想要知道奶牛们总共要放慢多少次速度。
输入输出格式
输入格式:
Line 1: Line 1 contains a single integer: N
Lines 2..N: Line i+1 contains two space-separated integers: A_i and B_i
Lines N+1..N+N: line N+i contains a single integer: P_i
输出格式:
Lines 1..N: Line i contains the number of times cow i has to slow down.
输入输出样例
输入样例#1:
5
1 4
5 4
1 3
2 4
4
2
1
5
3
输出样例#1:
0
1
0
2
1
【分析】
树状数组维护dfs序差分
【代码】
当奶牛i走向牧场P_i时候,他可能会经过正在吃草的同伴旁。当路过已经有奶牛的牧场时,奶牛i会放慢自己的速度,防止打扰他的朋友。
FJ想要知道奶牛们总共要放慢多少次速度。
输入输出格式
输入格式:
Line 1: Line 1 contains a single integer: N
Lines 2..N: Line i+1 contains two space-separated integers: A_i and B_i
Lines N+1..N+N: line N+i contains a single integer: P_i
输出格式:
Lines 1..N: Line i contains the number of times cow i has to slow down.
输入输出样例
输入样例#1:
5
1 4
5 4
1 3
2 4
4
2
1
5
3
输出样例#1:
0
1
0
2
1
【分析】
树状数组维护dfs序差分
【代码】
//洛谷 P2982 [USACO10FEB]慢下来Slowing down #include<iostream> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> #define M(a) memset(a,0,sizeof a) #define fo(i,j,k) for(i=j;i<=k;i++) using namespace std; const int mxn=100005; vector <int> f[mxn]; int n,m,cnt=1; int dfn[mxn],pre[mxn],c[mxn],p[mxn]; 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; } inline int lowbit(int x) {return x&(-x);} inline void add(int x,int v) { for(int i=x;i<=n;i+=lowbit(i)) c[i]+=v; } inline int get(int x) { int sum=0; for(int i=x;i;i-=lowbit(i)) sum+=c[i]; return sum; } inline void dfs(int u) { int i,j,x=f[u].size()-1; dfn[u]=cnt; fo(i,0,x) { int v=f[u][i]; if(!dfn[v]) cnt++,dfs(v); } pre[u]=cnt; } int main() { int i,j,u,v,x,y; n=read(); fo(i,2,n) { u=read(),v=read(); f[u].push_back(v); f[v].push_back(u); } dfs(1); fo(i,1,n) p[i]=read(); fo(i,1,n) { printf("%d\n",get(dfn[p[i]])); add(dfn[p[i]],1),add(pre[p[i]]+1,-1); } return 0; }
相关文章推荐
- 洛谷P2982 [USACO10FEB]慢下来Slowing down [2017年四月计划 树状数组01]
- 洛谷 P2982 [USACO10FEB]慢下来Slowing down
- 洛谷P2982 [USACO10FEB]慢下来Slowing down
- 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying
- 洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving
- 洛谷P2982 [USACO10FEB]慢下来Slowing down(线段树 DFS序 区间增减 单点查询)
- 线段树+Dfs序【p2982】[USACO10FEB]慢下来Slowing down
- 洛谷P2984 [USACO10FEB]给巧克力Chocolate Giving
- 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying
- 洛谷2982 USACO10FEB 慢下来 Slowing down
- [luoguP2982][USACO10FEB]慢下来Slowing down(dfs序 + 线段树)
- 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying
- 【洛谷 P1709】[USACO5.5]隐藏口令Hidden Password
- 离散-洛谷P2205 [USACO13JAN]画栅栏Painting the Fence
- 洛谷P2738 [USACO4.1]篱笆回路Fence Loops(Floyed求最小环)
- 【枚举】洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
- 洛谷 P3094 [USACO13DEC]假期计划Vacation Planning
- 洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence
- 洛谷——P2737 [USACO4.1]麦香牛块Beef McNuggets
- 洛谷P1217 [USACO1.5]回文质数 Prime Palindromes