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 思路: 题目要求统计书上路径信息,想到树上分治算法 实际上这是一道点分治裸题,我就不瞎 ...
随机推荐
- Masquerade strikes back Gym - 101911D(补题) 数学
https://vjudge.net/problem/Gym-101911D 具体思路: 对于每一个数,假设当前的数是10 分解 4次,首先 1 10 这是一对,然后下一次就记录 10 1,这样的话直 ...
- arch优化开机
查看开机时间 systemd-analyze 具体开机时间 systemd-analyze blame 你可以systemctl --all | grep not-found 查看有哪些服务挂掉了.然 ...
- Where can I check in?
Where can I check in? 在哪儿办理登记手续?
- Linux下搜索命令
linux下用于查找文件的5个命令,有需要的朋友可以参考下.包括find,whereis,locate,which与type. linux下用于查找文件的5个命令,有需要的朋友可以参考下.包括find ...
- 基于scrapy的分布式爬虫抓取新浪微博个人信息和微博内容存入MySQL
为了学习机器学习深度学习和文本挖掘方面的知识,需要获取一定的数据,新浪微博的大量数据可以作为此次研究历程的对象 一.环境准备 python 2.7 scrapy框架的部署(可以查看上一篇博客的简 ...
- 设计模式之笔记--职责链模式(Chain of Responsibility)
职责链模式(Chain of Responsibility) 定义 职责链模式(Chain of Responsibility),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系 ...
- 域名 DNS命令——dig
dig命令详解 1.查看域名的A记录 # dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.e ...
- HDU 2822
Dogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 5505(数论-gcd的应用)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Go语言入门之切片的概念
切片是对数组的抽象,对切片的改变会改变原数组的值 package main import "fmt" func test6(){ arr:=[...],,,,,,,,,,} s1: ...