Codeforces E. famil Door and Roads expectations

Source: Internet
Author: User

A tree, n nodes, the edge length is 1, there are Q queries, each query given u,v (u! = V), ask in the tree the probability plus an edge, if the u,v in a ring, then this method of addition is legal, the value is the length of the ring, all the legal edge of the occurrence of equal probability, ask the value of expectations.

2 <= n,m <= 10^5

For u,v the original path on the edge must be within the ring, the contribution of 1, the new addition of the edge must also be within the ring, contribute to 1, the rest of the side of the contribution of the line

Consider 2 scenarios:

1.LCA (u,v) Not equal to U and V

2.lca (u,v) for u or V


//File Name:cf629E.cpp//Created time:2017 January 05 Thursday 17:20 29 seconds#include<bits/stdc++.h>#defineLL Long Longusing namespacestd;Const intMAXN =100000+2;intSIZ[MAXN],DIS[MAXN];intpa[maxn][ -]; LL F[maxn],g[maxn];vector<int>G[MAXN];voidDfs0 (intUintp) {Siz[u]=1; Dis[u]= Dis[p] +1; F[u]=0;  for(intI=0; I<g[u].size (); + +i) {        intv =G[u][i]; if(v = = p)Continue; pa[v][0] =u;        Dfs0 (V,u); Siz[u]+=Siz[v]; F[u]+ = F[v] +Siz[v]; }}voidDFS1 (intUintPintN) {     for(intI=0; I<g[u].size (); + +i) {        intv =G[u][i]; if(v = = p)Continue; G[V]= n + g[u]-2*Siz[v];    DFS1 (V,u,n); }}voidCAL_PA (intN) {     for(intj=1;(1&LT;&LT;J) <=n;++K) {         for(intI=1; i<=n;++i) {            if(pa[i][j-1] != -1) Pa[i][j]= pa[pa[i][j-1]][j-1]; }    }}intCal_lca (intAintb) {    if(Dis[a] <dis[b]) swap (A, b); intCNT =0;  for(;(1&LT;&LT;CNT) <=dis[a];++CNT); --CNT;  for(intj=cnt;j>=0;--j) {        if(Dis[a]-(1&LT;&LT;J) >=Dis[b]) a=Pa[a][j]; }    if(A = = b)returnA;  for(intj=cnt;j>=0;--j) {        if(Pa[a][j]! =-1&& Pa[a][j]! =Pa[b][j]) a= Pa[a][j],b =Pa[b][j]; }    returnpa[a][0];}intCalintUintv) {    intCNT =0;  for(;(1&LT;&LT;CNT) <=dis[u];++CNT); --CNT;  for(intj=cnt;j>=0;--j) {        if(Dis[u]-(1&LT;&LT;J) >dis[v]) u=Pa[u][j]; }    returnu;}voidSolveintNintm) {memset (PA,-1,sizeof(PA)); Dfs0 (1,0);//DIS,SIZ,IN,F,PA[I][0],DEPg[1] = f[1]; DFS1 (1,0, n);//gCAL_PA (n);//PA    intu,v,lca,w;  while(m--) {scanf ("%d%d",&u,&v); LCA=Cal_lca (U,V); if(LCA! = u && LCA! =v) {            intTMP = Dis[u] + dis[v]-2* DIS[LCA] +1; DoubleAns = tmp + (F[u] +0.0)/Siz[u] + (F[v] +0.0) /Siz[v]; printf ("%.15f\n", ans); }        Else{            if(LCA = =u) swap (u,v); W=cal (U,V); if(pa[w][0] !=v) {printf ("-1"); return ; }            intTMP = Dis[u]-DIS[V] +1; DoubleAns = tmp + (F[u] +0.0)/Siz[u] + (G[v]-f[w]-siz[w] +0.0)/(N-siz[w]); printf ("%.15f\n", ans); }    }}intMain () {intn,m; scanf ("%d%d",&n,&m);  for(intI=1, u,v;i<n;++i) {scanf ("%d%d",&u,&v);        G[u].push_back (v);    G[v].push_back (U);    } solve (n,m); return 0;}

Codeforces E. famil Door and Roads expectations

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: 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.