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 & 点分治的更多相关文章

  1. 点分治练习——BZOJ 2152

    做的第二道点分治的题目,比较裸,算是模板题吧(感觉比之前那题还简单点. 题目:BZOJ 2152 聪聪可可 题目大意:给出一棵树,求树上两点间长度为3的倍数(0也算)的路径数. 解题思路: 基本和PO ...

  2. bzoj 2152聪聪可可

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MB Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰 ...

  3. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  4. BZOJ 2152: 聪聪可可 点分治

    2152: 聪聪可可 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...

  5. bzoj 2152: 聪聪可可 树的点分治

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 485  Solved: 251[Submit][Status] Descripti ...

  6. 洛谷 2634&&BZOJ 2152: 聪聪可可【点分治学习+超详细注释】

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 3435  Solved: 1776[Submit][Status][Discuss ...

  7. BZOJ 2152 聪聪可可(点分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2152 [题目大意] 给出一棵树,问任取两点之间距离为3的倍数的概率是多少 [题解] 树 ...

  8. [国家集训队][bzoj 2152] 聪聪可可 [点分治]

    题面: http://www.lydsy.com/JudgeOnline/problem.php?id=2152 思路: 题目要求统计书上路径信息,想到树上分治算法 实际上这是一道点分治裸题,我就不瞎 ...

  9. bzoj 2152 聪聪可可(点分治模板)

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 3194  Solved: 1647[Submit][Status][Discuss ...

随机推荐

  1. Yii ActiveRecord 的via和viaTable示例

    Yii中,将两个不相关的表利用中间表关联有via和viaTable两种方法,这里通过用户权限查询来进行示例. 关系如上,需要建立三个表 用户表 权限表 用户表 数据: 权限表 数据: 关联表 数据: ...

  2. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  3. github添加ssh方法(windows版)

    生成一个新的SSH key 打开 git bash 输入  ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 输入一个文件 ...

  4. Bing Test -必应每日壁纸自动换

    今天向大家推荐一个桌面美化类的工具,没错就是自动更换壁纸,而且是精美的必应每日壁纸哦!绿色小巧,开机自启动,设置后每日自动更新你的桌面~ 软件名称:Bing Test 链接: http://pan.b ...

  5. 使用keychain保存用户名和密码等敏感信息 KeychainItemWrapper和SFHFKeychainUtils

    iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式,每个ios程序都有一个独立的keychain存储.相对于 NSUserDefaults.文件保存等一般方式,ke ...

  6. 基于SSM的租赁管理系统0.3_20161225_数据库设计

    数据库设计 1. 概念模型 2. 类模型 3. 生成SQL use test; /*========================================================== ...

  7. mac显示任何来源

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Microsoft YaHei"; color: #333333; ba ...

  8. JAVA程序中SQL语句无法传递中文参数

    vi /etc/my.cnf [mysqld]# The default character set that will be used when a new schema or table is# ...

  9. Linux远程服务器上安装SVN

    前言 SVN服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊,独立SVN服务器不结合Apache安装使用,连接独立SVN服务器也不用HTTP协议.这是比较快捷的SVN服务器配置方法 ...

  10. HTML入门教程 这里可以免费学习啦

    本文目标 30分钟内让你明白HTML是什么,并对它有一些基本的了解.一旦入门后,你可以从网上找到更多更详细的资料来继续学习. 什么是HTML HTML是英文Hyper Text Mark-up Lan ...