2152: 聪聪可可

 

Description

聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃、两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已经玩儿腻了这种低智商的游戏。他们的爸爸快被他们的争吵烦死了,所以他发明了一个新游戏:由爸爸在纸上画n个“点”,并用n-1条“边”把这n个“点”恰好连通(其实这就是一棵树)。并且每条“边”上都有一个数。接下来由聪聪和可可分别随即选一个点(当然他们选点时是看不到这棵树的),如果两个点之间所有边上数的和加起来恰好是3的倍数,则判聪聪赢,否则可可赢。聪聪非常爱思考问题,在每次游戏后都会仔细研究这棵树,希望知道对于这张图自己的获胜概率是多少。现请你帮忙求出这个值以验证聪聪的答案是否正确。

Input

输入的第1行包含1个正整数n。后面n-1行,每行3个整数x、y、w,表示x号点和y号点之间有一条边,上面的数是w。

Output

以即约分数形式输出这个概率(即“a/b”的形式,其中a和b必须互质。如果概率为1,输出“1/1”)。

Sample Input

5
1 2 1
1 3 2
1 4 1
2 5 3

Sample Output

13/25
【样例说明】
13组点对分别是(1,1) (2,2) (2,3) (2,5) (3,2) (3,3) (3,4) (3,5) (4,3) (4,4) (5,2) (5,3) (5,5)。

【数据规模】
对于100%的数据,n<=20000。

HINT

 

Source

 

点分治好厉害QAQ

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<algorithm>
 7 #include<string>
 8 #include<map>
 9 #include<queue>
10 #include<vector>
11 #include<set>
12 #define inf 1000000000
13 #define maxn 20000+5
14 #define maxm 20000+5
15 #define eps 1e-10
16 #define ll long long
17 #define for0(i,n) for(int i=0;i<=(n);i++)
18 #define for1(i,n) for(int i=1;i<=(n);i++)
19 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
20 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
22 using namespace std;
23 int read(){
24     int x=0,f=1;char ch=getchar();
25     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
26     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
27     return x*f;
28 }
29 struct edge{
30     int go,next,w;
31 }e[2*maxm];
32 int tot,sum,k,root,head[maxn],s[maxn],f[maxn],deep[maxn],d[maxn];
33 bool vis[maxn];
34 ll ans,n,cnt[3];
35 void insert(int u,int v,int w){
36     e[++tot]=(edge){v,head[u],w};head[u]=tot;
37     e[++tot]=(edge){u,head[v],w};head[v]=tot;
38 }
39 void getroot(int x,int fa){
40     f[x]=-inf;s[x]=1;
41     for4(i,x)
42         if(y!=fa&&!vis[y]){
43             getroot(y,x);
44             s[x]+=s[y];
45             f[x]=max(f[x],s[y]);
46         }
47     f[x]=max(f[x],sum-s[x]);
48     if(f[x]<f[root])root=x;
49 }
50 void getdeep(int x,int fa){
51     deep[++deep[0]]=d[x];
52     for4(i,x)
53         if(!vis[y]&&y!=fa){
54             d[y]=d[x]+e[i].w;
55             getdeep(y,x);
56         }
57 }
58 ll calc(int x,int now){
59     deep[0]=0;d[x]=now;
60     getdeep(x,0);
61     cnt[0]=cnt[1]=cnt[2]=0;
62     for1(i,deep[0])cnt[deep[i]%3]++;
63     return cnt[1]*cnt[2]*2+cnt[0]*cnt[0];
64 }
65 void work(int x){
66     vis[x]=1;
67     ans+=calc(x,0);
68     for4(i,x)
69         if(!vis[y]){
70             ans-=calc(y,e[i].w);
71             sum=s[y];
72             root=0;
73             getroot(y,x);
74             work(root);
75         }
76 }
77 ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
78 int main(){
79     //freopen("input.txt","r",stdin);
80     //freopen("output.txt","w",stdout);
81     n=read();
82     for1(i,n-1){
83         int x=read(),y=read(),z=read();
84         insert(x,y,z);
85     }
86     root=0;sum=n;f[0]=inf;
87     getroot(1,0);
88     work(root);
89     ll tmp=n*n,tmp2=gcd(ans,tmp);
90     printf("%lld/%lld\n",ans/tmp2,tmp/tmp2);
91     return 0;
92 }
View Code

 

posted @ 2016-06-19 19:52  HTWX  阅读(94)  评论(0编辑  收藏  举报