Bzoj2152: Cong Coco

Source: Internet
Author: User
2152: Cong Coco time limit: 3 sec memory limit: 259 MB
Submit: 448 solved: 236
[Submit] [Status] Description

Cong and ke are brothers. They often fight for trivial matters, for example, there is only one last popsicle in the House, and both of them want to eat it. Both of them want to play with computers (but they only have one computer in their house )...... In general, stone scissors are good, but they are tired of playing this low IQ game. Their father is getting bored with their quarrel, so he invented a new game: His father painted N "dots" on paper ", use n-1 "edges" to connect the N "points" (in fact, this is a tree ). And each "edge" has a number. Then, Cong and Coco will select a vertex (Of course they won't see this tree when selecting a vertex). If the number on all sides between the two vertices is exactly a multiple of 3, cong wins, otherwise Coco wins. Cong is very fond of thinking about the problem. After each game, he will carefully study this tree and want to know the probability of winning this picture. Please help us find this value to verify that Cong's answer is correct.

Input

The input row 1st contains a positive integer n. In the n-1 row, each row has three integers x, y, and W, indicating that there is an edge between x and y, and the number above is W.

Output

This probability is output in the form of an approximate score (that is, in the form of "A/B", where A and B must be mutually qualitative. If the probability is 1, "1/1" is output ").

Sample input5
1 2 1
1 3 2
1 4 1
2 5 3
Sample output13/25
[Example]
Group 13 points are) (5, 3) (5, 5 ).

[Data scale]
For 100% of data, n <= 20000.
Hint Source

Tree governance

Problem: Divide and conquer points, which is similar to the Tree of poj. It is better to divide the number of statistical paths with vertices. Code:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 20000+10014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26     int x=0,f=1;char ch=getchar();27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29     return x*f;30 }31 struct edge{int go,next,w;}e[2*maxn];32 int tot,sum,k,root,head[maxn],s[maxn],f[maxn],deep[maxn],d[maxn];33 bool vis[maxn];34 ll ans=0,n,cnt[3];35 inline void insert(int x,int y,int z)36 {37     e[++tot].go=y;e[tot].next=head[x];e[tot].w=z;head[x]=tot;38     e[++tot].go=x;e[tot].next=head[y];e[tot].w=z;head[y]=tot;39 }40 void getroot(int x,int fa)41 {42     f[x]=-inf;s[x]=1;43     for(int i=head[x],y;i;i=e[i].next)44      if(!vis[y=e[i].go]&&y!=fa)45       {46         getroot(y,x);47         s[x]+=s[y];48         f[x]=max(f[x],s[y]);49       }50     f[x]=max(f[x],sum-s[x]);51     if(f[x]<f[root])root=x;52 }53 void getdeep(int x,int fa)54 {55     deep[++deep[0]]=d[x];56     for(int i=head[x],y;i;i=e[i].next)57      if(!vis[y=e[i].go]&&y!=fa)58       {59           d[y]=d[x]+e[i].w;60           getdeep(y,x);61       }62 }63 ll calc(int x,int now)64 {65     deep[0]=0;d[x]=now;66     getdeep(x,0);67     cnt[0]=cnt[1]=cnt[2]=0;68     for1(i,deep[0])cnt[deep[i]%3]++;69     return cnt[1]*cnt[2]*2+cnt[0]*cnt[0];70 }71 void work(int x)72 {73     vis[x]=1;74     ans+=calc(x,0);75     for(int i=head[x],y;i;i=e[i].next)76      if(!vis[y=e[i].go])77       {78           ans-=calc(y,e[i].w);79           sum=s[y];80           root=0;81           getroot(y,x);82           work(root);83       }84 }85 inline ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}86 int main()87 {88     freopen("input.txt","r",stdin);89     freopen("output.txt","w",stdout);90     n=read();91     for1(i,n-1){int x=read(),y=read(),z=read();insert(x,y,z);} 92     root=0;sum=n;93     f[0]=inf;94     getroot(1,0);95     work(root);96     ll tmp=n*n,t=gcd(ans,tmp);97     printf("%lld/%lld\n",ans/t,tmp/t);98     return 0;99 }
View code

 

Bzoj2152: Cong Coco

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.