传送门: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的更多相关文章

  1. BZOJ 3127 [Usaco2013 Open]Yin and Yang(树点分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3127 [题目大意] 给出一棵01边权树,求存在多少条路径,使得路径上0和1的数量相同, ...

  2. 【BZOJ】【3697】采药人的路径&【3127】【USACO2013 Open】Yin and Yang

    点分治 Orz hzwer 倒是比较好想到点分治……然而在方案统计这里,我犯了两个错误…… 1.我比较傻逼的想的是:通过儿子来更新父亲,也就是统计以x为根的子树中xxxx的路径有多少条……这样转移. ...

  3. BZOJ3127:[USACO2013OPEN]Yin and Yang

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...

  4. 洛谷P3085 [USACO13OPEN]阴和阳Yin and Yang(点分治,树上差分)

    洛谷题目传送门 闲话 偶然翻到一道没有题解的淀粉质,想证明一下自己是真的弱 然而ZSYC(字符串组合)早就切了 然后证明成功了,WA到怀疑人生,只好借着ZSY的代码拍,拍了几万组就出来了... 思路 ...

  5. Yin and Yang Stones(思路题)

    Problem Description: A mysterious circular arrangement of black stones and white stones has appeared ...

  6. 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞

    3697: 采药人的路径 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 681  Solved: 246[Submit][Status][Discus ...

  7. Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)

    https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. AssetBundle loading failed because.....已解决

    http://blog.csdn.net/ldghd/article/details/9632455 *****************************      一      ******* ...

随机推荐

  1. jmeter处理响应结果中文乱码

    1. 在线程下面添加后置处理器BeanShell PostProcessor,增加script:prev.setDataEncoding("UTF-8"); 2. 在jmeter. ...

  2. 云计算之路-阿里云上:基于Xen的IO模型进一步分析“黑色0.1秒”问题

    在发现云服务器读取OCS缓存的“黑色0.1秒”是发生在socket读取数据时,而且是发生在读取开始的字节,甚至在socket写数据时(比如写入缓存key)也会出现超过50ms的情况,我们的好奇心被激发 ...

  3. ES6 export,import报错

    问题描述: 现有两个文件: profile.js const firstName = 'Michael'; const lastName = 'Jackson'; const year = 2018; ...

  4. 虚拟现实-VR-UE4-创建C++版工程

    首先,创建C++版本的UE4 项目工程,我使用的是4.12.3版本,据了解,新版本后面的编译都是vs2015 所以,想要创建C++版本的工程,就需要安装vs2015 至于vs2015的安装,自己百度吧 ...

  5. Linux pthread 线程池实现

    基于pthread封装了一个简易的ThreadPool,具有以下特性: 1.具有优先级的任务队列 2.线程池大小可以二次调整,增加线程或者删除空闲线程 3.任务两种重写方式,重写run或者使用函数回调 ...

  6. SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  7. 并查集——poj1611(入门)

    传送门:The Suspects 并查集水题 #include <iostream> #include <cstdio> #include <algorithm> ...

  8. c# 复选下拉框

    引用dll: http://pan.baidu.com/s/1qXa97UO 自定义类: namespace TMI_S { /// <summary> /// 功能描述:自定义多选下拉框 ...

  9. 一个android控件资源网站

    http://www.androidviews.net/ 里面有各种常用控件,赞~

  10. HDU 1338 Game Prediction

    http://acm.hdu.edu.cn/showproblem.php?pid=1338 Problem Description Suppose there are M people, inclu ...