BZOJ 2152 & 点分治
Description:
只是打法法塔前测试一下板子
Code:
/*=================================
# Created time: 2016-04-20 14:35
# Filename: 2152.cpp
# Description:
=================================*/
#define me AcrossTheSky&HalfSummer11
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define Abs(x) ((x) > 0 ? (x) : (-(x)))
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 100005
#define maxm 100005
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num){
num = 0; bool f = true;char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = false;ch = getchar();}
while(ch >= '0' && ch <= '9') {num = num * 10 + ch - '0';ch = getchar();}
num = f ? num: -num;
}
int outs[100];
template<class T> inline
void write(T x){
if (x==0) {putchar('0'); putchar(' '); return;}
if (x<0) {putchar('-'); x=-x;}
int num=0;
while (x){ outs[num++]=(x%10); x=x/10;}
FORM(i,num-1,0) putchar(outs[i]+'0');
}
/*==================split line==================*/
int n,m,mn,root,cnt,ans,sume=0;
bool vis[maxn];
struct Edge{int to,next,w;} e[maxm];
int first[maxn],sz[maxn],dep[maxn],d[maxn];
inline int gcd(int a,int b) { return b==0?a:gcd(b,a%b);}
inline void addedge(int x,int y,int w){
sume++; e[sume].to=y; e[sume].w=w;e[sume].next=first[x]; first[x]=sume;
}
inline int getroot(int x,int fa,int sum){
sz[x]=1; int mx=0,y; //`vis[x]=true;
for (int i = first[x];i;i=e[i].next)
if (!vis[y=e[i].to] && e[i].to!=fa){
getroot(y,x,sum);
sz[x]+=sz[y];
mx=max(mx,sz[y]);
}
mx=max(mx,sum-mx);
if (mx<mn) mn=mx,root=x;
}
inline void getdep(int x,int fa){
dep[++cnt]=d[x]; int y;
for (int i=first[x];i;i=e[i].next)
if (!vis[y=e[i].to] && e[i].to!=fa){
d[y]=d[x]+e[i].w;
getdep(y,x);
}
}
inline int cal(int x,int last=0){
int c[3]; memset(c,0,sizeof(c));
cnt=0; d[x]=last;
getdep(x,-1);
FORP(i,1,cnt) c[dep[i]%3]++;
return c[0]*c[0]+(c[1]*c[2])*2;
}
inline void dfs(int x,int sum){
vis[x]=true;
ans+=cal(x); int y;
for (int i=first[x];i;i=e[i].next) if (!vis[y=e[i].to]){
ans-=cal(y,e[i].w);
int s=sz[y]>sz[x]?sum-sz[x]:sz[y];
root=0; mn=INF; getroot(y,x,s);
dfs(root,s);
}
}
int main(){
read(n);
FORP(i,1,n-1) {
int x,y,z; read(x); read(y); read(z);
addedge(x,y,z%3); addedge(y,x,z%3);
}
memset(vis,false,sizeof(vis));
mn=INF;
getroot((n+1)>>1,-1,n);
dfs(root,n);
int down=n*n,t=gcd(ans,down);
write(ans/t); putchar('/'); write(down/t); puts("");
}
BZOJ 2152 & 点分治的更多相关文章
- 点分治练习——BZOJ 2152
做的第二道点分治的题目,比较裸,算是模板题吧(感觉比之前那题还简单点. 题目:BZOJ 2152 聪聪可可 题目大意:给出一棵树,求树上两点间长度为3的倍数(0也算)的路径数. 解题思路: 基本和PO ...
- bzoj 2152聪聪可可
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰 ...
- 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 思路: 题目要求统计书上路径信息,想到树上分治算法 实际上这是一道点分治裸题,我就不瞎 ...
- bzoj 2152 聪聪可可(点分治模板)
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 3194 Solved: 1647[Submit][Status][Discuss ...
随机推荐
- ios显示一个下载banner
<meta name="apple-itunes-app" content="app-id=432274380" /> 这个标签是告诉iphone的 ...
- 疯狂了!当游戏爱上MongoDB会怎么样???
导读 前端时间魔兽这个电影我相信大家都看过了哈,作为一个码农,有时候我也会去思考魔兽世界这个游戏背后他的一些设计和实现,比如他用什么数据库.当然真正用什么数据库这个我是不确定的,我们今天的主题是当游戏 ...
- MVC开发模式下的用户角色权限控制
前提: MVC开发模式 大概思想: 1.在MVC开发模式下,每个功能都对应着不同的控制器或操作方法名(如修改密码功能可能对应着User/changepd),把每个功能对应的控制器名和操作方法名存到数据 ...
- IIS网站或系统验证码不显示问题——"使用了托管的处理程序,但是未安装或未完整安装 ASP.NET"
在IIS上发布了一个系统,但是登陆页面的验证码图片一直出不来,尝试了各种办法,权限.路径.继承父类路径等都不管用,进入Login.html,对着无验证码图片的图标,右键复制图片的网址,粘贴到地址栏,出 ...
- LCQCL
Linux Command Quick Check List 记录一些我Linux使用中的问题的解决方案. # 挂起: 几乎只对RAM供电以保存工作状态的极低能耗状态. 任意键或电源键退出(主要看RP ...
- 33个超级有用必须要收藏的PHP代码样例
作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...
- HTML DOM Event 对象
var event;if (document.createEvent){event = document.createEvent("HTMLEvents");event.initE ...
- 在WebPart中获取Office 365中的未读邮件数
// Create the web request HttpWebRequest request = WebRequest.Create("https://outlook.office365 ...
- Ubuntu实用小指令
1.Ubuntu命令行下打开图形界面的文件夹 可以使用 nautilus path 为使用方便,可以给命令nautilus加一个别名cd /home/usernamevi .bash_aliases在 ...
- ASP.NET获取百度地图提供的API接口里面的JSON
思路:开始是想直接在前台获取,但是跨域访问还是有点难度,而且格式必须是josnp格式的,最后嫌麻烦,不得已放弃. 我做的ASP.NET 而这个有自带的解析类,直接引用就行了 先在后台获取到JOSN: ...