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的倍数的概率是多少 [题解] 树 ...
随机推荐
- MySQL源码中的String
适用于离开作用域就销毁的字符串.
- 使用PSSH批量操作Linux服务器
简介 服务器多了,有一个问题就是如何批量快速操作多台服务器,在网上搜到了PSSH工具,试用了一下发现挺好用,推荐给大家. pssh是一个python编写的可以在多台服务器上执行命令的轻量级管理工具,同 ...
- Kotlin 0
#0 下载与配置: 官网上写的很详细,不再重复. #1 Hello world fun main(args: Array<String>) { println("Hello, w ...
- C++学习014函数值传递和地址传递
当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数 区别就是一个本身不被改变,而另一本身也在改变, 在开发时候都会用到, 这里做下记录 #include <iostre ...
- resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)
#!/usr/bin/python import os import json import subprocess from cloudinit.sources.DataSourceConfigDri ...
- AcCoder Contest-115 D - Christmas
D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...
- LeetCode 全解(bug free 训练)
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- 用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1
package com.ljn.base; /** * @author lijinnan * @date:2013-9-12 上午9:55:32 */ public class IncDecThrea ...
- android命令模式IntentService 远程下载文件
服务可用在一下情景: 1,用户离开activity后,仍需要继续工作,例如从网络下载文件,播放音乐. 2,无论activity出现或离开,都需要持续工作,例如网络聊天应用. 3,连接网络服务,正在使用 ...