bzoj 2152 点剖分
比较裸的点剖分,访问到每个重心时,记录一个b数组,
代表当前子树中长度为i的边的数量是b[i],因为是3的倍数,
所以边长mod 3保存就行了,然后记录一个sum数组,代表
当前子树中一个子节点为根的子树中的情况(类似b),然后
用这两个数组不断的更新答案就行了。
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/
//By BLADEVIL
var
n :longint;
pre, other, len :array[..] of longint;
last :array[..] of longint;
l, top :longint;
size, stack, yy :array[..] of longint;
ff :array[..] of boolean;
root :longint;
b, sum :array[..] of longint;
ans :longint;
function gcd(x,y:longint):longint;
begin
if x<y then exit(gcd(y,x)) else
if y= then exit(x) else exit(gcd(y,x mod y));
end;
procedure connect(x,y,z:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
len[l]:=z;
end;
procedure dfs_size(x,fa:longint);
var
q, p :longint;
begin
size[x]:=;
inc(top);
stack[top]:=x;
q:=last[x];
while q<> do
begin
p:=other[q];
if (ff[q]) or (p=fa) then
begin
q:=pre[q];
continue;
end;
dfs_size(p,x);
inc(size[x],size[p]);
q:=pre[q];
end;
yy[x]:=fa;
end;
procedure getroot(u:longint);
var
ms, s, x, p, q :longint;
i :longint;
begin
top:=;
dfs_size(u,);
ms:=maxlongint;
for i:= to top do
begin
x:=stack[i];
s:=size[u]-size[x];
q:=last[x];
while q<> do
begin
p:=other[q];
if (ff[q]) or (p=yy[x]) then
begin
q:=pre[q];
continue;
end;
if size[p]>s then s:=size[p];
q:=pre[q];
end;
if s<ms then
begin
ms:=s;
root:=x;
end;
end;
end;
procedure dfs_value(x,fa,lz:longint);
var
q, p :longint;
begin
inc(sum[lz mod ]);
q:=last[x];
while q<> do
begin
p:=other[q];
if (p=fa) or (ff[q]) then
begin
q:=pre[q];
continue;
end;
dfs_value(p,x,lz+len[q]);
q:=pre[q];
end;
end;
procedure solve(u:longint);
var
i, q, p :longint;
begin
getroot(u);
if top= then exit;
fillchar(b,sizeof(b),);
b[]:=;
top:=;
q:=last[root];
while q<> do
begin
p:=other[q];
if ff[q] then
begin
q:=pre[q];
continue;
end;
fillchar(sum,sizeof(sum),);
dfs_value(p,root,len[q]);
for i:= to do ans:=ans+b[i]*sum[-i];
ans:=ans+sum[]*b[];
for i:= to do inc(b[i],sum[i]);
q:=pre[q];
end;
q:=last[root];
while q<> do
begin
p:=other[q];
if ff[q] then
begin
q:=pre[q];
continue;
end;
ff[q]:=true;
ff[q xor ]:=true;
solve(p);
q:=pre[q];
end;
end;
procedure main;
var
i :longint;
x, y, z :longint;
ans1, ans2 :longint;
g :longint;
begin
read(n);
l:=;
fillchar(b,sizeof(b),$ff);
b[]:=;
for i:= to n- do
begin
read(x,y,z);
z:=z mod ;
connect(x,y,z);
connect(y,x,z);
end;
ans:=;
solve();
ans1:=*ans+n; ans2:=n*n;
g:=gcd(ans1,ans2);
writeln(ans1 div g,'/',ans2 div g);
end;
begin
main;
end.
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
/************************************************************** Problem: 2152 User: idy002 Language: ...
- BZOJ 2152 聪聪可可(点分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2152 [题目大意] 给出一棵树,问任取两点之间距离为3的倍数的概率是多少 [题解] 树 ...
随机推荐
- OpenCV入门:(四:混合两张图片)
1. 原理 对两张图片使用如下公式可以得到两张图片的混合图片, 其中f0(x),f1(x)分别是图片1和图片2同一位置的像素点. 2. OpenCV中的AddWeight函数 函数和参数说明: ) s ...
- 「暑期训练」「基础DP」 Monkey and Banana (HDU-1069)
题意与分析 给定立方体(个数不限),求最多能堆叠(堆叠要求上方的方块严格小于下方方块)的高度. 表面上个数不限,问题是堆叠的要求决定了每个方块最多可以使用三次.然后就是对3n" role=& ...
- Viewer.js 图片预览插件使用
一.简介 Viewer.js 是一款强大的图片查看器. Viewer.js 有以下特点: 支持移动设备触摸事件 支持响应式 支持放大/缩小 支持旋转(类似微博的图片旋转) 支持水平/垂直翻转 支持图片 ...
- 【LoadRunner】解决LR11无法录制Chrome浏览器脚本问题
LoadRunner录制脚本时,遇到高版本的IE.FireFox,或者Chrome浏览器,会出现无法录制脚本的问题,下面就来讲一下如何利用LR自带的wplus_init_wsock.exe插件进行脚本 ...
- 局部敏感哈希LSH
之前介绍了Annoy,Annoy是一种高维空间寻找近似最近邻的算法(ANN)的一种,接下来再讨论一种ANN算法,LSH局部敏感哈希. LSH的基本思想是: 原始空间中相邻的数据点通过映射或投影变换后, ...
- Pro Git - 笔记3
Git Branching Branches in a Nutshell Branches in a Nutshell let’s assume that you have a directory c ...
- WEBSTORM中html文件运行之后出现乱码的问题解决
出现如下问题: 解决方案: 1.点击"文件编码" 2.选择GBK 3.点击Reload. 4.此时,源代码中的中文字体会变成乱码,把这些乱码重新输入成原先的中文.然后运行html文 ...
- PAT 1084 外观数列
https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120 外观数列是指具有以下特点的整数序列: d, ...
- 算法(11)Find All Duplicates in an Array
题目:数组的长度是n,里面的数是1到n,其中肯定有重复的,找到里面重复的数字 思路:自己想愣是没有想出来,直接看答案,关键点是看nums[i]和nums[nums[i]-1]之间的关系,遍历整个数组, ...
- SRM707 div1 MultiplyAddPuzzle
题目大意:给定4个数,s,t,a,b 每次可以将s加a或者乘b,问最少多少次可以得到t 做法:考虑最后的形式,肯定是s*b^n + a*f(b),f(b)是关于b的多项式 那么b乘多少次实际上是可以知 ...