bzoj3127/3697 [Usaco2013 Open]Yin and Yang
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3127
http://www.lydsy.com/JudgeOnline/problem.php?id=3697
【题解】
点分治。
f[i,0/1]表示前面一坨路径和为i,是否存在休息站。
分类讨论:休息站在点分的地方,休息站在前面子树,休息站在当前子树
子树合并的时候顺便算一发贡献即可。
# include <stdio.h>
# include <string.h>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ;
const int mod = 1e9+; # define RG register
# define ST static int n; ll ans = ;
int head[M], nxt[M], to[M], w[M], tot=;
bool vis[M]; inline void add(int u, int v, int _w) {
++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; w[tot] = _w;
}
inline void adde(int u, int v, int _w) {
add(u, v, _w); add(v, u, _w);
} int sz[M], mx[M];
inline void dfsSize(int x, int fa = ) {
sz[x] = ; mx[x] = ;
for (int i=head[x]; i; i=nxt[i]) {
if(to[i] == fa || vis[to[i]]) continue;
dfsSize(to[i], x);
sz[x] += sz[to[i]];
if(sz[to[i]] > mx[x]) mx[x] = sz[to[i]];
}
} int centre = , mi;
inline void dfsCentre(int x, int tp, int fa = ) {
if(sz[tp] - sz[x] > mx[x]) mx[x] = sz[tp] - sz[x];
if(mx[x] < mi) mi = mx[x], centre = x;
for (int i=head[x]; i; i=nxt[i]) {
if(to[i] == fa || vis[to[i]]) continue;
dfsCentre(to[i], tp, x);
}
} int t[M];
ll f[M][], g[M][];
int mxd, vmin = 1e9, vmax = ;
// g: cur tree, f: all
inline void dfsAns(int x, int v, int fa) {
if(t[v]) g[v][] ++;
else g[v][] ++;
t[v] ++;
vmin = min(vmin, v), vmax = max(vmax, v);
for (int i=head[x]; i; i=nxt[i]) {
if(to[i] == fa || vis[to[i]]) continue;
dfsAns(to[i], v+w[i], x);
}
t[v] --;
} inline void calcAns(int x) {
int Vmin = 1e7, Vmax = ;
f[n][] = ;
for (int i=head[x]; i; i=nxt[i]) {
if(vis[to[i]]) continue;
vmin = 1e7, vmax = ;
dfsAns(to[i], n+w[i], x);
Vmin = min(Vmin, vmin), Vmax = max(Vmax, vmax);
ans += (f[n][]-) * g[n][]; //啥都不动,没有1,是没有意义的,为了计数方便,前面先赋值1.
for (int j=vmin-n; j<=vmax-n; ++j)
// 将g合并到f上,统计答案
ans += f[n-j][] * g[n+j][] + f[n-j][] * g[n+j][] + f[n-j][] * g[n+j][];
for (int j=vmin; j<=vmax; ++j) {
f[j][] += g[j][];
f[j][] += g[j][];
g[j][] = g[j][] = ;
}
}
for (int j=Vmin; j<=Vmax; ++j) f[j][] = f[j][] = ;
} inline void dfs(int x) {
dfsSize(x);
mi = n; dfsCentre(x, x);
// cross the centre
calcAns(centre);
// ================
vis[centre] = ;
for (int i=head[centre]; i; i=nxt[i]) {
if(!vis[to[i]]) dfs(to[i]);
}
} int main() {
scanf("%d", &n);
for (int i=; i<n; ++i) {
int u, v, _w; scanf("%d%d%d", &u, &v, &_w);
adde(u, v, _w ? : -);
}
dfs();
printf("%lld\n", ans);
return ;
}
把0/1记录1/-1会方便很多啊不过注意下标+n
bzoj3127/3697 [Usaco2013 Open]Yin and Yang的更多相关文章
- BZOJ 3127 [Usaco2013 Open]Yin and Yang(树点分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3127 [题目大意] 给出一棵01边权树,求存在多少条路径,使得路径上0和1的数量相同, ...
- 【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang
点分治 Orz hzwer 倒是比较好想到点分治……然而在方案统计这里,我犯了两个错误…… 1.我比较傻逼的想的是:通过儿子来更新父亲,也就是统计以x为根的子树中xxxx的路径有多少条……这样转移. ...
- BZOJ3127:[USACO2013OPEN]Yin and Yang
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- 洛谷P3085 [USACO13OPEN]阴和阳Yin and Yang(点分治,树上差分)
洛谷题目传送门 闲话 偶然翻到一道没有题解的淀粉质,想证明一下自己是真的弱 然而ZSYC(字符串组合)早就切了 然后证明成功了,WA到怀疑人生,只好借着ZSY的代码拍,拍了几万组就出来了... 思路 ...
- Yin and Yang Stones(思路题)
Problem Description: A mysterious circular arrangement of black stones and white stones has appeared ...
- 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞
3697: 采药人的路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 681 Solved: 246[Submit][Status][Discus ...
- Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)
https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- AssetBundle loading failed because.....已解决
http://blog.csdn.net/ldghd/article/details/9632455 ***************************** 一 ******* ...
随机推荐
- 深度学习:激活函数的比较和优缺点,sigmoid,tanh,relu
https://blog.csdn.net/u011684265/article/details/78039280
- axios应用
Skip to content Features Business Explore Marketplace Pricing Sign in or Sign up Watch929 St ...
- LINUX网络相关命令(转)
网络连通性 Ping:发送一个 ICMP 回声请求消息给主机,一直持续到到你按下 Ctrl+C .Ping 表示一个包通过 ICMP 从你的机器发送出去,然后在IP层得到回应.Ping 可以检测你与另 ...
- MySQL☞between ... and ...
between 初值 and 终值:求出该列列值在初值和终值之间所有的数据 格式如下: select 列名/* from 表名 where 列名 between 初值 and 终值 如下图:
- 问题 C: 质因数的个数
1947: 质因数的个数 时间限制: 1 Sec 内存限制: 32 MB提交: 245 解决: 114[提交][状态][讨论版][命题人:外部导入] 题目描述 求正整数N(N>1)的质因数的 ...
- CentOS安装Harbor
CentOS版本:7.4 Harbor版本:1.5.0 Docker版本:1.12.6 Docker Compose版本:1.21.2 一.安装Harbor(http方式,80端口) 1.安装Dock ...
- UVA 11881 Internal Rate of Return(数学+二分)
In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero ...
- 微信小程序—setTimeOut定时器的坑
原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...
- 【EasyNetQ】- 多态发布和订阅
您可以订阅接口,然后发布该接口的实现. 我们来看一个例子.我有一个接口IAnimal和两个实现Cat和Dog: public interface IAnimal { string Name { get ...
- 数组中键key相等时,后面的值覆盖前面的值
<?php $arr[]='abc'; $arr[]='; $arr[]='; $arr[]='; var_dump($arr); 结果;