
标签:
题解:其实就是求以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.
1782: [Usaco2010 Feb]slowdown 慢慢游
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4431724.html