"BZOJ2815" "ZJOI2012" disaster amoeba and cockroach dynamic multiplication LCA disaster tree

Source: Internet
Author: User

Advertising:
#include <stdio.h>int main(){    puts("转载请注明出处[vmurder]谢谢");    puts("网址:blog.csdn.net/vmurder/article/details/44104163");}
Test instructions

Please see the original question surface JS S hININg Blog

Http://www.cnblogs.com/JS-Shining/archive/2013/01/12/2857429.html

Exercises

We build a disaster tree so that any ancestor of one node is extinct, then it is extinct.
You can sweep each node according to the topology sequence and then add it to the disaster tree simply by assigning its father to the LCA of all its food.
We can dynamically process multiply LCA arrays for each node F i,J Represents the first of I 1 << j ) High ancestor.

Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 70000#define M 600000#define LOGNusing namespace STD;structksd{intV,next;} E[M],E[M],EE[M];intHead[n],cnt,head[n],cnt,head[n];inline voidAddintUintV) {e[++cnt].v=v;    Ee[cnt].v=u;    E[cnt].next=head[u];    EE[CNT].NEXT=HEAD[V]; head[v]=head[u]=cnt;}inline voidADD (intUintV) {e[++cnt].v=v;    E[cnt].next=head[u]; head[u]=cnt;}intF[n][logn],dep[n];intGetlca (intAintb) {intIif(Dep[a]<dep[b]) swap (A, b); for(i=logn-1; i>=0; i--)if(Dep[f[a][i]]>=dep[b]) a=f[a][i];if(a==b)returnA for(i=logn-1; i>=0; i--)if(F[a][i]!=f[b][i]) a=f[a][i],b=f[b][i];returnf[a][0];}intD[n],ans[n]; Queue<int>QintDfsintx) {ans[x]=1; for(intI=head[x];i;i=e[i].next) Ans[x]+=dfs (E[I].V);returnANS[X];}intNintMain () {intI,j,k;intA,b,c;intU,v,lca;scanf("%d", &n); for(i=1; i<=n;i++) { while(scanf("%d", &a), a) {Add (a,i);        d[i]++; }    } for(i=1; i<=n;i++)if(!d[i]) Add (n+1, i), d[i]=1; Q.push (n+1); while(!q.empty ())        {U=q.front (), Q.pop (); Lca=0; for(I=head[u];i;i=ee[i].next) {v=ee[i].v;if(I==head[u]) lca=v;ElseLca=getlca (LCA,V); } for(I=head[u];i;i=e[i].next) {d[v=e[i].v]--;if(!d[v]) Q.push (v); }if(LCA) {ADD (lca,u), f[u][0]=lca; for(j=1; j<logn;j++) f[u][j]=f[f[u][j-1]][j-1]; } dep[u]=dep[lca]+1; } DFS (n+1); for(i=1; i<=n;i++)printf("%d\n", ans[i]-1);return 0;}

"BZOJ2815" "ZJOI2012" disaster amoeba and cockroach dynamic multiplication LCA disaster tree

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.