P6154 游走 做法很简单,一个记忆化搜索。 两个关键点: 1.深搜记录路径条数f[x]=(f[x]+f[y])%mod
2.回溯中记录以x为起点的路径长度g[x]=(g[x]+f[y]+g[y])%mod
#include
#define int long long
using namespace std;
const int N=7e5+10;
const int mod=998244353;
int n,m,cnt,tmp,ans;
int tot,to[N],nxt[N],head[N];
int f[N],g[N];
void add(int x,int y) {
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int x) {
if(f[x])
return;
f[x]=1;
for(int i=head[x]; i; i=nxt[i])
{
int y=to[i];
dfs(y);
cout
关注
打赏