传送门: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. PRO*C 函数事例 3 -- 游标使用

    1.Oracle中的游标    Oracle使用两种游标: 显式游标和隐式游标. 不管语句返回多少条记录, Oracle为每条使用的SQL语句隐式地定义一个游标. Oracle为每个DELETE , ...

  2. How to add a webpart to your website

          I have download a webpart that can play media on the website from the internet.Then how to add ...

  3. jmeter使用BeanShell断言

    1. 首先存储一个接口的响应结果,如在http请求的BeanShell PostProcessor: import java.io.UnsupportedEncodingException; Syst ...

  4. 程序员编程利器:20款最好的免费的IDEs和编辑器

    程序员编程利器:20款最好的免费的IDEs和编辑器 还没转眼明年可就大年三十了,忙的可真是晕头转了个向,看着亲朋好友们那让人欣羡的小肚腩,不禁感慨,岁月是一把猪饲料,绿了芭蕉,肥了那杨柳小蛮腰,可怜我 ...

  5. LLLYYY的数字思维(模拟题)

    链接:https://ac.nowcoder.com/acm/contest/318/G LLLYYY很喜欢写暴力模拟贪心思维.某一天在机房,他突然抛给了队友ppq一 个问题.问题如下: 有一个函数f ...

  6. 今日头条 2018 AI Camp 6 月 2 日在线笔试编程题第二道——两数差的和

    题目 给 n 个实数 a_1, a_2 ... a_n, 要求计算这 n 个数两两之间差的绝对值下取整后的和是多少. 输入描述 第一行为一个正整数 n 和一个整数 m.接下来 n 行,第 i 行代表一 ...

  7. UVA 11884 A Shooting Game(记忆化搜索)

    A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...

  8. unity 归纳

    1.获取控件四个角在屏幕上的坐标 Vector3[] corners = new Vector3[]; gameObject.GetComponent<RectTransform>().G ...

  9. 玩转VFS(sys_open, overlayfs, rename)

    带着问题学习dentry 1)文件删除时 dentry的结构是如何变化的?是直接设置无效的吗?如果此时正好有个访问是在要删除目录下的一个文件,这咋办呢? 2)内核document目录下级联式地使用 d ...

  10. 解决微信小程序配置https不成功问题

    拿到一个微信小程序的项目,需要配置https安全链接(为什么必须使用https不再赘述),预想这个已经很成熟的流程,应该不会有太大问题,结果还真是出乎意料,竟然掉进一个大坑,折腾好久. 申请证书配置的 ...