树形DP,恩然后就不会了。。。

先写了个错的离谱程序。。。果然WA了

然后开始乱搞,欸,对了!

令f[i], g[i], h[i]分别表示i号节点自己放士兵,被儿子上的士兵控制,不被儿子上的士兵控制但被父亲上的士兵控制的情况下,以i为子树中最少的士兵数

F[i], G[i], H[i]表示对应的方案数,然后这方程写的沁人心脾。。。看程序吧

 /**************************************************************
Problem: 2314
User: rausen
Language: C++
Result: Accepted
Time:3224 ms
Memory:99512 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
const int N = 5e5 + ;
const int inf = 1e9;
const int mod = ;
const int Maxlen = N * ; struct edge {
int next, to;
edge() {}
edge(int _n, int _t) : next(_n), to(_t) {}
} e[N << ]; int n;
int first[N], tot;
ll f[N], g[N], h[N];
ll F[N], G[N], H[N];
ll ans;
bool vis[N];
char buf[Maxlen], *c = buf;
int Len; inline int read() {
int x = ;
while (*c < '' || '' < *c) ++c;
while ('' <= *c && *c <= '')
x = x * + *c - '', ++c;
return x;
} inline void Add_Edges(int x, int y) {
e[++tot] = edge(first[x], y), first[x] = tot;
e[++tot] = edge(first[y], x), first[y] = tot;
} #define y e[x].to
void dfs(int p) {
int x, mn;
ll t;
vis[p] = ;
f[p] = , g[p] = inf, h[p] = ;
F[p] = G[p] = H[p] = ;
for (x = first[p]; x; x = e[x].next)
if (!vis[y]) {
dfs(y); mn = min(min(f[y], g[y]), h[y]), t = ;
if (f[y] == mn) t += F[y];
if (g[y] == mn) t += G[y];
if (h[y] == mn) t += H[y];
f[p] += mn, (F[p] *= t) %= mod; mn = min(min(g[p] + f[y], g[p] + g[y]), h[p] + f[y]), t = ;
if (g[p] + f[y] == mn) t += G[p] * F[y];
if (g[p] + g[y] == mn) t += G[p] * G[y];
if (h[p] + f[y] == mn) t += H[p] * F[y];
g[p] = mn, G[p] = t % mod; h[p] += g[y], (H[p] *= G[y]) %= mod;
}
}
#undef y int main() {
Len = fread(c, , Maxlen, stdin);
buf[Len] = '\0';
int i, x, y;
n = read();
for (i = ; i < n; ++i) {
x = read(), y = read();
Add_Edges(x, y);
}
dfs();
printf("%lld\n", min(f[], g[]));
printf("%lld\n", f[] == g[] ? (F[] + G[]) % mod : f[] < g[] ? F[] : G[]);
return ;
}

BZOJ2314 士兵的放置的更多相关文章

  1. bzoj2314: 士兵的放置(树形DP)

    0表示被父亲控制,1表示被儿子控制,2表示被自己控制.f表示最少士兵数,g表示方案数. 转移贼难写,写了好久之后写不下去了,看了一眼题解,学习了...原来还可以这么搞 比如求f[i][1]的时候,要在 ...

  2. 【BZOJ2314】士兵的放置 树形DP

    [BZOJ2314]士兵的放置 Description 八中有N个房间和N-1双向通道,任意两个房间均可到达.现在出了一件极BT的事,就是八中开始闹鬼了.老大决定加强安保,现在如果在某个房间中放一个士 ...

  3. BZOJ 2314: 士兵的放置( 树形dp )

    树形dp... dp(x, 0)表示结点x不放士兵, 由父亲控制: dp(x, 1)表示结点x不放士兵, 由儿子控制: dp(x, 2)表示结点x放士兵. ---------------------- ...

  4. BZOJ 2314 士兵的放置(支配集)

    显然是\(DP\). 设\(dp[i][0/1/2]\)代表以i为根且\(i上有士兵放置/i被控制但i上没有士兵/i没有被控制\)的最小代价. \(g[i][0/1/2]\)代表对应的方案数. 然后运 ...

  5. bzoj AC倒序

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

  6. 【BZOJ-1458】士兵占领 最大流

    1458: 士兵占领 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 782  Solved: 456[Submit][Status][Discuss] ...

  7. bzoj1458 士兵占领

    费用流,连下面几类边 1.s->s',流量为n*m,费用为0,表示最多可放置n*m个士兵 2.s'->行 (1)流量为a[i],费用为-n*m,表示必须在这一行放置a[i]个士兵. (2) ...

  8. 【BZOJ】1458: 士兵占领(上下界网络流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1458 是不是我脑洞太小了.......直接弄上下界最小流........(就当复习了.. 二分图X和 ...

  9. 1458: 士兵占领 - BZOJ

    Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放 ...

随机推荐

  1. (ubuntu ufw)My firewall is blocking network connections from the docker container to outside

    Maybe this is due to the current version, but the current answer doesn't work on my system (Docker 0 ...

  2. SmartSprites 智能批量合并 CSS 雪碧图

    做前端的稍微有点经验的都知道 可以通过合并小图片 来减少请求数, 最早可能都是通过 fw.ps 等工具来手动合并, 这种方式的缺点就不吐槽了,效率低,可维护性差 等等 .... 一些很厉害的人,往往会 ...

  3. 一只青蛙从第一级台阶跳到第n级,每次可以跳任意级,共有多少种跳法,并写出递推式

    是斐波那契数列问题 假设f(n)是n个台阶跳的次数:(假设已经调到第n个台阶,最后一次是由哪个台阶跳上来的) f(n) = f(n-1)+f(n-2)+...+f(n-(n-1)) + f(n-n) ...

  4. 解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat

    解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat ...

  5. android 获取经纬度

    android 定位的两种方式:GPS_PROVIDER and NETWORK_PROVIDER 定位的可以借助LocationManager来实现 MainActivity代码 static fi ...

  6. 制造抽象基类--《C++必知必会》 条款33

    抽象类,含有纯虚函数的类,不可以创建对象. 然而,有时我们并不需要某个函数定义为纯虚函数,但是任然希望此类像抽象类一样,不可以创建对象. 方法1:通过确保类中不存在共有构造函数来模拟抽象基类的性质.意 ...

  7. cocos代码研究(25)Widget子类PageView学习笔记

    基础理论 ListView控件是一个显示滚动项目列表的视图组. 列表项是通过使用addChild或insertDefaultItem插入到列表中的,继承自ScrollView. 代码实践 static ...

  8. Mail.Ru Cup 2018 Round 3 Solution

    A. Determine Line Water. #include <bits/stdc++.h> using namespace std; ]; int main() { while ( ...

  9. uva473

     Raucous Rockers  You just inherited the rights to n previously unreleased songs recorded by the pop ...

  10. HIVE: 自定义TextInputFormat (旧版MapReduceAPI ok, 新版MapReduceAPI实现有BUG?)

    我们的输入文件 hello0, 内容如下: xiaowang 28 shanghai@_@zhangsan 38 beijing@_@someone 100 unknown 逻辑上有3条记录, 它们以 ...