【bzoj 1782】: [Usaco2010 Feb]slowdown 慢慢游
来源:互联网 发布:淘宝怎么看下单排名 编辑:程序博客网 时间:2024/05/02 21:55
1782: [Usaco2010 Feb]slowdown 慢慢游
Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 559 Solved: 340
[Submit][Status]
Description
每天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会放慢自己的速度,防止打扰他的朋友。 考虑如下的牧场结构(括号内的数字代表了牧场的所有者)。
Input
* 第1行 : 一个正整数N * 第2…N行: 第i+1行包括一对正整数A_i,B_i * 第N+1..N+N行: 第 N+i行 包括一个正整数: P_i
Output
* 第一行到第N行:第i行表示第i只奶牛需要被放慢的次数
Sample Input
5
1 4
5 4
1 3
2 4
4
2
1
5
3
1 4
5 4
1 3
2 4
4
2
1
5
3
Sample Output
0
1
0
2
1
1
0
2
1
查询某个节点的答案
就是看它到根节点有多少个比它小
所以按值建立树状数组,维护dfs序
//#define _TEST _TEST#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <cmath>#include <algorithm>using namespace std;/************************************************Code By willinglive************************************************//////////////////////////////////////////////////#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)#define MS(arr,x) memset(arr,x,sizeof(arr))#define LL long long#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)/////////////////////////////////////////////////const int N=100010;int n;struct edge{int v,next;}e[N*2];int head[N],k;int ans[N],id[N];int c[N];/////////////////////////////////////////////////inline int lowbit(int x){return x&-x;}void add(int id,int x){for(;id<=n;id+=lowbit(id))c[id]+=x;}int query(int id){int res=0;for(;id>0;id-=lowbit(id))res+=c[id];return res;}void adde(int u,int v){e[k].v=v;e[k].next=head[u];head[u]=k++;}inline int getint(){ int res=0;char c=getchar(); while(!isdigit(c))c=getchar(); while(isdigit(c))res=res*10+c-'0',c=getchar(); return res;}void dfs(int u,int fa){ ans[id[u]]=query(id[u]); add(id[u],1); INE(i,u,e) { int v=e[i].v; if(v==fa) continue; dfs(v,u); } add(id[u],-1);}/////////////////////////////////////////////////void input(){ MS(head,-1); n=getint(); int u,v; rep(i,1,n-1) u=getint(),v=getint(),adde(u,v),adde(v,u); rep(i,1,n) id[getint()]=i;}void solve(){ ///////////////////init/////////////////// ////////////////calculate//////////////// dfs(1,-1); /////////////////output///////////////// rep(i,1,n) printf("%d\n",ans[i]);}/////////////////////////////////////////////////int main(){ #ifndef _TEST freopen("std.in","r",stdin); freopen("std.out","w",stdout); #endif input(); solve(); #ifdef _TEST for(;;); #endif return 0;}
0 0
- 【bzoj 1782】: [Usaco2010 Feb]slowdown 慢慢游
- bzoj 1782: [Usaco2010 Feb]slowdown 慢慢游
- BZOJ 1782 [Usaco2010 Feb]slowdown慢慢游 树状数组
- 1782: [Usaco2010 Feb]slowdown 慢慢游
- bzoj 1782: [Usaco2010 Feb]slowdown 慢慢游 (线段树+DFS序)
- BZOJ 1782 [Usaco2010 Feb]slowdown 慢慢游 dfs序,树状数组
- bzoj1782: [Usaco2010 Feb]slowdown 慢慢游
- [BZOJ1782] [Usaco2010 Feb]slowdown 慢慢游
- bzoj1782[Usaco2010 Feb]slowdown 慢慢游
- [bzoj1782][Usaco2010 Feb]slowdown 慢慢游 dfs序+线段树
- [BZOJ1782][Usaco2010 Feb]slowdown 慢慢游(dfs序+线段树)
- BZOJ 2015: [Usaco2010 Feb]Chocolate Giving spfa
- bzoj 2015: [Usaco2010 Feb]Chocolate Giving
- 【洛谷2982】[Usaco2010 Feb]慢下来Slowdown(dfs序+线段树)
- chocolate giving[usaco2010 Feb]
- bzoj2014 [Usaco2010 Feb]Chocolate Buying
- bzoj2015 [Usaco2010 Feb]Chocolate Giving
- [BZOJ2015][Usaco2010 Feb]Chocolate Giving
- 破解之路
- Android 微信分享,无需那么麻烦。
- 《深入理解Flash Player的安全域(Security Domains)》(下)
- 注脚框架-AndroidAnnotations
- 启动SOA Suit 12c server
- 【bzoj 1782】: [Usaco2010 Feb]slowdown 慢慢游
- Java多线程总结之由synchronized说开去
- hhhhhhhh
- 数据结构 第四章
- PL/SQL Developer使用技巧
- 黑马程序员-day13字符串String
- Bash Shell 流程控制 —— select
- 文件下载-SpringMVC中测试
- android开发No Launcher activity found! 解决方法