2018.07.20 bzoj2152: 聪聪可可(点分治)
传送门
本蒟蒻AC的第二道点分治,调了30min" role="presentation" style="position: relative;">30min30min发现自己把gcd" role="presentation" style="position: relative;">gcdgcd写错了。
这题是一个点分治裸板,记录整颗子树中到根的距离模3" role="presentation" style="position: relative;">33的情况,容斥一下扣去多算的部分就行了。
代码如下:
#include<bits/stdc++.h>
#define N 20005
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
int n,tot,first[N],siz[N],msiz[N],d[N],cnt[3],sum,rt,ans=0;
bool vis[N];
struct Node{int w,v,next;}e[N<<1];
inline void add(int u,int v,int w){e[++tot].v=v,e[tot].w=w,e[tot].next=first[u],first[u]=tot;}
inline void getroot(int p,int fa){
siz[p]=1,msiz[p]=0;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(v==fa||vis[v])continue;
getroot(v,p),siz[p]+=siz[v];
if(siz[v]>msiz[p])msiz[p]=siz[v];
}
msiz[p]=max(msiz[p],sum-siz[p]);
if(msiz[p]<msiz[rt])rt=p;
}
inline void getdis(int p,int fa){
++cnt[d[p]];
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(v==fa||vis[v])continue;
d[v]=(d[p]+e[i].w)%3,getdis(v,p);
}
}
inline int calc(int p,int v){
cnt[0]=cnt[1]=cnt[2]=0,d[p]=v,getdis(p,0);
return cnt[0]*cnt[0]+cnt[1]*cnt[2]*2;
}
inline void solve(int p){
ans+=calc(p,0);
vis[p]=true;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(vis[v])continue;
ans-=calc(v,e[i].w);
rt=0,sum=siz[v];
getroot(v,0);
solve(rt);
}
}
inline int gcd(int a,int b){while(b){int t=a;a=b,b=t%a;}return a;}
int main(){
memset(vis,false,sizeof(vis));
n=read();
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read()%3;
add(u,v,w),add(v,u,w);
}
rt=0,msiz[0]=sum=n,getroot(1,0),solve(rt);
int g=gcd(ans,n*n);
printf("%d/%d",ans/g,n*n/g);
return 0;
}
2018.07.20 bzoj2152: 聪聪可可(点分治)的更多相关文章
- [bzoj2152][聪聪和可可] (点分治+概率)
Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...
- 2018.07.20 洛谷P4178 Tree(点分治)
传送门 又一道点分治. 直接维护子树内到根的所有路径长度,然后排序+双指针统计答案. 代码如下: #include<bits/stdc++.h> #define N 40005 using ...
- 2018.07.20 bzoj1614: Telephone Lines架设电话线(二分+最短路)
传送门 这题直接做显然gg" role="presentation" style="position: relative;">gggg,看这数据 ...
- 2018.07.20 bzoj3211: 花神游历各国(线段树)
传送门 维护区间开方,区间求和.这个是线段树常规操作. 显然一个数被开方若干次之后要么是1,要么是0,所以用线段树维护区间最大和区间和,如果区间最大不超过1就剪枝剪掉,不然就继续递归直到叶节点时停下进 ...
- 2018.07.20 atcoder Largest Smallest Cyclic Shift(贪心)
传送门 题意:给你x个a,y个b,z个c,显然这些字符可以拼成若干字符串,然后求这些字符串中最小表示法表示出来的最大的那一个. 解法:贪心思想,用multiset维护现在拼成的字串,每次取一个最小的和 ...
- 【BZOJ2152】聪聪可可(点分治)
[BZOJ2152]聪聪可可(点分治) 题面 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电 ...
- BZOJ2152 [国家集训队] 聪聪可可 [点分治]
题目传送门 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 5237 Solved: 2750[Submit][Status][Discuss ...
- BZOJ2152 聪聪可可 【点分治】
BZOJ2152 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问 ...
- 【bzoj2152】【聪聪可可】【点分治】
[问题描写叙述] 聪聪和可但是兄弟俩.他们俩常常为了一些琐事打起来,比如家中仅仅剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(但是他们家仅仅有一台电脑)--遇到这样的问题,普通情况下石头剪刀布就好 ...
随机推荐
- cprogram作业
刘金福 SA17225205 第三次作业 url:http://blog.csdn.net/liu896749150/article/details/78176433 学号:SA17225404 姓名 ...
- 【336】Tutorial of Endnote
Now I start to use Endnote to manage my literatures and I need to learn how to use it. Below is some ...
- 原生nodejs 学习笔记2
本章节学习流, 流的一个好处在于减少各种异步IO的回调地狱.IO操作遍及我们各种操作,比如数据库读写,文件读写, 文件转换压缩--别的不说,比如第一节,我们要将一个HTML文件返回浏览器,就涉及IO操 ...
- Hibernate merge和update的区别
今天做了个测试,写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样.实体bean很简单,就id和name两个字段,接下来分别给出以下几种测试情形的控制台日志内容: 1. 数 ...
- LeetCode关于数组中求和的题型
15. 3Sum:三数之和为0的组合 Given an array S of n integers, are there elements a, b, c in S such that a + b + ...
- Python的logging,记录log的包
最近在做自动化测试时,想给他加上日志,所以用到logging的模块,以下是python增加log的几种方式 一.python代码配置方式(当然还有一种是可以多模块通用的一个python代码设置,这个网 ...
- scrollLeft滚动(用animate替代)
原: let checkedLeft1 = $('#dateBox').find('.checked').position().left let checkedLeft2 = $('#dateBox' ...
- Python property() 函数
Python property() 函数 Python 内置函数 描述 property() 函数的作用是在新式类中返回属性值. 语法 以下是 property() 方法的语法: class pro ...
- 55. Jump Game (Array; Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- python之类之多继承
class A(object): def test_func(self): print("from A") class B(A): pass # def test_func(sel ...