BZOJ 2152 / Luogu P2634 [国家集训队]聪聪可可 (点分治/树形DP)
题意
一棵树,给定边权,求满足两点之间的路径上权值和为3的倍数的点对数量.
分析
点分治板题,对每个重心求子树下面的到根的距离模3分别为0,1,2的点的个数就行了.
O(3nlogn)O(3nlogn)O(3nlogn)
CODE
#include<bits/stdc++.h>
using namespace std;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 20005;
int n, fir[MAXN], cnt; bool ban[MAXN];
struct edge{ int to, nxt, w; }e[MAXN<<1];
inline void link(int u, int v, int wt) {
e[++cnt] = (edge){ v, fir[u], wt }, fir[u] = cnt;
e[++cnt] = (edge){ u, fir[v], wt }, fir[v] = cnt;
}
int Get_Size(int x, int ff) { //求SIZE
int re = 1;
for(int v, i = fir[x]; i; i = e[i].nxt)
if(!ban[v=e[i].to] && v != ff) re += Get_Size(v, x);
return re;
}
int Get_Root(int x, int ff, int Size, int &G) { //找重心
int re = 1; bool flg = true;
for(int v, i = fir[x]; i; i = e[i].nxt)
if(!ban[v=e[i].to] && v != ff) {
int temp = Get_Root(v, x, Size, G);
if(temp<<1 > Size) flg = false;
re += temp;
}
if(Size-re<<1 > Size) flg = false;
if(flg) G = x;
return re;
}
int now[3], tmp[3], Ans;
void Count(int x, int ff, int dis) {
++tmp[dis];
for(int v, i = fir[x]; i; i = e[i].nxt)
if(!ban[v=e[i].to] && v != ff) Count(v, x, (dis+e[i].w)%3);
}
inline void Solve(int x) { //算答案
now[0] = now[1] = now[2] = 0;
for(int v, i = fir[x]; i; i = e[i].nxt)
if(!ban[v=e[i].to]) {
tmp[0] = tmp[1] = tmp[2] = 0;
Count(v, x, e[i].w);
Ans += tmp[0] * now[0] << 1;
Ans += tmp[1] * now[2] << 1;
Ans += tmp[2] * now[1] << 1;
now[0] += tmp[0];
now[1] += tmp[1];
now[2] += tmp[2];
}
Ans += (now[0]<<1) + 1;
}
void TDC(int x) { //点分治
int Size = Get_Size(x, 0);
Get_Root(x, 0, Size, x);
Solve(x); ban[x] = 1; //打标记
for(int v, i = fir[x]; i; i = e[i].nxt)
if(!ban[v=e[i].to]) TDC(v);
}
int main() {
read(n);
for(int i = 1, x, y, z; i < n; ++i)
read(x), read(y), read(z), link(x, y, z%3);
TDC(1);
int d = Ans ? __gcd(Ans, n*n) : 1;
printf("%d/%d\n", Ans/d, n*n/d);
}
UpdUpdUpd…sb了…直接树形DP不就完事了… O(3n)O(3n)O(3n)
CODE
(粘来的代码)
// luogu-judger-enable-o2
#include<cstdio>
#include<cctype>
#define maxn 20005
char cb[1<<15],*cs,*ct;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
inline void read(int &a){
char c;while(!isdigit(c=getc()));
for(a=c-'0';isdigit(c=getc());a=a*10+c-'0');
}
int n,ans,f[maxn][3],g[3];
int fir[maxn],nxt[maxn<<1],to[maxn<<1],w[maxn<<1],tot;
inline void line(int x,int y,int z){nxt[++tot]=fir[x];fir[x]=tot;to[tot]=y;w[tot]=z;}
void dfs(int u,int pre){
f[u][0]=1;
for(int i=fir[u],v;i;i=nxt[i]) if((v=to[i])!=pre){
dfs(v,u);
for(int j=0;j<3;j++) g[(j+w[i])%3]=f[v][j];
for(int j=0;j<3;j++) ans+=f[u][j]*g[j?3-j:0],f[u][j]+=g[j];
}
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main()
{
#ifndef ONLINE_JUDGE
freopen("H.in","r",stdin);
#endif
read(n);
for(int i=1,x,y,z;i<n;i++) read(x),read(y),read(z),line(x,y,z),line(y,x,z);
dfs(1,0);
ans=ans*2+n;
int d=gcd(ans,n*n);
printf("%d/%d",ans/d,n*n/d);
}
BZOJ 2152 / Luogu P2634 [国家集训队]聪聪可可 (点分治/树形DP)的更多相关文章
- luogu P2634 [国家集训队]聪聪可可 点分治
Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...
- BZOJ 2127 / Luogu P1646 [国家集训队]happiness (最小割)
题面 BZOJ传送门 Luogu传送门 分析 这道题又出现了二元关系,于是我们只需要解方程确定怎么连边就行了 假设跟SSS分在一块是选文科,跟TTT分在一块是选理科,先加上所有的收益,再来考虑如何让需 ...
- [BZOJ2152]聪聪可可 点分治/树形dp
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Submit: 3602 Solved: 1858 [Submit][Status][Discu ...
- [LUOGU] P2634 [国家集训队]聪聪可可
点分治裸题,甚至不需要栈回撤. 尝试用容斥写了一波,就是把所有子树混一块计算,最后减去子树内路径条数. #include<iostream> #include<cstring> ...
- bzoj2152 / P2634 [国家集训队]聪聪可可(点分治)
P2634 [国家集训队]聪聪可可 淀粉质点分治板子 边权直接 mod 3 直接点分治统计出所有的符合条件的点对再和总方案数约分 至于约分.....gcd搞搞就好辣 #include<iostr ...
- 洛谷 P2634 [国家集训队]聪聪可可 解题报告
P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一 ...
- 洛谷 P2634 [国家集训队]聪聪可可-树分治(点分治,容斥版) +读入挂+手动O2优化吸点氧才过。。。-树上路径为3的倍数的路径数量
P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- P2634 [国家集训队]聪聪可可(题解)(点分治)
P2634 [国家集训队]聪聪可可(题解)(点分治) 洛谷题目 #include<iostream> #include<cstdlib> #include<cstdio& ...
- 模板—点分治A(容斥)(洛谷P2634 [国家集训队]聪聪可可)
洛谷P2634 [国家集训队]聪聪可可 静态点分治 一开始还以为要把分治树建出来……• 树的结构不发生改变,点权边权都不变,那么我们利用刚刚的思路,有两种具体的分治方法.• A:朴素做法,直接找重心, ...
随机推荐
- Infix to Prefix conversion using two stacks
Infix : An expression is called the Infix expression if the operator appears in between the operands ...
- Linux:IFS分隔符的使用
IFS分隔符的使用 data="name, gender,rollno,location" 我们可以使用IFS读取变量中的每一个条目. oldIFS=$IFS IFS=, #IFS ...
- Kudu建表语句
--建表CREATE TABLE kudu_testdb.perf_test_t1( id string ENCODING PLAIN_ENCODING COMPRESSION SNAPPY, int ...
- lesson12Homework
package StringPractice; public class arrayTest { //1. 把A数组的前5个元素复制到B数组中. public static void main(Str ...
- c语言中gets()的详细用法
gets从标准输入设备读字符串函数.可以无限读取,不会判断上限,以回车结束读取,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出. 从stdin流中读取字符串,直至接受到换行符 ...
- C#面向对象15 多态
多态 概念:让一个对象能够表现出多种的状态(类型) 实现多态的3种手段:1.虚方法 2.抽象类 3.接口 1.虚方法 步骤:1.将父类的方法标记为虚方法,使用关键字 virtual,这个函数可以被子类 ...
- python中关于空的说法
0908自我总结 python中关于空的说法 python中表示空的数据 常量None 常量False 任何形式的数值类型零,如0,0L,0.0,0j 空的序列[],() 空的字典{} 用户自定义的n ...
- centos安装配置mariadb
CentOS7下使用yum安装MariaDB CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL.M ...
- LeetCode 172:阶乘后的零
给定一个整数 n, 返回 n! 结果中尾数为零的数量. 示例 : 输入: 输出: 解释: ! = , 尾数中没有零. 示例 : 输入: 输出: 解释: ! = , 尾数中有个零. 说明:算法的时间复杂 ...
- Mac之常见问题
1. 在命令行下无法使用ll命令 需要设置命令的别名. 文件位置:-/.bash_profile source ~/.profile export PATH="/usr/local/opt/ ...