"More than 2016 schools" T2 forest (tree dp, number theory)

Source: Internet
Author: User

Test instructions: There is a tree of n points, each point on a bit of right

Defines the sum of the points of the path length for all points passing through, the diameter of the tree is the largest path length in a tree

There are n inquiries, each asking for answers to the product of the diameter of all trees

One edge is deleted after each inquiry, and the number of trees is +1.

Ask to answer n times, answer mod 10^9+7

n<=100000

Idea: Because know each time to delete which side so you can do it offline, add one side at a time

The Edge will merge the two trees, considering the merging of the trees.

Knowing the shape of the original tree, the parent-child relationship between points

Consider DP, set dp[u,1],dp[u,2] to be the root of u, the longest and the minimum of the path in the subtree, and record from which son to fetch

F[u] is the maximum length of a path in a subtree that is rooted in U

Note that the longest with the second must be transferred by two different sons, which are updated attention

For ans[i] to merge x and Y, they must be parent-child relationships in the original tree

Father with X as Y

The new ans is the old ans/old f[y]* new f[the root of the subtree, that is, the topmost root node of x]

Recursion up

The division modulus is appeared, and the inverse element is obtained using the Fermat theorem

a^ (p-2) =a^-1 (mod p)

The solution is brute force + multiplication optimization, with a diameter of four points to the maximum value of U1,V1,U2,V2

But I'm too lazy to write

1 Constmo=1000000007;2 varG:Array[1..100000,1..2] ofInt64;3H:Array[1..100000,1..2] ofLongint;4FA,CX,CY,B,FF:Array[1..100000] ofLongint;5F,a,ans:Array[1..100000] ofInt64;6Head,vet,next:Array[1..300000] ofLongint;7 N,i,x,y,tot:longint;8 T:int64;9 Ten procedureAdd (a,b:longint); One begin A Inc (TOT); -next[tot]:=Head[a]; -vet[tot]:=b; thehead[a]:=tot; - End; -  - functionMax (X,y:int64): Int64; + begin -  ifX>y Thenexit (x); + exit (y); A End; at  - procedureSwapvarx,y:longint); - varT:longint; - begin -T:=x; X:=y; y:=T; - End; in  - procedureDFS (u,pre:longint); to varE,v:longint; + begin -e:=Head[u]; the   whileE<>0  Do *  begin $v:=Vet[e];Panax Notoginseng   ifV<>pre Then -   begin theff[v]:=u; + DFS (v,u); A   End; thee:=Next[e]; +  End; - End; $  $ functionmi (x,y:int64): Int64; - varTmp:int64; - begin themi:=1; tmp:=x; -   whileY>0  DoWuyi  begin the   ifY and 1=1  ThenMi:=mi*tmpMoDmo; -Tmp:=tmp*tmpMoDmo; WuY:=y>>1; -  End; About End; $  - functionexf (X:int64): Int64; - begin -Exit (MI (x,mo-2)); A End; +  the begin -Assign (input,'forest.in'); Reset (input); $Assign (output,'Forest.out'); Rewrite (output); the READLN (n); the   fori:=1  toN Do the  begin the read (a[i]); -f[i]:=A[i]; in  End; the   fori:=1  toN-1  Do the  begin About read (cx[i],cy[i]); the Add (Cx[i],cy[i]); the Add (Cy[i],cx[i]); the  End; +   fori:=1  toN-1  Doread (b[i]); -Dfs1,-1); thet:=1;Bayi   fori:=1  toN DoT:=t*a[i]MoDmo; theans[n]:=T; the   fori:=n-1 Downto 1  Do -  begin -X:=cx[b[i]]; y:=Cy[b[i]]; the   ifFf[y]<>x Thenswap (x, y); theT:=T*EXF (F[y])MoDmo; thefa[y]:=x; the    whileX>0  Do -   begin the    iffa[x]=0  ThenT:=T*EXF (F[x])MoDmo; thef[x]:=Max (f[x],f[y]); the    ifG[y,1]+a[y]>g[x,1] Then94    begin the     ifH[x,1]<>y Then the     begin theG[x,2]:=g[x,1]; H[x,2]:=h[x,1];98     End; AboutG[x,1]:=g[y,1]+A[y]; -H[x,1]:=y;101    End102     Else if(G[y,1]+a[y]>g[x,2]) and(Y<>h[x,1]) Then103     begin104G[x,2]:=g[y,1]+A[y]; theH[x,2]:=y;106     End;107F[x]:=max (F[x],g[x,1]+g[x,2]+a[x]);108Y:=x; x:=Fa[x];109   End; theT:=t*f[y]MoDmo;111ans[i]:=T; the  End;113   fori:=1  toN DoWriteln (Ans[i]); the  the close (input); the close (output);117 End.

"More than 2016 schools" T2 forest (tree dp, number theory)

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.