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) &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)