【题解】CF#852 E-Casinos and travel
天啊我怎么这么蠢……写了一个树形dp,的确发现记录的很多值并没有什么用,然而当时脑子没转过弯来还是写了这个树形dp……虽然能A但就不解释了,总之是个垃圾算法(ー̀дー́)
#include <bits/stdc++.h>
using namespace std;
#define maxn 1000000
#define mod 1000000007
#define int long long
int n, ans, rec, fa[maxn];
int g[maxn][], f[maxn][]; int read()
{
int x = , k = ;
char c; c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} struct edge
{
int cnp, to[maxn], last[maxn], head[maxn];
edge() { cnp = ; }
void add(int u, int v)
{
to[cnp] = v, last[cnp] = head[u], head[u] = cnp ++;
to[cnp] = u, last[cnp] = head[v], head[v] = cnp ++;
}
}E1; void Up(int &x, int y) { x = (x + y) % mod; }
int Inv(int x)
{
int base = , timer = mod - ;
for(; timer; timer >>= , x = x * x % mod)
if(timer & ) base = base * x % mod;
return base;
} void dfs(int u)
{
int t1 = , flag = ;
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
if(v == fa[u]) continue; fa[v] = u;
dfs(v); flag = ;
t1 = t1 * ((g[v][] + g[v][]) % mod) % mod;
}
if(flag) g[u][] = g[u][] = t1 % mod;
else g[u][] = , g[u][] = ;
} void dfs2(int u)
{
int t1 = f[fa[u]][] * g[fa[u]][] % mod * Inv(g[u][] + g[u][]) % mod;
int t2 = f[fa[u]][] * g[fa[u]][] % mod * Inv(g[u][] + g[u][]) % mod;
f[u][] = f[u][] = (t1 + t2) % mod; if(u == ) f[u][] = ; if(u == && rec != ) f[u][] = ;
Up(ans, (f[u][] * g[u][]) % mod * % mod);
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
if(v == fa[u]) continue;
dfs2(v);
}
} signed main()
{
n = read();
for(int i = ; i < n; i ++)
{
int x = read(), y = read();
E1.add(x, y); if(x == || y == ) rec ++;
}
dfs(); dfs2();
printf("%I64d\n", ans);
return ;
}
其实我们可以直接推公式。我们注意到每个叶子结点完全可以决定从根到的路径上的节点是偶数个还是奇数个,也就是它本身是否建造赌场是一定的。至于剩下的节点,我们大可以随便决定。所以 \(ans = (n - x) * 2^{n - x} + x * 2 ^ {n - x + 1}\)。(其中 \(x\) 为叶子节点的个数)。那么整理一下就是 \(ans = (n + x) * 2 ^ {n - x}\)。
【题解】CF#852 E-Casinos and travel的更多相关文章
- CF 852E Casinos and travel
题目链接 \(Desccription\) 给定一棵树,John从任意一个点开始,每次走向一个未到达过的点.每个点都可以有或没有赌场,每经过一个赌场心情都会反转,旅行开始前心情很好. 问有多少种方案使 ...
- 竞赛题解 - CF Round #524 Div.2
CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)
还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- [CF852E]Casinos and travel(2019-11-15考试)
题目大意 有一棵\(n\)个点的树,令\(f(u)\)表示给树黑白染色,满足以\(u\)为根的树中,每个叶子节点到根的路径上黑点数量为偶数的染色方案数,求\(\sum\limits_{u=1}^n f ...
- [题解] [CF 1250J] The Parade
题面 题目大意: 给定一个 \(n\) , 所有军人的数量均在 \([1, n]\) 给定 \(a_i\) 代表高度为 \(i\) 的军人的个数 你要将这些军人分成 \(k\) 行, 满足下面两个条件 ...
- 题解 CF 1372 B
题目 传送门 题意 给出 \(n\),输出 \(a\) ,\(b\) (\(0 < a \leq b < n\)),使\(a+b=n\)且 \(\operatorname{lcm}(a,b ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)
随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T3(贪心)
是一道水题 虽然看起来像是DP,但其实是贪心 扫一遍就A了 QwQ #include <cstdio> #include <algorithm> #include <cs ...
随机推荐
- ORB-SLAM(十一)EPnP
EPnP在ORB-SLAM中主要用于Tracking线程中的重定位Relocalization模块,需要通过当前关键帧Bow与候选帧匹配上的3D地图点,迅速建立当前相机的初始姿态. PnP问题解决了已 ...
- fastjson处理json
返回主页 你是风儿 博客园首页新随笔联系订阅管理 随笔 - 29 文章 - 0 评论 - 23 FastJson对于JSON格式字符串.JSON对象及JavaBean之间的相互转换 fastJson对 ...
- python generator
def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] for char in reverse('g ...
- hdu2094产生冠军(思维题)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【SpringCloud】第七篇: 高可用的分布式配置中心(Spring Cloud Config)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- Python 集合内置函数大全(非常全!)
Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员 s ...
- Django常用命令总结
安装Django: pip install django 指定版本 pip3 install django==2.0 新建项目: django-admin.py startprject mysite ...
- spring boot 报错 Error creating bean with name
Application 启动类 要和父目录平级
- jQuery 对象 与 原生 DOM 对象 相互转换
区别 jQuery 选择器得到的 jQuery对象 和 原生JS 中的document.getElementById() document.querySelector取得的 DOM对象 是两种不同类型 ...
- 352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...