Bzoj 1787 [Ahoi2008]meet Emergency Collection (1832 [AHOI2008] party)

Source: Internet
Author: User

1787: [Ahoi2008]meet Emergency collection Time limit:20 Sec Memory limit:162 MB
submit:1841 solved:857
[Submit] [Status] [Discuss] Description input Output Sample input6 4
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
Sample Output
5 2
2 5
4 1
6 0HINT

Source

Day1

Ideas

Lca.

To find the LCA between three points, there will be two points of the same situation, the meeting point is another point.

Code

1 /**************************************************************2 problem:17873 user:hahalidaxin24 language:c++5 result:accepted6 time:3716 Ms7 memory:65216 KB8 ****************************************************************/9  Ten#include <cstdio> One#include <cstring> A#include <queue> -#include <vector> -#include <iostream> the using namespacestd; -    - Const intMAXN =500000+Ten; - Const intMaxd = +; +    - structedge{intu,v; + }; Avector<int>G[MAXN]; atVector<edge>es; -    - intD[MAXN]; - intP[maxn][maxd]; -    - voidAddedge (intUintv) { in Es.push_back (Edge) {u,v}); -     intM=es.size (); G[u].push_back (M-1); to } + voidDfsintUintFA) {  -      for(intI=1; i<=maxd;i++) {//construct a multiplication array the         if(d[u]< (1<<i)) Break; *p[u][i]=p[p[u][i-1]][i-1]; $     }Panax Notoginseng      for(intI=0; I<g[u].size (); i++) { -Edge E=es[g[u][i]];intv=e.v; the         if(v!=FA) +d[v]=d[u]+1, p[v][0]=u, DFS (v,u); A     } the } + intLcaintUintv) { -     if(d[v]>D[u]) swap (U,V); $     intdep=d[u]-D[v]; $      for(intI=0; i<maxd;i++) -         if((1<<i) &AMP;DEP) u=P[u][i]; -     if(U==V)returnu; the      for(inti=maxd-1; i>=0; i--) -         if(p[u][i]!=P[v][i])WuyiU=p[u][i], v=P[v][i]; the     returnp[u][0]; - } Wu intDistintXintY) {returnd[x]+d[y]-(D[lca (x, y)]<<1); } - intn,m; About    $ voidReadint&x) { -     CharC=GetChar (); -      while(!isdigit (c)) c=GetChar (); -x=0; A      while(IsDigit (c)) +x=x*Ten+c-'0', c=GetChar (); the } - intMain () { $ read (n), read (m); the     intu,v; the      for(intI=0; i<n-1; i++) { the Read (U), read (v); the Addedge (u,v); Addedge (v,u); -     } inDFS (n>>1,-1); the     inta,b,c,lab,lac,lbc,s; the      while(m--) { About Read ( a), read (b), read (c); theLab=lca (A, B), Lac=lca (a,c), lbc=LCA (B,C); the         if(LAB==LAC) s=LBC; the         Else if(LAB==LBC) s=Lac; +         Elses=Lab; -printf"%d%d\n", S,dist (a,s) +dist (b,s) +Dist (c,s)); the     }Bayi     return 0; the}

Bzoj 1787 [Ahoi2008]meet Emergency Collection (1832 [AHOI2008] party)

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.