"CF" 121 Div.1 C. Fools and Roads

Source: Internet
Author: User

Test instructions is given a tree. Also, given the following K queries:

An arbitrary two-point u,v is given, and the edges of the path from U to V are counted.

After k queries, the sum of the counts of each side is output separately.

The idea utilizes LCA, on cnt[u]++, cnt[v]++, and on Cnt[lca (U, v)]-= 2.
Dfs then solves the count of each side.

1 /*191C*/2#include <iostream>3#include <string>4#include <map>5#include <queue>6#include <Set>7#include <stack>8#include <vector>9#include <deque>Ten#include <algorithm> One#include <cstdio> A#include <cmath> -#include <ctime> -#include <cstring> the#include <climits> -#include <cctype> -#include <cassert> -#include <functional> +#include <iterator> -#include <iomanip> + using namespacestd; A //#pragma COMMENT (linker, "/stack:102400000,1024000") at  - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> - #definePII pair<int,int> in #defineVpii vector<pair<int,int> > - #defineRep (I, A, n) for (int i=a;i<n;++i) to #definePer (i, a, n) for (int i=n-1;i>=a;--i) + #defineCLR Clear - #definePB Push_back the #defineMP Make_pair * #defineFIR First $ #defineSEC SecondPanax Notoginseng #defineAll (x) (x). Begin (), (x). End () - #defineSZ (x) ((int) (x). Size ()) the #defineLson L, Mid, rt<<1 + #defineRson mid+1, R, rt<<1|1 A  the Const intMAXN = 1e5+5; + Const intMaxd = -; - Vpii E[MAXN]; $ BOOLVISIT[MAXN]; $ intANS[MAXN]; - intCNT[MAXN]; - intDEEP[MAXN]; the intPre[maxn][maxd]; - Wuyi voidDfsintUintFA) { the     intSZ =SZ (E[u]), V; -      WuDeep[u] = Deep[fa] +1; -Rep (I,0, SZ) { Aboutv =E[u][i].fir; $         if(v = =FA) -             Continue; -pre[v][0] =u; -Rep (J,1, Maxd) APRE[V][J] = pre[pre[v][j-1]][j-1]; + Dfs (v, u); the     } - } $  the intLCA (intUintv) { the     if(Deep[u] >Deep[v]) the swap (U, v); the     if(Deep[u] <Deep[v]) { -         intTMP = Deep[v]-Deep[u]; inRep (I,0, Maxd) { the             if(TMP & (1<<i)) thev =Pre[v][i]; About         } the     } the      the     if(U! =v) { +Per (I,0, Maxd) { -             if(Pre[u][i]! =Pre[v][i]) { theU =Pre[u][i];Bayiv =Pre[v][i]; the             } the         } -         returnpre[u][0]; -}Else { the         returnu; the     } the } the  - intDFS2 (intUintEid) { the     intRET =0, Sz =SZ (E[u]); the     inte, V; the     94Visit[u] =true; theRep (I,0, SZ) { thev =E[u][i].fir; theE =e[u][i].sec;98         if(!Visit[v]) { AboutRET + =DFS2 (V, e); -         }101     }102RET + =Cnt[u];103Ans[eid] =ret;104      the     returnret;106 }107 108 intMain () {109Ios::sync_with_stdio (false); the #ifndef Online_judge111Freopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout);113     #endif the      the     intN; the     intu, v, FA;117     118scanf"%d", &n);119Rep (I,1, N) { -scanf"%d%d", &u, &v);121 E[U].PB (MP (V, i));122 E[V].PB (MP (U, i));123     }124      theDfs1,0);126     127     intK; -     129scanf"%d", &k); the      while(k--) {131scanf"%d%d", &u, &v); theFA =LCA (U, v);133++Cnt[u];134++Cnt[v];135CNT[FA]-=2;136     }137     138DFS2 (1,0);139Rep (I,1, N) $printf"%d", Ans[i]);141Putchar ('\ n');142     143 #ifndef Online_judge144printf"Time =%d.\n", (int) clock ());145     #endif146     147     return 0;148}

"CF" 121 Div.1 C. Fools and Roads

Related Article

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.