51nod1253 Kundu and Tree

Source: Internet
Author: User

The tree consists of n points and N-1 edges. The sides of the tree have 2 colors red (' r ') and Black (' B '). Given the color of the N-1 edges, ask how many triples (a,b,c) of nodes are satisfied: Node A to Node B, node B to node C, node c to Node A, each path has at least one edge red. Note (a,b,c), (B,a,c), and all other permutations are considered to be the same ternary group. The result of the output is 1000000007.   Input
Line 1th: 1 number N (1 <= n <= 50000) line 2-n: 2 numbers per line plus one color, indicating the start and end of edges and color.
Output
Outputs 1 numbers, corresponding to the number of 3 tuples that meet the criteria.

preprocessing for each edge (A, A, b), the path from a to go through the number of paths is a/no red edge

There are two types of situations:

1.a,b,c 22 paths do not go through the 3rd, when three paths have a unique common point, the common point to A,b,c path at least two have red edge, thus can be counted

A path with two points in the 2.a,b,c is passed through the 3rd, when the enumeration is counted.

#include <cstdio>typedefLong Longi64;Const intn=50007, r=n* +;Charbuf[r+7],*ptr=buf-1;int _(){    intx=0, c=*++ptr;  while(c< -) c=*++ptr;  while(c> -) x=x*Ten+c- -, c=*++ptr; returnx;}int_c () {intc=*++ptr;  while(c<'a') c=*++ptr; returnc=='R';}intN;intes[n*2],enx[n*2],e0[n],ev[n*2],ep=2;intf1[n],f2[n],f3[n],sz[n];i64 ans=0;voidDFS1 (intWintPA) {Sz[w]=1;  for(intI=e0[w];i;i=Enx[i]) {        intu=Es[i]; if(u!=PA)            {DFS1 (u,w); SZ[W]+=Sz[u]; if(Ev[i]) f1[w]+=f3[u]=Sz[u]; Elsef1[w]+=f3[u]=F1[u]; }    }}voidDFS2 (intWintPA) {     for(intI=e0[w];i;i=Enx[i]) {        intu=Es[i]; if(u!=PA) {            if(Ev[i]) f2[u]=n-Sz[u]; Elsef2[u]=f2[w]+f1[w]-F1[u];        DFS2 (U,W); }    }}voidDFS3 (intWintPA) {    Statici64 Fs[n],fl[n],fr[n],ss[n],sl[n],sr[n]; Static intp;  for(intI=e0[w];i;i=Enx[i]) {        intu=Es[i]; if(u!=pa) dfs3 (u,w); } P=0;  for(intI=e0[w];i;i=Enx[i]) {        intu=Es[i]; ++p; if(u!=PA) {Fs[p]=fl[p]=fr[p]=F3[u]; SS[P]=sl[p]=sr[p]=sz[u]-Fs[p]; }Else{Fs[p]=fl[p]=fr[p]=F2[w]; SS[P]=sl[p]=sr[p]=n-sz[w]-Fs[p]; }} i64 A0=ans;  for(intI=2; i<=p;++i) fl[i]+=fl[i-1],sl[i]+=sl[i-1];  for(inti=p-1; i;--i) fr[i]+=fr[i+1],sr[i]+=sr[i+1];  for(intI=2; i<p;++i) {ans+=fl[i-1]* (fs[i]*sr[i+1]+ss[i]*fr[i+1]); Ans+ = (sl[i-1]+fl[i-1]) *fs[i]*fr[i+1]; }     for(intI=1; i<p;++i) ans+=fl[i]*fs[i+1];}intMain () {fread (buf,1, R,stdin); N=_();  for(intI=1; i<n;++i) {        intA=_ (), B=_ (), c=_c (); ES[EP]=b;enx[ep]=e0[a];ev[ep]=c;e0[a]=ep++; ES[EP]=a;enx[ep]=e0[b];ev[ep]=c;e0[b]=ep++; } DFS1 (1,0); DFS2 (1,0); DFS3 (1,0); printf ("%lld", ans%1000000007); return 0;}

51nod1253 Kundu and 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.