zoukankan      html  css  js  c++  java
  • BZOJ1782: [Usaco2010 Feb]slowdown 慢慢游

    1782: [Usaco2010 Feb]slowdown 慢慢游

    Time Limit: 1 Sec  Memory Limit: 64 MB
    Submit: 541  Solved: 326
    [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

    Sample Output

    0
    1
    0
    2
    1

    HINT

     

    Source

    题解:
    树状数组+dfs序。。。大都市meg的简化版
    代码:
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1000000000
    13 #define maxn 100000+1000
    14 #define maxm 500+100
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 using namespace std;
    19 inline int read()
    20 {
    21     int x=0,f=1;char ch=getchar();
    22     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    23     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    24     return x*f;
    25 }
    26 struct edge{int go,next;}e[2*maxn];
    27 int n,tot,ti,s[2*maxn],l[maxn],r[maxn],head[maxn];
    28 bool v[maxn];
    29 void insert(int x,int y)
    30 {
    31     e[++tot].go=y;e[tot].next=head[x];head[x]=tot;
    32     e[++tot].go=x;e[tot].next=head[y];head[y]=tot;
    33 }
    34 void dfs(int x)
    35 {
    36     v[x]=1;
    37     l[x]=++ti;
    38     for(int i=head[x],y;i;i=e[i].next)
    39      if(!v[y=e[i].go])dfs(y);
    40     r[x]=++ti; 
    41 }
    42 void add(int x,int y)
    43 {
    44     for(;x<=2*n;x+=x&(-x))s[x]+=y;
    45 }
    46 int sum(int x)
    47 {
    48     int t=0;
    49     for(;x;x-=x&(-x))t+=s[x];
    50     return t;
    51 }
    52 int main()
    53 {
    54     freopen("input.txt","r",stdin);
    55     freopen("output.txt","w",stdout);
    56     n=read();
    57     int x,y;
    58     for(int i=1;i<n;i++)x=read(),y=read(),insert(x,y);
    59     dfs(1);
    60     for(int i=1;i<=n;i++)
    61      {
    62          int x=read();
    63          printf("%d
    ",sum(l[x]));
    64          add(l[x],1);add(r[x],-1);
    65      } 
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    phpstudy apache 服务无法启动
    Nginx+keepalived实现负载均衡高可用配置
    Linux系统下zookeeper客户端命令使用
    JVM探究之 —— 类文件结构(脑图)
    JVM探究之 —— 类加载器-双亲委派模型
    Centos7 配置静态IP并使用xshell远程连接
    JVM探究之 —— 类加载过程
    JVM探究之 —— 垃圾回收(二)
    避免git clone和push时每次都需要输入用户名和密码
    jsch配置sftp服务器ssh免密登录
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3941400.html
Copyright © 2011-2022 走看看