Bzoj1602: [usaco2008 Oct] farm walk

Source: Internet
Author: User
1602: [usaco Oct] ranch walking time limit: 5 sec memory limit: 64 MB
Submit: 1084 solved: 556
[Submit] [Status] Description

Nheaded ox (2 <= n <= 1000) when someone else is marked as 1 to n and grazed on N plots of land marked as 1 to n, the I-th ox grazes on the I-th pasture. The N pieces of land are connected by N-1 edges. The cows can walk on the edge. The I edge connects to the AI and Bi pastures. The I edge length is Li (1 <= LI <= 10000 ). These edges are arranged to be accessible by any two cows, so this is a tree. These cows are very communicative and often access each other. They want you to help them calculate the distance between Q (1 <= q <= 1000) and dairy cows.

Input

* First line: two integers separated by spaces: N and Q

* Line 2 to line N: line I + 1 has two integers separated by spaces: AI, Bi, Li

* Line n + 1 to line N + Q: each row has two spaces separated integers: P1 and P2, indicating the number of the two cows.

Output

* Row 1st to row Q: each row outputs a number, indicating the distance between the two cows.

Sample input4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
Sample output2
7
Hint Source

Qualifying Round

Question: I don't want to multiply the number. I just need to paste the code for tree link generation and change it to... Each node stores the distance to the father, so the code for the LCA should be determined:
  1 const maxn=1000+100;  2 type node1=record  3      go,next,z:longint;  4      end;  5      node2=record  6      l,r,mid,sum:longint;  7      end;  8   9 var  e:array[0..2*maxn] of node1; 10      t:array[0..4*maxn] of node2; 11      p,a,v,fa,s,head,dep,son,top:array[0..maxn] of longint; 12      i,n,m,x,y,z,sz,ans,tot:longint; 13      ch:char; 14      procedure swap(var x,y:longint); 15       var t:longint; 16       begin 17         t:=x;x:=y;y:=t; 18       end; 19      procedure insert(x,y,z:longint); 20       begin 21         inc(tot); 22         e[tot].go:=y;e[tot].z:=z;e[tot].next:=head[x];head[x]:=tot; 23       end; 24      function min(x,y:longint):longint; 25       begin 26         if x<y then exit(x) else exit(y); 27       end; 28      function max(x,y:longint):longint; 29       begin 30         if x>y then exit(x) else exit(y); 31       end; 32  33 procedure dfs1(x:longint); 34  var i,j,y:longint; 35  begin 36    j:=0;s[x]:=1; 37    i:=head[x]; 38    while i<>0 do 39     begin 40      y:=e[i].go; 41      if dep[y]=0 then 42       begin 43         dep[y]:=dep[x]+1; 44         fa[y]:=x;v[y]:=e[i].z; 45         dfs1(y); 46         inc(s[x],s[y]); 47         if s[y]>s[j] then j:=y; 48       end; 49      i:=e[i].next; 50     end; 51    son[x]:=j; 52  end; 53 procedure dfs2(x,chain:longint); 54  var i,y:longint; 55  begin 56    inc(sz);p[x]:=sz;top[x]:=chain; 57    if son[x]<>0 then dfs2(son[x],chain); 58    i:=head[x]; 59    while i<>0 do 60      begin 61       y:=e[i].go; 62       if (p[y]=0) and (y<>son[x]) then dfs2(y,y); 63       i:=e[i].next; 64      end; 65  end; 66 procedure pushup(k:longint); 67  begin 68     t[k].sum:=t[k<<1].sum+t[k<<1+1].sum; 69  end; 70  71 procedure build(k,x,y:longint); 72  begin 73    with t[k] do 74     begin 75      l:=x;r:=y;mid:=(l+r)>>1; 76      if l=r then begin sum:=a[l];exit;end; 77      build(k<<1,l,mid);build(k<<1+1,mid+1,r); 78      pushup(k); 79     end; 80  end; 81 function getsum(k,x,y:longint):longint; 82  begin 83    with t[k] do 84     begin 85      if (l=x) and (r=y) then exit(sum); 86      if y<=mid then exit(getsum(k<<1,x,y)) 87      else if x>mid then exit(getsum(k<<1+1,x,y)) 88      else exit(getsum(k<<1,x,mid)+getsum(k<<1+1,mid+1,y)); 89     end; 90  end; 91 procedure init; 92  begin 93    readln(n,m); 94    for i:=1 to n-1 do begin readln(x,y,z);insert(x,y,z);insert(y,x,z);end; 95    dep[1]:=1; 96    dfs1(1); 97    dfs2(1,1); 98    for i:=2 to n do a[p[i]]:=v[i]; 99    build(1,1,n);100  end;101 procedure solvesum;102  begin103    ans:=0;104    readln(x,y);105      while top[x]<>top[y] do106      begin107       if dep[top[x]]<dep[top[y]] then swap(x,y);108       inc(ans,getsum(1,p[top[x]],p[x]));109       x:=fa[top[x]];110      end;111    if dep[x]>dep[y] then swap(x,y);112    inc(ans,getsum(1,p[x],p[y]));113    if p[x]<>1 then dec(ans,v[x]);114    writeln(ans);115  end;116 117 procedure main;118  begin119    for i:=1 to m do solvesum;120  end;121 122 begin123   assign(input,‘input.txt‘);assign(output,‘output.txt‘);124   reset(input);rewrite(output);125   init;126   main;127   close(input);close(output);128 end.                                             
View code

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.