Codeforces Round #326 (Div. 1)-C. Duff in the Army tree multiplication algorithm

Source: Internet
Author: User

Test instructions: A number of n points, M personal live at some point in which each person's label 1-m, ask U-v path on the label before a person, and output designator, a < 10.

Practice, the use of multiplication, id[j][i] said I to I 2^j ancestors of the first 10 people, then each query direct maintenance is good, the details are many, just beginning do not know how to ask Id[j][i].

Here the 2^j is divided into two parts, the former 2^ (j-1) and the rear 2^ (j-1), and then the maintenance of the recursion.

Feel the tree chain can also be done, do not know if it will be tle, tree chain subdivision of the word segment tree each point maintenance 10 values, each merge on the line.

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5 +5;Const intMAXDEP = -;intPAR[MAXDEP][MAXN], DEP[MAXN], N;vector<int>ID[MAXDEP][MAXN], Cit[maxn];vector<int>G[MAXN];voidinit () { for(inti =0; i < MAXN; i++) {g[i].clear ();        Cit[i].clear ();  for(intj =0; J < MAXDEP; J + +) {id[j][i].clear (); }} memset (Par,-1,sizeof(PAR));}voidUpdate (Vector <int> &v1, Vector <int> &v2) {     for(intx:v2)    {v1.push_back (x);    } sort (V1.begin (), V1.end ());    V1.erase (Unique (V1.begin (), V1.end ()), V1.end ());  while(V1.size () >Ten) {v1.pop_back (); }}voidDfsintUintfather) {par[0][u] =father; Dep[u]= Dep[father] +1;  for(inti = u; I <= u; i++) {Update (id[0][i], cit[par[0][i]]); Update (id[0][i], cit[i]);  for(intj =0; J +1< MAXDEP; J + +) {            if(~Par[j][i]) {Par[j+1][i] =Par[j][par[j][i]]; Update (Id[j+1][i], cit[i]); Update (Id[j+1][i], id[j][i]); Update (Id[j+1][i], Id[j][par[j][i]]); } Else{par[j+1][i] =-1; }        }    }     for(intV:g[u]) {        if(V! =father)        {DFS (V, u); }    }}intLcaintUintv) {if(Dep[u] >Dep[v])    {Swap (U, v); }     for(intK =0; K < MAXDEP; k++) {        if((Dep[v]-dep[u]) >> K &1) {v=Par[k][v]; }    }    if(U = =v) {returnu; }     for(inti = maxdep-1; I >=0; i--) {        if(Par[i][u]! =Par[i][v]) {u=Par[i][u]; V=Par[i][v]; }    }    returnpar[0][u];}intAnc;vector<int> Solve (intUintv) {vector<int>Res; if(U = =v) {update (res, cit[u]); }     for(inti = maxdep-1; I >=0; i--) {        if(~par[i][u] && Dep[par[i][u]] >=Dep[anc])            {Update (res, id[i][u]); U=Par[i][u]; }    }     for(inti = maxdep-1; I >=0; i--) {        if(~par[i][v] && Dep[par[i][v]] >=Dep[anc])            {Update (res, id[i][v]); V=Par[i][v]; }} vector<int>EMP;    Update (res, EMP); returnRes;}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin);#endif    intm, q;  while(~SCANF ("%d%d%d", &n, &m, &q)) {init ();  for(inti =0; I < n1; i++) {            intu, v; scanf ("%d%d", &u, &v);            G[u].push_back (v);        G[v].push_back (U); }         for(inti =0; I < m; i++) {            intC; scanf ("%d", &c); if(Cit[c].size () <Ten) {Cit[c].push_back (i+1); id[0][c].push_back (i+1); }} DFS (1,0);  while(q--) {            intu, V, A; scanf ("%d%d%d", &u, &v, &a); ANC=LCA (U, v); Auto Res=Solve (U, v); inttot = min ((int) Res.size (), a); printf ("%d%c"Tot"\ n"[!tot]);  for(inti =0; I < min ((int) Res.size (), a); i++) {printf ("%d%c", Res[i],"\ n"[i+1==min ((int) Res.size (), a)]); }        }    }    return 0;}

Codeforces Round #326 (Div. 1)-C. Duff in the Army tree multiplication algorithm

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.