WC模拟(1.6) T1 送你一个DAG

送你一个DAG

题目背景:

1.6 WC模拟T1

分析:斯特林数优化DP

 

最暴力的想法就是f[i][j]表示到i点长度为j的路径有多少条,复杂度O(n2),然后考虑优化,我们发现将disk变为(dis + 1)k,我们将其二项式定理展开,可以得到一个系数只与幂次和k相关的多项式,那么我们定义状态f[i][j]表示到i的所有路径长度的j次方之和,可以预处理组合数之后,O(k)获得f[i][j]的值,这样做的复杂度是O(k2),考虑如何继续优化,我们知道,第二类斯特林数可以进行下降幂到自然幂的转化,那么我们考虑维护下降幂,定义f[i][j]表示到i的所有路径长度的j次下降幂之和,那么考虑将disk转化为(dis + 1)k,显然

(dis + 1)k

= (dis + 1) * disk - 1

= (dis - k + 1) * disk - 1 + k * disk-1

= disk + k * disk - 1

那么我们就可以O(1)转移得到f[i][j],然后最后利用公式:

 

O(nk)的时间转化为自然幂就可以了。

 

Source:

 

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 100000 + 10;
const int MAXK = 500 + 10;
const int mod = 998244353;

int n, m, x, y, k;
int f[MAXN][MAXK], s[MAXK][MAXK];
std::vector<int> edge[MAXN], top;
bool vis[MAXN];

inline void add(int &x, int t) {
	x += t, (x >= mod) ? (x -= mod) : (x);
}

inline void add_edge(int x, int y) {
	edge[x].push_back(y);
}

inline void read_in() {
	R(n), R(m), R(k);
	for (int i = 1; i <= m; ++i) R(x), R(y), add_edge(x, y);
}

inline void pre(int n) {
	s[0][0] = 1;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i; ++j) {
			add(s[i][j], (long long)j * s[i - 1][j] % mod);
			add(s[i][j], s[i - 1][j - 1]);
		}
}

void dfs(int cur) {
	vis[cur] = true;
	for (register int p = 0; p < edge[cur].size(); ++p) {
		int v = edge[cur][p];
		if (!vis[v]) dfs(v);
	}
	top.push_back(cur);
}

inline void solve() {
	dfs(1), pre(k), f[1][0] = 1;
	for (int i = top.size() - 1; i >= 0; --i) {
		int cur = top[i];
		for (register int p = 0; p < edge[cur].size(); ++p) {
			int v = edge[cur][p];
			add(f[v][0], f[cur][0]);
			for (register int j = 1; j <= k; ++j) {
				add(f[v][j], (long long)j * f[cur][j - 1] % mod);
				add(f[v][j], f[cur][j]);
			}
		}
	}
	
	for (register int cur = 1; cur <= n; ++cur) {
		int ans = 0;
		for (register int i = 0; i <= k; ++i)
			add(ans, (long long)s[k][i] * f[cur][i] % mod);
		W(ans), write_char('\n');
	}
}

int main() {
	freopen("xmasdag.in", "r", stdin);
	freopen("xmasdag.out", "w", stdout);
	read_in();
	solve();
	flush();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值