zoukankan      html  css  js  c++  java
  • bzoj 2152

      1 /**************************************************************
      2     Problem: 2152
      3     User: idy002
      4     Language: C++
      5     Result: Accepted
      6     Time:500 ms
      7     Memory:2612 kb
      8 ****************************************************************/
      9  
     10 #include <cstdio>
     11 #define N 20010
     12  
     13 typedef long long dnt;
     14  
     15 int n;
     16 int head[N], dest[N+N], wght[N+N], next[N+N], ntot;
     17 int fat[N], vis[N], siz[N], bac[N], dis[N], siz_tot, cur_root;
     18 int vv[3], vtot;
     19 dnt path_tot, path_ans;
     20  
     21 void insert( int u, int v, int w ) {
     22     ntot++;
     23     next[ntot] = head[u];
     24     wght[ntot] = w;
     25     dest[ntot] = v;
     26     head[u] = ntot;
     27 }
     28  
     29 void dfs_root( int u ) {
     30     siz[u] = 1, bac[u] = 0;
     31     for( int t=head[u]; t; t=next[t] ) {
     32         int v=dest[t];
     33         if( vis[v] || v==fat[u] ) continue;
     34         fat[v] = u;
     35         dfs_root(v);
     36         siz[u]+=siz[v];
     37         if( siz[v]>bac[u] ) bac[u]=siz[v];
     38     }
     39     if( siz_tot-siz[u]>bac[u] ) bac[u]=siz_tot-siz[u];
     40     if( bac[cur_root]>bac[u] ) cur_root=u;
     41 }
     42 void dfs_read( int u ) {
     43     int a = (3-dis[u]%3)%3;
     44     path_ans += vv[a];
     45     path_tot += vtot;
     46     for( int t=head[u]; t; t=next[t] ) {
     47         int v=dest[t], w=wght[t];
     48         if( vis[v] || v==fat[u] ) continue;
     49         dis[v]=dis[u]+w;
     50         fat[v]=u;
     51         dfs_read( v );
     52     }
     53 }
     54 void dfs_write( int u ) {
     55     int a = dis[u]%3;
     56     vv[a]++;
     57     vtot++;
     58     for( int t=head[u]; t; t=next[t] ) {
     59         int v=dest[t];
     60         if( vis[v] || v==fat[u] ) continue;
     61         dfs_write(v);
     62     }
     63 }
     64 void bfs( int rt ) {
     65     siz_tot = siz[rt];
     66     cur_root = 0;
     67     fat[rt] = rt;
     68     dfs_root(rt);
     69      
     70     rt=cur_root;
     71     vis[rt] = true;
     72     vv[0] = 1;
     73     vtot = 1;
     74     for( int t=head[rt]; t; t=next[t] ) {
     75         int v=dest[t], w=wght[t];
     76         if( vis[v] ) continue;
     77         dis[v] = w;
     78         fat[v] = rt;
     79         dfs_read(v);
     80         dfs_write(v);
     81     }
     82     vv[0] = vv[1] = vv[2] = 0;
     83     vtot = 0;
     84     for( int t=head[rt]; t; t=next[t] ) {
     85         int v=dest[t];
     86         if( vis[v] ) continue;
     87         bfs(v);
     88     }
     89 }
     90 void build_vdcp() {
     91     bac[0] = n;
     92     siz[1] = n;
     93     bfs( 1 );
     94 }
     95 dnt gcd( dnt a, dnt b ) {
     96     return b ? gcd(b,a%b) : a;
     97 }
     98  
     99 int main() {
    100     scanf( "%d", &n );
    101     for( int i=1,u,v,w; i<n; i++ ) {
    102         scanf( "%d%d%d", &u, &v, &w );
    103         insert( u, v, w );
    104         insert( v, u, w );
    105     }
    106     build_vdcp();
    107     path_tot *= 2;
    108     path_ans *= 2;
    109     path_tot += n;
    110     path_ans += n;
    111     dnt cd = gcd(path_tot,path_ans);
    112     printf( "%lld/%lld
    ", path_ans/cd, path_tot/cd );
    113 }
    View Code
  • 相关阅读:
    POJ3159 Candies —— 差分约束 spfa
    POJ1511 Invitation Cards —— 最短路spfa
    POJ1860 Currency Exchange —— spfa求正环
    POJ3259 Wormholes —— spfa求负环
    POJ3660 Cow Contest —— Floyd 传递闭包
    POJ3268 Silver Cow Party —— 最短路
    POJ1797 Heavy Transportation —— 最短路变形
    POJ2253 Frogger —— 最短路变形
    POJ1759 Garland —— 二分
    POJ3685 Matrix —— 二分
  • 原文地址:https://www.cnblogs.com/idy002/p/4374242.html
Copyright © 2011-2022 走看看