题解CF#Bubble Cup X - Finals E-Casinos and travel

Posted twilight-sx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解CF#Bubble Cup X - Finals 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][2], f[maxn][2];

int read()
{
    int x = 0, k = 1;
    char c; c = getchar();
    while(c < 0 || c > 9) { if(c == -) k = -1; c = getchar(); }
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = getchar();
    return x * k;
}

struct edge
{
    int cnp, to[maxn], last[maxn], head[maxn];
    edge() { cnp = 2; }
    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 = 1, timer = mod - 2;
    for(; timer; timer >>= 1, x = x * x % mod)
        if(timer & 1) base = base * x % mod;
    return base;
}

void dfs(int u)
{
    int t1 = 1, flag = 0;
    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 = 1; 
        t1 = t1 * ((g[v][0] + g[v][1]) % mod) % mod;
    }
    if(flag) g[u][0] = g[u][1] = t1 % mod;
    else g[u][0] = 1, g[u][1] = 0;
}

void dfs2(int u)
{
    int t1 = f[fa[u]][0] * g[fa[u]][0] % mod * Inv(g[u][0] + g[u][1]) % mod;
    int t2 = f[fa[u]][1] * g[fa[u]][1] % mod * Inv(g[u][0] + g[u][1]) % mod;
    f[u][0] = f[u][1] = (t1 + t2) % mod; if(u == 1) f[u][0] = 1; if(u == 1 && rec != 1) f[u][1] = 1;
    Up(ans, (f[u][0] * g[u][0]) % mod * 2 % 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 = 1; i < n; i ++)
    {
        int x = read(), y = read();
        E1.add(x, y); if(x == 1 || y == 1) rec ++;
    }
    dfs(1); dfs2(1);
    printf("%I64d
", ans);
     return 0;
}

  其实我们可以直接推公式。我们注意到每个叶子结点完全可以决定从根到的路径上的节点是偶数个还是奇数个,也就是它本身是否建造赌场是一定的。至于剩下的节点,我们大可以随便决定。所以 (ans = (n - x) * 2^{n - x} + x * 2 ^ {n - x + 1})。(其中 (x) 为叶子节点的个数)。那么整理一下就是 (ans = (n + x) * 2 ^ {n - x})。

以上是关于题解CF#Bubble Cup X - Finals E-Casinos and travel的主要内容,如果未能解决你的问题,请参考以下文章

Bubble Cup 11 - Finals [Online Mirror, Div. 1] 体验记 + 部分题解

CF-1218 or 1219 Bubble Cup 12--BubbleReactor

codeforces cf round#505(based on vk cup 2018 final) C. Plasticine zebra

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) BWeakened Common Diviso(代

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) 题解

Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))ABCD1D2E题解