zoukankan      html  css  js  c++  java
  • bzoj4011 [HNOI2015]落忆枫音 拓扑排序+DP

    题目传送门

    https://lydsy.com/JudgeOnline/problem.php?id=4011

    题解

    首先考虑如果没有那么一条被新加进来的奇怪的边的做法。

    我们只需要给每一个点挑一个父亲就可以接上去了,所以答案应该是每一个点的入度的乘积。

    但是有了那样一条新加进来的边以后,如果破坏了原图的 DAG 性,导致如果直接选入度的话会可能有环。我们可以先直接和上面一样统计入度乘积,然后去掉不合法的方案。

    不合法的方案就是存在环的方案。因为环是新加的边 ((x, y)) 带来的,所以新加的 ((x, y)) 一定在环中。所以每一条环都是从 (y)(x) 的路径加上整条边。

    我们需要统计每一条路径的不在这条路径上的点的入度乘积和即可。


    预处理逆元以后,时间复杂度 (O(n))

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 100000 + 7;
    const int M = 200000 + 7;
    const int P = 1e9 + 7;
    
    int n, m, ex, ey, ans;
    int q[N], idg[N], idg2[N], dp[N], vis[N];
    
    struct Edge { int to, ne; } g[M]; int head[N], tot;
    inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
    inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }
    
    inline int smod(int x) { return x >= P ? x - P : x; }
    inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
    inline int fpow(int x, int y) {
    	int ans = 1;
    	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
    	return ans;
    }
    
    int fac[N], inv[N], ifac[N];
    inline void ycl(const int &n = ::n) {
    	fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (ll)fac[i - 1] * i % P;
    	inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (ll)(P - P / i) * inv[P % i] % P;
    	ifac[0] = 1; for (int i = 1; i <= n; ++i) ifac[i] = (ll)ifac[i - 1] * inv[i] % P;
    }
    inline int C(int x, int y) {
    	if (x < y) return 0;
    	return (ll)fac[x] * ifac[y] % P * ifac[x - y] % P;
    }
    
    inline void dfs(int x) {
    	vis[x] = 1;
    	for fec(i, x, y) {
    		++idg[y];
    		if (!vis[y]) dfs(y);
    	}
    }
    
    inline void work() {
    	ycl();
    	ans = 1, idg[1] = 1, ++idg[ey];
    	for (int i = 1; i <= n; ++i) ans = (ll)ans * idg[i] % P;
    	memcpy(idg2, idg, sizeof(int) * (n + 1));
    	memset(idg, 0, sizeof(int) * (n + 1));
    	dfs(ey);
    	int hd = 0, tl = 0;
    	dp[ey] = ans, q[++tl] = ey;
    	while (hd < tl) {
    		int x = q[++hd];
    		dp[x] = (ll)dp[x] * inv[idg2[x]] % P;
    		for fec(i, x, y) {
    			sadd(dp[y], dp[x]);
    			if (!--idg[y]) q[++tl] = y;
    		}
    	}
    	if (vis[ex]) sadd(ans, P - dp[ex]);
    	printf("%d
    ", ans);
    }
    
    inline void init() {
    	read(n), read(m), read(ex), read(ey);
    	int x, y;
    	for (int i = 1; i <= m; ++i) read(x), read(y), addedge(x, y), ++idg[y];
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    leetcode_239. 滑动窗口最大值
    pagehelper分页 配置参数 supportMethodsArguments 建议不要全局设置
    java面经收集
    HTTP协议超级详解
    MySQL数据库用户常用权限命令
    MySQL数据库的隔离级别
    InnoDB存储引擎的事务
    MySQL系统函数
    MySQL数据库备份与恢复
    MySQL数据库常见的数据类型
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4011.html
Copyright © 2011-2022 走看看