/**************************************************************
Problem: 2152
User: idy002
Language: C++
Result: Accepted
Time:500 ms
Memory:2612 kb
****************************************************************/ #include <cstdio>
#define N 20010 typedef long long dnt; int n;
int head[N], dest[N+N], wght[N+N], next[N+N], ntot;
int fat[N], vis[N], siz[N], bac[N], dis[N], siz_tot, cur_root;
int vv[], vtot;
dnt path_tot, path_ans; void insert( int u, int v, int w ) {
ntot++;
next[ntot] = head[u];
wght[ntot] = w;
dest[ntot] = v;
head[u] = ntot;
} void dfs_root( int u ) {
siz[u] = , bac[u] = ;
for( int t=head[u]; t; t=next[t] ) {
int v=dest[t];
if( vis[v] || v==fat[u] ) continue;
fat[v] = u;
dfs_root(v);
siz[u]+=siz[v];
if( siz[v]>bac[u] ) bac[u]=siz[v];
}
if( siz_tot-siz[u]>bac[u] ) bac[u]=siz_tot-siz[u];
if( bac[cur_root]>bac[u] ) cur_root=u;
}
void dfs_read( int u ) {
int a = (-dis[u]%)%;
path_ans += vv[a];
path_tot += vtot;
for( int t=head[u]; t; t=next[t] ) {
int v=dest[t], w=wght[t];
if( vis[v] || v==fat[u] ) continue;
dis[v]=dis[u]+w;
fat[v]=u;
dfs_read( v );
}
}
void dfs_write( int u ) {
int a = dis[u]%;
vv[a]++;
vtot++;
for( int t=head[u]; t; t=next[t] ) {
int v=dest[t];
if( vis[v] || v==fat[u] ) continue;
dfs_write(v);
}
}
void bfs( int rt ) {
siz_tot = siz[rt];
cur_root = ;
fat[rt] = rt;
dfs_root(rt); rt=cur_root;
vis[rt] = true;
vv[] = ;
vtot = ;
for( int t=head[rt]; t; t=next[t] ) {
int v=dest[t], w=wght[t];
if( vis[v] ) continue;
dis[v] = w;
fat[v] = rt;
dfs_read(v);
dfs_write(v);
}
vv[] = vv[] = vv[] = ;
vtot = ;
for( int t=head[rt]; t; t=next[t] ) {
int v=dest[t];
if( vis[v] ) continue;
bfs(v);
}
}
void build_vdcp() {
bac[] = n;
siz[] = n;
bfs( );
}
dnt gcd( dnt a, dnt b ) {
return b ? gcd(b,a%b) : a;
} int main() {
scanf( "%d", &n );
for( int i=,u,v,w; i<n; i++ ) {
scanf( "%d%d%d", &u, &v, &w );
insert( u, v, w );
insert( v, u, w );
}
build_vdcp();
path_tot *= ;
path_ans *= ;
path_tot += n;
path_ans += n;
dnt cd = gcd(path_tot,path_ans);
printf( "%lld/%lld\n", path_ans/cd, path_tot/cd );
}

bzoj 2152的更多相关文章

  1. bzoj 2152聪聪可可

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MB Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰 ...

  2. 点分治练习——BZOJ 2152

    做的第二道点分治的题目,比较裸,算是模板题吧(感觉比之前那题还简单点. 题目:BZOJ 2152 聪聪可可 题目大意:给出一棵树,求树上两点间长度为3的倍数(0也算)的路径数. 解题思路: 基本和PO ...

  3. BZOJ 2152 & 点分治

    Description: 只是打法法塔前测试一下板子 Code: /*================================= # Created time: 2016-04-20 14:3 ...

  4. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  5. BZOJ 2152: 聪聪可可 点分治

    2152: 聪聪可可 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...

  6. bzoj 2152: 聪聪可可 树的点分治

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 485  Solved: 251[Submit][Status] Descripti ...

  7. 洛谷 2634&&BZOJ 2152: 聪聪可可【点分治学习+超详细注释】

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 3435  Solved: 1776[Submit][Status][Discuss ...

  8. BZOJ 2152 聪聪可可(点分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2152 [题目大意] 给出一棵树,问任取两点之间距离为3的倍数的概率是多少 [题解] 树 ...

  9. [国家集训队][bzoj 2152] 聪聪可可 [点分治]

    题面: http://www.lydsy.com/JudgeOnline/problem.php?id=2152 思路: 题目要求统计书上路径信息,想到树上分治算法 实际上这是一道点分治裸题,我就不瞎 ...

随机推荐

  1. scrapy爬虫框架介绍

    一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...

  2. HBase笔记之远程Shell界面命令行无法删除字符的解决方案

    方法一: 设置终端退格键为ASCII 127 在XShell的界面中,设置 文件 --> 属性 --> 终端 --> 键盘 --> BACKSPACE键序列,改为ASCII 1 ...

  3. Git HTTPS 方式自动保存用户名密码

    一行命令搞定: git config --global credential.helper wincred 第一次输入用户名和密码提交,第二次就不需要了 参考: https://help.github ...

  4. gpk-update-icon占用CPU及清除【原创】

    发现服务器有个gpk-update-icon一直占用CPU进程 网上查看相关信息比较少. gpk-update-icon是gnome的更新图标进程 俩种处理方法: 1.杀掉gpk-update-ico ...

  5. 【转载】C#异常Retry通用类

    //Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...

  6. nfs服务器配置

    安装 nfs-utils,该软件包提供了 NFS 工具: # yum install nfs-utils 配置启动脚本,使得共享在系统每次启动时都有效: # chkconfig --add rpcbi ...

  7. Qt跨线程调用错误解析及解决办法

    错误提示:Error: Cannot create children for a parent that is in a different thread. 错误案例分析 新建SerialLink子线 ...

  8. LAMP网站架构解释

    对于大流量.大并发量的网站系统架构来说,除了硬件上使用高 性能的服务器.负载均衡.CDN等之外,在软件架构上需要重点关注下面几个环节:使用高性能的操作系统(OS).高性能的网页服务器(Web Serv ...

  9. 9.Python3标准库--数据压缩与归档

    ''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...

  10. python【项目】:工资管理(简易版)

    功能要求: 登录系统用户认证通过后才能列出下一级菜单员工信息表 登录系统要有用户登录.注册账号.删除账号.修改密码.退出 登录密码要有加密功能 从info.txt文件读取员工及工资信息,最后通过增加, ...