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 思路: 题目要求统计书上路径信息,想到树上分治算法 实际上这是一道点分治裸题,我就不瞎 ...
随机推荐
- 蓝色的企业后台cms管理系统——后台
链接:http://pan.baidu.com/s/1kViBtTt 密码:7hbk
- Msql中的触发器
解发器 当执行某种操作时解发的行为. 比如, 当表变动时触发的动作. 像商城订单, 当下单时, 库存减少. 语法: create trigger trigger_name after/befor in ...
- python并发编程之asyncio协程(三)
协程实现了在单线程下的并发,每个协程共享线程的几乎所有的资源,除了协程自己私有的上下文栈:协程的切换属于程序级别的切换,对于操作系统来说是无感知的,因此切换速度更快.开销更小.效率更高,在有多IO操作 ...
- HZ与Jiffies
2.4 内核定时器 内核中许多部分的工作都高度依赖于时间信息.Linux内核利用硬件提供的不同的定时器以支持忙等待或睡眠等待等时间相关的服务.忙等待时,CPU 会不断运转.但是睡眠等待时,进程将放弃C ...
- 一个python爬虫工具类
写了一个爬虫工具类. # -*- coding: utf-8 -*- # @Time : 2018/8/7 16:29 # @Author : cxa # @File : utils.py # @So ...
- 15个你不得不知道的Chrome dev tools的小技巧
转载自:https://www.imooc.com/article/2559 谷歌浏览器如今是Web开发者们所使用的最流行的网页浏览器.伴随每六个星期一次的发布周期和不断扩大的强大的开发功能,Chro ...
- 当array_filter函数的callback留空时 他会过滤掉所有键值为false的键
当array_filter函数的callback留空时 他会过滤掉所有键值为false的键
- nginx解析带中文的url重定向之后404问题
首先,有这样一个需求:一个系统的图片存储是放在upyun上的,现在有个客户需要本地化部署,所以需要将图片进行本地存储.为了兼容,图片存储的路径格式保持和在upyun上的存储路径一致.在upyun上的存 ...
- java基础11 继承(super、extends关键字和重写,这三个要素出现的前提:必须存在继承关系)
面向对象的三大特征: 1.封装 (将一类属性封装起来,并提供set()和get()方法给其他对象设置和获取值.或者是将一个运算方法封装起来,其他对象需要此种做运算时,给此对象调用) 2.继承 ...
- Codefroces 919D Substring(拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/919/D 题目大意:给你一张有向图,给你每个顶点上的字母和一些边,让你找出一条路径,路径上的相同字母数最多 ...