bzoj 2152
/**************************************************************
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的更多相关文章
- bzoj 2152聪聪可可
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰 ...
- 点分治练习——BZOJ 2152
做的第二道点分治的题目,比较裸,算是模板题吧(感觉比之前那题还简单点. 题目:BZOJ 2152 聪聪可可 题目大意:给出一棵树,求树上两点间长度为3的倍数(0也算)的路径数. 解题思路: 基本和PO ...
- BZOJ 2152 & 点分治
Description: 只是打法法塔前测试一下板子 Code: /*================================= # Created time: 2016-04-20 14:3 ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- BZOJ 2152: 聪聪可可 点分治
2152: 聪聪可可 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...
- bzoj 2152: 聪聪可可 树的点分治
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 485 Solved: 251[Submit][Status] Descripti ...
- 洛谷 2634&&BZOJ 2152: 聪聪可可【点分治学习+超详细注释】
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 3435 Solved: 1776[Submit][Status][Discuss ...
- BZOJ 2152 聪聪可可(点分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2152 [题目大意] 给出一棵树,问任取两点之间距离为3的倍数的概率是多少 [题解] 树 ...
- [国家集训队][bzoj 2152] 聪聪可可 [点分治]
题面: http://www.lydsy.com/JudgeOnline/problem.php?id=2152 思路: 题目要求统计书上路径信息,想到树上分治算法 实际上这是一道点分治裸题,我就不瞎 ...
随机推荐
- Caffe提取任意层特征并进行可视化
现在Caffe的Matlab接口 (matcaffe3) 和python接口都非常强大, 可以直接提取任意层的feature map以及parameters, 所以本文仅仅作为参考, 更多最新的信息请 ...
- html 中的列表
html 中列表可以分为 1. 无序列表(ul--li 的形式) 2. 有序列表(ol li的形式) 3. 定义列表(dl 的形式) 下面来看几种列表的具体内容: 1.无序列表. 无序列表的格式 ...
- hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS ( ...
- windows+python3.6下安装fasttext+fasttext在win上的使用+gensim(fasttext)
真是坑了好久,faxttext对win并不是很友好,所以遇到了很多坑,记录下来,以供大家少走弯路. 法1:刚开始直接用pip install fasttext,最后一直报下面这个错误 “error:M ...
- JSON.parse()——json字符串转JS
JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScript 对象. 语法 JSON.parse(text[, ...
- PHP深浅拷贝
举个栗子: <?php class Example1 { public $name; public function __construct($name) { $this->name = ...
- webconfig的配置解析
<?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...
- Linux实用命令之xdg-open
为什么要介绍 xdg-open 呢,得先从需求说起. 一般在控制台中,可以使用命令操作各式文本文件.但难以避免,需要操作一些非文本文件,如 pdf,doc 等. 此时,一般的做法是,打开文件管理器,再 ...
- SQL 变量 条件查询 插入数据
(本文只是总结网络上的教程) 在操作数据库时 SQL语句中难免会用到变量 比如 在條件值已知的情況下 INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值 ...
- react native系列 - 从零开始构建
从零开始构建第一步,当然是从hello world开始,第一课我们没什么代码都不写,只用生成的代码来打包apk.为什么一开始就要学会打包,因为如果连打包都不会,以后做好了也没用.学会了打包,才能让我们 ...