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/1c7qmsA 密码:2es8
- java校验身份证号码
/** * 18位身份证校验,粗略的校验 * @author lyl * @param idCard * @return */ public static boolean is18ByteIdCard ...
- Ubuntu 17.10 用 apt 搭建 lamp 环境(精简版)
这篇文章主要用来快速部署以 php 5.6 为主的 lamp 环境,要看详细安装包括虚拟主机配置的请参考这篇:http://www.cnblogs.com/mingc/p/7864030.html 一 ...
- Qualcom QMI系列-基本知识介绍(转)
1 引言1.1 编写目的 介绍Qualcom QMI 基本知识,API使用,设计原理,基于QMI的RemoteEfs(NV)分析1.2 阅读建议 高通平台入门1.3 参考资料 ...
- linux device tree源代码解析--转
//Based on Linux v3.14 source code Linux设备树机制(Device Tree) 一.描述 ARM Device Tree起源于OpenFirmware (OF), ...
- awk正则匹配nginx日志【原创】
查看网页访问代码不为200和30x所有行的内容 awk '{if($9!~/200|30*/) print $0}' /app/logs/http_access.log 或 awk '$9!~/200 ...
- 用C++写程序的一些感悟
前言 近期使用C++有了一些心得很感悟,这里整理一下. 心得1 如果只会使用LabVIEW写程序,还想要进一步深入程序设计,一定要学习一门文本语言. 什么是会用LabVIEW 会用是个比较笼统的概念. ...
- 4.rabbitmq 路由
1. 生产者 #coding:utf8 import pika import json import sys severity = sys.argv[1] if len(sys.argv) > ...
- java图片转byte转string
第一种:原始乱码: public static void main(String[] args) throws IOException { File imgFile = new File(" ...
- Codefroces 735D Taxes(哥德巴赫猜想)
题目链接:http://codeforces.com/problemset/problem/735/D 题目大意:给一个n,n可以被分解成n1+n2+n3+....nk(1=<k<=n). ...