BZOJ 2152 Cong Coco (tree division), bzoj2152

Source: Internet
Author: User

BZOJ 2152 Cong Coco (tree division), bzoj2152

Address: BZOJ 2152
Find the number of vertices with the sum of three. The simplest point grouping.
The Code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=20000+10;int head[MAXN], cnt, root, ans, min1;int siz[MAXN], ha[4], dep[MAXN], vis[MAXN];struct node{        int v, w, next;}edge[MAXN<<1];void add(int u, int v, int w){        edge[cnt].v=v;        edge[cnt].w=w;        edge[cnt].next=head[u];        head[u]=cnt++;}void init(){        memset(head,-1,sizeof(head));        cnt=ans=0;        memset(vis,0,sizeof(vis));}void getroot(int u, int fa, int s){        siz[u]=1;        int i, max1=0;        for(i=head[u];i!=-1;i=edge[i].next){                int v=edge[i].v;                if(v==fa||vis[v]) continue ;                getroot(v,u,s);                siz[u]+=siz[v];                max1=max(max1,siz[v]);        }        max1=max(max1,s-siz[u]);        if(min1>max1){                root=u;                min1=max1;        }}void getdep(int u, int fa){        ha[dep[u]%3]++;        siz[u]=1;        for(int i=head[u];i!=-1;i=edge[i].next){                int v=edge[i].v;                if(v==fa||vis[v])continue ;                dep[v]=dep[u]+edge[i].w;                getdep(v,u);                siz[u]+=siz[v];        }}int Cal(int u, int len){        dep[u]=len;        ha[0]=ha[1]=ha[2]=0;        getdep(u,-1);        return ha[0]*ha[0]+ha[1]*ha[2]*2;}void work(int u){        vis[u]=1;        ans+=Cal(u,0);        for(int i=head[u];i!=-1;i=edge[i].next){                int v=edge[i].v;                if(vis[v]) continue ;                ans-=Cal(v,edge[i].w);                min1=INF;                getroot(v,-1,siz[v]);                work(root);        }}int gcd(int x, int y){        return x==0?y:gcd(y%x,x);}int main(){        int n, u, v, w, i, j, _gcd;        while(scanf("%d",&n)!=EOF){                init();                for(i=1;i<n;i++){                        scanf("%d%d%d",&u,&v,&w);                        add(u,v,w%3);                        add(v,u,w%3);                }                min1=INF;                getroot(1,-1,n);                work(root);                _gcd=gcd(ans,n*n);                printf("%d/%d\n",ans/_gcd,n*n/_gcd);        }        return 0;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.