1782: [Usaco2010 Feb]slowdown 慢慢游

1782: [Usaco2010 Feb]slowdown 慢慢游

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 570  Solved: 346
[Submit][Status][Discuss]

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

Gold

题解:其实就是求以1为根节点的树上从树根到某一点的链上比自己权值小的点的个数

这样子经典的问题直接树转数组搞定= =,省选前刷水感觉棒棒哒

  1 /**************************************************************
  2     Problem: 1782
  3     User: HansBug
  4     Language: Pascal
  5     Result: Accepted
  6     Time:576 ms
  7     Memory:6972 kb
  8 ****************************************************************/
  9  
 10 type
 11     point=^node;
 12     node=record
 13                g:longint;
 14                next:point;
 15     end;
 16  
 17 var
 18    i,j,k,l,m,n,t:longint;
 19    a:array[0..100005] of point;
 20    b,c:array[0..100005,1..2] of longint;
 21    d,e:array[0..200000] of longint;
 22 procedure swap(var x,y:longint);
 23           var z:longint;
 24           begin
 25                z:=x;x:=y;y:=z;
 26           end;
 27 procedure sort(l,r:longint);
 28           var i,j,x,y:longint;
 29           begin
 30                i:=l;j:=r;x:=c[(l+r) div 2,1];
 31                repeat
 32                      while c[i,1]<x do inc(i);
 33                      while c[j,1]>x do dec(j);
 34                      if i<=j then
 35                         begin
 36                              swap(c[i,1],c[j,1]);
 37                              swap(c[i,2],c[j,2]);
 38                              inc(i);dec(j);
 39                         end;
 40                until i>j;
 41                if i<r then sort(i,r);
 42                if l<j then sort(l,j);
 43           end;
 44 procedure edg(x,y:longint);
 45           var p:point;
 46           begin
 47                new(p);p^.g:=y;p^.next:=a[x];a[x]:=p;
 48           end;
 49 function sum(x:longint):longint;
 50          begin
 51               sum:=0;
 52               while x>0 do
 53                     begin
 54                          inc(sum,d[x]);
 55                          dec(x,x and (-x));
 56                     end;
 57          end;
 58 procedure add(x,y:longint);
 59           begin
 60                if x=0 then exit;
 61                while x<=n do
 62                      begin
 63                           inc(d[x],y);
 64                           inc(x,x and (-x));
 65                      end;
 66           end;
 67 procedure dfs(y,x:longint);
 68           var p:point;
 69           begin
 70                add(x,1);
 71                e[x]:=sum(x-1);p:=a[x];
 72                while p<>nil do
 73                      begin
 74                           if p^.g<>y then dfs(x,p^.g);
 75                           p:=p^.next;
 76                      end;
 77                add(x,-1);
 78           end;
 79  
 80 begin
 81      readln(n);t:=1;
 82      for i:=1 to n-1 do readln(b[i,1],b[i,2]);
 83      for i:=1 to n do
 84          begin
 85               c[i,2]:=i;
 86               readln(c[i,1]);
 87          end;
 88      sort(1,n);
 89      for i:=1 to n-1 do
 90          begin
 91               b[i,1]:=c[b[i,1],2];
 92               b[i,2]:=c[b[i,2],2];
 93               edg(b[i,1],b[i,2]);
 94               edg(b[i,2],b[i,1]);
 95          end;
 96      t:=c[t,2];
 97      fillchar(d,sizeof(d),0);
 98      dfs(0,t);
 99      for i:=1 to n do writeln(e[i]);
100      readln;
101 end.    
原文地址:https://www.cnblogs.com/HansBug/p/4431724.html