首页 > 其他分享 >29. 牛客-一人行者

29. 牛客-一人行者

时间:2022-09-03 11:02:21浏览次数:74  
标签:行者 const 29 牛客 operator return mint dp MOD

本来不想为了这题写一篇博客的,但因为昨天被一组数据卡了一个小时,还是有必要来记录一下。

牛客练习赛 102D:一人行者

题意是给一棵树,求断掉每一条边后得到的两棵树各自的联通子集数量,对 \(998244353\) 取模。

容易想到树形 dp,令 \(dp[u][0/1]\) 表示 \(u\) 的子树中是否包含 \(u\) 的子集数量,显然有状态转移方程:

\[\left\{ \begin{array}{l} dp[u][0]=\sum (dp[v][0]+dp[v][1])\\ dp[u][1]=\prod (1+dp[v][1]) \end{array} \right. \]

其中 \(v\) 是 \(u\) 的子节点。

然后考虑断边的操作,假设断的边是 \((u,v)\),其中 \(v\) 是儿子,那么 \(v\) 所在的子树的答案 \(V=dp[v][0]+dp[v][1]\),而 \(u\) 所在的树的答案是总的联通子集个数减去 \(V\) 再减去以 \(v\) 作为中间节点的联通子集个数。那么还需要求出每个节点“向上的子树”中联通子集的个数,其实就是换根 dp,重新进行一次 dfs 后自上而下计算即可。

于是我交了一份这样的代码:

#include <bits/stdc++.h>
using namespace std;
using std::cin;
using std::cout;
using std::string;
#define endl '\n'
#define TRACE(x) std::cerr << #x << " = " << x << endl
#define fi first
#define se second
#define pb push_back

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int, int>;

constexpr int maxn = 5e5 + 5;
constexpr int inf = 0x3f3f3f3f;
constexpr ll mod = 998244353;
template <uint MOD = mod> struct mint {
    uint x;
    mint() : x(0) {}
    mint(ll _x) {
        _x %= MOD;
        if (_x < 0)
            _x += MOD;
        x = _x;
    }
    explicit operator int() const { return x; } 
    mint &operator+=(const mint &a) {
        x += a.x;
        if (x >= MOD)
            x -= MOD;
        return *this;
    }
    mint &operator-=(const mint &a) {
        x += MOD - a.x;
        if (x >= MOD)
            x -= MOD;
        return *this;
    }
    mint &operator*=(const mint &a) {
        x = (ull)x * a.x % MOD;
        return *this;
    }
    mint pow(ll pw) const {
        mint res = 1;
        mint cur = *this;
        while (pw) {
            if (pw & 1)
                res *= cur;
            cur *= cur;
            pw >>= 1;
        }
        return res;
    }
    mint inv() const {
        assert(x != 0);
        uint t = x;
        uint res = 1;
        while (t != 1) {
            uint z = MOD / t;
            res = (ull)res * (MOD - z) % MOD;
            t = MOD - t * z;
        }
        return res;
    }
    mint &operator/=(const mint &a) { return *this *= a.inv(); }
    mint operator+(const mint &a) const { return mint(*this) += a; }
    mint operator-(const mint &a) const { return mint(*this) -= a; }
    mint operator*(const mint &a) const { return mint(*this) *= a; }
    mint operator/(const mint &a) const { return mint(*this) /= a; }
    bool operator==(const mint &a) const { return x == a.x; }
    bool operator!=(const mint &a) const { return x != a.x; }
    bool operator<(const mint &a) const { return x < a.x; }
    friend std::istream &operator>>(std::istream &is, mint &a) {
        ll v;
        is >> v;
        a = mint(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const mint &a) {
        return os << a.x;
    }
};

int fa[maxn];
pii q[maxn];
vector<int> g[maxn];
using Z = mint<998244353>;
Z dp[2][maxn];
void dfs(int u, int f) {
    fa[u] = f;
    dp[1][u] = 1;
    for (auto& v : g[u]) {
        if (v == f) continue;
        dfs(v, u);
        dp[0][u] += dp[0][v] + dp[1][v];
        dp[1][u] *= (dp[1][v] + 1);
    }
}
Z up[maxn];
void dfs1(int u, int f) {
    if (f) {
        up[u] = (up[f] + 1) * dp[1][f] / (dp[1][u] + 1);
    }
    for (auto& v : g[u]) {
        if (v == f) continue;
        dfs1(v, u);
    }
}
void solve() {
    int n;
    cin >> n;
    for (int i = 1, u, v; i < n; ++i) {
        cin >> u >> v;
        g[u].pb(v), g[v].pb(u);
        q[i] = {u, v};
    }
    dfs(1, 0);
    dfs1(1, 0);
    for (int i = 1; i < n; ++i) {
        auto [u, v] = q[i];
        bool f = 0;
        if (fa[u] == v) {
            f = 1;
            swap(u, v);
        }
        Z V = dp[0][v] + dp[1][v];
        Z U = dp[0][1] + dp[1][1] - V - up[v] * dp[1][v];
        if (f) swap(U, V);
        cout << U << " " << V << endl;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    for (int cas = 1; cas <= T; ++cas) {
        solve();
    }
    return 0;
}

这份代码有一个问题,你能看出来吗?

提示:问题出在 dp 的过程中

提示2:mint 类没有任何问题,也没有数组越界,但是 RE 了

答案很简单。

注意第 105 行。

当 \(dp[1][u]=mod-1\) 的时候,这里就出问题了。

之前的思路都是正确的,只要避免这里除零的问题,就可以得到正确的答案了。所以记录一下前后缀积就行了。

昨晚调了一个小时也没发现是这里的问题。不过看起来不止我一个人在这里被卡了。

以后再用乘法逆元的时候,一定要考虑清楚会不会产生除 \(0\) 的情况。

标签:行者,const,29,牛客,operator,return,mint,dp,MOD
From: https://www.cnblogs.com/theophania/p/p29.html

相关文章

  • "蔚来杯"2022牛客暑期多校训练营6
    A.Array给定\(\geqslant2\)的整数\(a_1,a_2,...,a_n\),满足\(\sum\limits_{i=1}^n\frac{1}{a_i}\leqslant\frac{1}{2}\),构造一个循环数列,使得其任意长度为\(a_i\)的子区......
  • 牛客练习赛102 B-C
    B清楚姐姐带带我 当数大于1e9的时候就取模//#defineintllconstintN=1e5+10,mod=19980829;intn,m;voidsolve(){llres=0;boolflag......
  • 牛客练习赛102
    A对所有消息做一下前缀和,对每个人的消息做一下前缀和,分别判断是否有长度为\(a,b\)的连续段B考虑当前已经算出来前\(i-1\)个操作的最大值\(x\),那么第\(i\)个操作......
  • 29.祈使句
    一,祈使句的定义:祈使句就是表示请求、命令、建议、祝愿、邀请或要求的句子。其中句首的动词要用原型。二,祈使句的五大结构类型:①Please +动词原形+其他 ,它的否定形......
  • 2022牛客暑假多校01B[Spirit Circle Observation]
    2022牛客暑假多校01B[SpiritCircleObservation]大致题意给出一个长度为\(n\)的字符串\(s\),求有多少个子串对\((A,B)\),满足\(1.|A|=|B|\)\(2.\overline{A}+1=......
  • MRS There is a hole [0x212-0x229] in .debug_loc section. 报错
      这个警告产生是因为MRS删除DEBUG定义导致,要消除警告MRS设置需要更改一下 ......
  • 29 | JAVA集合Set(一种接口,Map的同级映射)
    Set如果我们只需要存储不重复的key,并不需要存储映射的value,那么就可以使用Set。Set用于存储不重复的元素集合,它主要提供以下几个方法:将元素添加进Set<E>:booleanadd(E......
  • 2022 8 29
    https://www.lanqiao.cn/problems/1463/learning/主要是运行时间问题:importosimportsysn=2021041820210418l=[]i=2x=nwhilei<pow(x+1,0.5):ifx%i==0......
  • 操,26-27-28-29-30-31-9-1,操一眨眼七天过去了!!!
    操,26-27-28-29-30-31-9-1,操一眨眼七天过去了!!! 操,我感觉距离上次写博客反省才刚刚过去,我竟然不知道干了什么都!!!!!!我今天干了什么?上午,我忘了。。我想起来了,上午坐那看盘,看......
  • cs294-ai-sys2022 lectures10 reading
    1. Theano:ACPUandGPUMathCompilerinPython(2010optional)动机Pythonisslow,onereasonisthatPythonusesfull-fledgedPythonobjectsontheheapto......