游走

大致题意:给你一个有向无环图,有重边,每次可以选择任何一个点作为起点和重点,路径长度记为经过路径条数求路径长度的期望

显然是一道期望题目,根据期望定义,我们知道期望等于总价值除以概率也就是次数

然后就是在DAG上随便dp一下就出来了,我们设\(f_i\)\(num_i\)分别表示到i为止的长度和路径条数

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;

const int N = 1e5 + 66, mod = 998244353;

int ver[N], nex[N], head[N], cnt;

inline void add_edge(int x, int y)
{
ver[++ cnt] = y;
nex[cnt] = head[x];
return (void)(head[x] = cnt);
}

int n, m, len, sum;
int deg[N], f[N], num[N];

inline int ksm(int a, int b)
{
int ret(1);
for (; b; b >>= 1)
{
if (b & 1) ret = ret * a % mod;
a = a * a % mod;
}
return ret;
}

inline void topsort()
{
int i, x, y; queue<int>q;
for (i = 1; i <= n; num[i] = 1, ++ i) if (! deg[i]) q.push(i);
while (! q.empty())
{
x = q.front(); q.pop();
for (i = head[x]; i; i = nex[i])
{
y = ver[i];
num[y] = (num[x] + num[y]) % mod, f[y] = (f[y] + f[x] + num[x]) % mod;
if (! -- deg[y]) q.push(y);
}
}
for (i = 1; i <= n; ++ i) len = (len + f[i] % mod) % mod, sum = (sum + num[i] % mod) % mod;
return;
}

signed main()
{
int i, x, y;
n = read(), m = read();
for (i = 1; i <= m; ++ i)
{
x = read(), y = read();
add_edge(x, y);
++ deg[y];
}
topsort();
put(len * ksm(sum, mod - 2) % mod);
return 0;
}