The first direct no brain Tarjan, backtracking can only be a layer up, too slow, add a variety of optimization or tle
The LCA multiplication method (online) was later learned. Complexity compared to LCA RMQ and Tarjan is slightly worse, but it can be synchronized maintenance of only the LCA multiplication, very magical algorithm
#include"Cstdio"#include"Queue"#include"Cmath"#include"Stack"#include"iostream"#include"algorithm"#include"CString"#include"Queue"#include"Map"#include"Vector"#definell Long Longusing namespacestd;Const intMAXN = 1e5+ -;Const intMaxe =200500;Const intINF =0x3f3f3f;structnode{intE,next; Node () {} node (intAintb): E (a), next (b) {}}edge[maxe];intN,m,q,tot;intVIS[MAXN],FIRST[MAXN],DEEP[MAXN];intp[maxn][ +];///record I go up jump 2^j to reach the nodevector<int> g[maxn][ +],ANS,NUM[MAXN];///G[i][j] record I up to the top of the 2^j nodes of the 10 minimum (not including I nodes, in order to update the convenience of)voidinit () {tot=0; memset (Vis,0,sizeof(VIS)); memset (First,-1,sizeof(first)); Memset (P,-1,sizeof(P));}voidAddedge (intUintv) {Edge[tot]=node (v,first[u]); First[u]=tot++; Edge[tot]=node (u,first[v]); FIRST[V]=tot++;}voidUpdate (vector<int> a,vector<int> b,vector<int> &c) {///Merging intSa=a.size (); intsb=b.size (); intI=0, j=0; intVa,vb; while(i+j<Ten&& (i<sa| | j<SB)) { if(I<SA) va=A[i]; ElseVa=INF; if(J<SB) vb=B[j]; Elsevb=INF; if(va<vb) {c.push_back (VA); I++; } Else{C.push_back (VB); J++; } }}voidMerge (vector<int> &a,vector<int> B) {///Merging intsb=b.size (); for(intI=0; i<sb;i++) A.push_back (B[i]);}voidinit_p () { for(intj=1;(1<<J) <=n;j++) for(intI=1; i<=n;i++)if(p[i][j-1]!=-1) {P[i][j]=p[p[i][j-1]][j-1];///I go up jump 2^j equivalent I jump up 2 times 2^ (j-1)Update (g[i][j-1],g[p[i][j-1]][j-1],g[i][j]); }}voidDfsintUintDEP) {Vis[u]=1; Deep[u]=DEP; for(inti=first[u];i!=-1; i=Edge[i].next) { intv=EDGE[I].E; if(Vis[v])Continue; p[v][0]=u;///Initialize, 2^0=1, equivalent to Father nodeg[v][0]=num[u];///InitializeDFS (v,dep+1); }}voidGet_lca (intUintVintk) { if(Deep[u]<deep[v]) swap (U,V);///guaranteed Deep[u]>deep[v] ease of Operation intA=u,b=v;///do not merge the elements inside the u,v to avoid duplication, so save them first .ans.clear (); intdif=deep[u]-Deep[v]; for(intI=0; i<= -; i++)if(dif& (1<<i)) {Merge (ans,g[u][i]); U=P[u][i]; } if(u!=v) { for(intI= -; i>=0; i--)if(p[u][i]!=-1&&p[u][i]!=P[v][i]) {Merge (ans,g[v][i]); Merge (Ans,g[u][i]); V=P[v][i]; U=P[u][i]; } Merge (Ans,num[a]); ///This is a u,v of two sub-trees of the LCA.Merge (ans,num[b]); Merge (ans,num[p[u][0]]); } ElseMerge (Ans,num[a]);///in this case V is the LCA, so only merge Usort (Ans.begin (), Ans.end ()); intT=min (K, (int) ans.size ()); cout<<t<<' '; for(intI=0; i<t;i++) cout<<ans[i]<<' '; cout<<Endl;}intMain () {//freopen ("In.txt", "R", stdin);scanf"%d%d%d",&n,&m,&q); Init (); for(intI=1; i<n;i++){ intU,V;SCANF ("%d%d",&u,&v); Addedge (U,V); } for(intI=1; i<=m;i++){ intT;SCANF ("%d",&t); if(Num[t].size () <Ten) Num[t].push_back (i); } DFS (1,1); Init_p (); for(intI=0; i<q;i++){ intu,v,k; scanf ("%d%d%d",&u,&v,&k); Get_lca (U,V,K); } return 0;}
View Code
Codeforces 587C: (LCA multiplier + maintenance minimum)