zoukankan      html  css  js  c++  java
  • Lindström–Gessel–Viennot引理及其应用

    给定有向无环图(Directed Acyclic Graph, DAG)$G = (V, E)$,以及源点集$S = { s_1, s_2, dots, s_n }$,汇点集$T = { t_1, t_2, dots, t_n }$。每一条边$(x, y)$都有一个权值$w(x, y)$。我们定义一条路径$pi: x_0 o x_1 o dots o x_k$的权值为:

    $$ w(pi) = prod_{i=1}^k w(x_{i-1}, x_i). $$

    对任意两个节点$x, y in V$,定义

    $$ e(x, y) = sum_{pi: x o y} w(pi), $$

    其中,$pi: x o y$表示一条从$x$出发,到达$y$的路径$pi$。特别地,如果对所有边$(x, y)$,都有$w(x, y) equiv 1$,则$e(x, y)$为$x$到$y$的所有路径的数量。

    我们记路径$n$元组$(pi_1, pi_2, dots, pi_n): S o T$表示$n$条从$S$中节点出发,到达$T$中节点的不相交路径,其中对所有$i in [n]$,$pi_i$从$s_i$出发,并且对任意两条路径$pi_i$和$pi_j$ $(i eq j)$,他们都不经过相同节点(包括路径的两个端点)。对于一个这样的路径$n$元组,存在一个置换$sigma(pi_1, pi_2, dots, pi_n): [n] o [n]$,使得第$i$条路径$pi_i$从$s_i$出发,到达$t_{sigma(i)}$。

    Lindström–Gessel–Viennot引理:

    $$ left| egin{matrix} e(s_1, t_1) & e(s_1, t_2) & dots & e(s_1, t_n) \ e(s_2, t_1) & e(s_2, t_2) & dots & e(s_2, t_n) \ vdots & vdots & ddots & vdots \ e(s_n, t_1) & e(s_n, t_2) & dots & e(s_n, t_n) end{matrix} ight| = sum_{(pi_1, pi_2, dots, pi_n): S o T} operatorname{sign}(sigma(pi_1, pi_2, dots, pi_n)) prod_{i=1}^n w(pi_i), $$

    其中$operatorname{sign}(sigma)$表示置换$sigma$的奇偶性,偶置换为$1$,奇置换为$-1$。

    特别地,如果对所有边$(x, y)$,都有$w(x, y) equiv 1$,并且置换只能是恒等置换(即$sigma(i) = i$)时才存在不相交的路径$n$元组,则所求行列式即为不相交的路径$n$元组的数量。

    例题1

    luogu P6657. 【模板】LGV引理

    有$m$个起点$s_i = (a_i, 1)$,以及对应的$m$个终点$t_i = (b_i, n)$,满足$1 leq a_1 leq a_2 leq dots leq a_m leq n$且$1 leq b_1 leq b_2 leq dots leq b_m leq n$。每一步可以从$(x, y)$走到$(x+1,y)$或者$(x,y+1)$。求两两不相交的路径$m$元组的数量。$1 leq n leq 10^6, 1 leq m leq 100$。

    解法:我们注意到只有恒等置换对应的路径才可能不相交。因此,Lindström–Gessel–Viennot引理所求的行列式即为答案。其中,

    $$e(s_i, t_j) = egin{cases} inom{b_j-a_i+n-1}{b_j-a_i} & b_j geq a_i, & \ 0 & ext{otherwise}. end{cases} $$

    时间复杂度为$O(n+m^3)$。

    例题2

    2021牛客暑期多校训练营9 C. Cells

    有$n$个起点$s_i = (0, a_i)$,以及对应的$n$个终点$t_i = (i, 0)$,其中$1 leq i leq n$,满足$0 leq a_1 < a_2 < dots < a_n leq 10^6$。每一步可以从$(x, y)$走到$(x+1,y)$或者$(x,y-1)$。求两两不相交的路径$n$元组的数量。$1 leq n leq 5 cdot 10^5$。

    解法:思路同例题1。但最后列出的行列式是$n imes n$阶的矩阵,直接$O(n^3)$Gauss消元是不能接受的。注意到,

    $$e(s_i, t_j) = inom{a_i+j}{j} = frac{(a_i+j)!}{j!a_i!}.$$

    所求行列式可化为

    $$ egin{aligned} left| egin{matrix} e(s_1, t_1) & e(s_1, t_2) & dots & e(s_1, t_n) \ e(s_2, t_1) & e(s_2, t_2) & dots & e(s_2, t_n) \ vdots & vdots & ddots & vdots \ e(s_n, t_1) & e(s_n, t_2) & dots & e(s_n, t_n) end{matrix} ight| & = left| egin{matrix} frac{(a_1+1)!}{1!a_1!} & frac{(a_1+2)!}{2!a_1!} & dots & frac{(a_1+n)!}{n!a_1!} \ frac{(a_2+1)!}{1!a_2!} & frac{(a_2+2)!}{2!a_2!} & dots & frac{(a_2+n)!}{n!a_2!} \ vdots & vdots & ddots & vdots \ frac{(a_n+1)!}{1!a_n!} & frac{(a_n+2)!}{2!a_n!} & dots & frac{(a_n+n)!}{n!a_n!} end{matrix} ight| =  left(prod_{i=1}^n frac{(a_i+1)!}{ i! a_i! } ight) left| egin{matrix} 1 & a_1+2 & dots & (a_1+2) dots (a_1+n) \ 1 & a_2+2 & dots & (a_2+2) dots (a_2+n) \ vdots & vdots & ddots & vdots \ 1 & a_n+2 & dots & (a_n+2) dots (a_n+n) \ end{matrix} ight| \ & = left(prod_{i=1}^n frac{a_i+1}{i!} ight) left| egin{matrix} 1 & a_1 & dots & a_1^{n-1} \ 1 & a_2 & dots & a_2^{n-1} \ vdots & vdots & ddots & vdots \ 1 & a_n & dots & a_n^{n-1} \ end{matrix} ight|  = left(prod_{i=1}^n frac{a_i+1}{i!} ight) left( prod_{1 leq i < j leq n} (a_i-a_j) ight) end{aligned} $$

    提出系数之后,可化为一个Vandermonde行列式,而最后我们要求$a_i$两两之差之积。我们注意到$0 leq a_i leq 10^6 =: W$,从而$-10^6 leq a_i-a_j leq 10^6$,因此可以考虑使用FFT优化卷积$O(W log W)$统计不同可能的差的数量即可。

    例题3

    CodeForces 167E. Wizards and Bets

    给定$n$个节点,$m$条边的有向无环图$G = (V, E)$,其有恰好$k$个无入边以及$k$个无出边的点,按照他们的编号从小到大排序后依次为$s_1, s_2, dots, s_k$以及$t_1, t_2, dots, t_k$。求【对应置换是偶置换的路径$k$元组的数量】与【对应置换是奇置换的路径$k$元组的数量】之差。$n leq 600, m leq 10^5$。

    解法:令每条边的权值$w(x, y) = 1$,则Lindström–Gessel–Viennot引理所求的行列式即为答案。所有的$e(s_i, t_j)$(即从$s_i$走到$t_j$的路径的数量)可以在$O(nm)$的时间内求出。于是总的时间复杂度为$O(n^3+nm)$。

    例题4

    NOI2021 Day1 2. 路径交点

    给定一个$k$层的有向无环图,其中第$i(1 leq i leq k)$层有$n_i$个节点,且$n_1 = n_k$。从第$i$层走向第$i+1$层的边$(x_1, y_1)$与$(x_2, y_2)$ $(x_1 eq x_2, y_1 eq y_2)$存在一个交点,当且仅当$(x_1-x_2)(y_1-y_2) < 0$。一个从第$1$层节点走向第$k$层节点的(不在顶点相交的)路径$n_1$元组的交点个数为其中任意两条路径的交点个数之和。求【交点个数为偶数的路径$n_1$元组的数量】与【交点个数为奇数的路径$n_1$元组的数量】之差。$k leq 100, n_i leq 200$。

    解法:注意到【交点个数为偶数的路径$n_1$元组】即为【对应置换是偶置换的路径$n_1$元组】。问题直接化为例题3。时间复杂度为$O(k n^3)$,其中$n = max{n_1,n_2,dots,n_k}$。

    例题5

    AtCoder Beginner Contest 216 H. Random Robots

    给定$k$个起点$(0, x_1), (0, x_2), dots, (0, x_k)$,满足$0 leq x_1 < x_2 < dots < x_k leq 1000$。每一步可以从$(x, y)$走到$(x+1,y)$或者$(x+1,y+1)$。求终点在$(n, y_i)$的所有不相交的路径$k$元组的数量。$1 leq k leq 10, 1 leq n leq 1000$。

    解法:

    根据Lindström–Gessel–Viennot引理,我们枚举所有可能的终点,记$x_i = (0, x_i)$以及$y_i = (n, y_i)$,将其求和得

    $$ sum_{0 leq y_1 < y_2 < dots < y_k leq x_k+n} left| egin{matrix} e(x_1, y_1) & e(x_1, y_2) & dots & e(x_1, y_n) \ e(x_2, y_1) & e(x_2, y_2) & dots & e(x_2, y_n) \ vdots & vdots & ddots & vdots \ e(x_n, y_1) & e(x_n, y_2) & dots & e(x_n, y_n) end{matrix} ight|. $$

    其中,$e(x_i, y_j) = egin{cases} inom{n}{y_j-x_i} & 0 leq y_j-x_i leq n \ 0 & ext{otherwise} end{cases}$。

    令$S = {s_1, s_2, dots, s_{|S|}} subseteq [k]$,$b$为终点$y_i$的上界,定义

    $$ f(S, b) = sum_{0 leq y_1 < y_2 < dots < y_{|S|} leq b} left| egin{matrix} e(x_{s_1}, y_1) & e(x_{s_1}, y_2) & dots & e(x_{s_1}, y_{|S|}) \ e(x_{s_2}, y_1) & e(x_{s_2}, y_2) & dots & e(x_{s_2}, y_{|S|}) \ vdots & vdots & ddots & vdots \ e(x_{s_{|S|}}, y_1) & e(x_{s_{|S|}}, y_2) & dots & e(x_{s_{|S|}}, y_{|S|}) end{matrix} ight|. $$

    于是所求为$f([k], x_k + n)$。

    我们可以考虑$f(S, b)$可以从哪些状态推得。

    1. $y_{|S|} < b$,此时来自的状态是$f(S, b-1)$。

    2. $y_{|S|} = b$,此时所求为

    $$ sum_{0 leq y_1 < y_2 < dots < y_{|S|-1} < y_{|S|} = b} left| egin{matrix} e(x_{s_1}, y_1) & e(x_{s_1}, y_2) & dots & e(x_{s_1}, y_{|S|}) \ e(x_{s_2}, y_1) & e(x_{s_2}, y_2) & dots & e(x_{s_2}, y_{|S|}) \ vdots & vdots & ddots & vdots \ e(x_{s_{|S|}}, y_1) & e(x_{s_{|S|}}, y_2) & dots & e(x_{s_{|S|}}, y_{|S|}) end{matrix} ight| = sum_{i=1}^{|S|} (-1)^{|S|+i} e(x_{s_i}, y_{|S|}) left| egin{matrix} e(x_{s_1}, y_1) & e(x_{s_1}, y_2) & dots & e(x_{s_1}, y_{|S|-1}) \ vdots & vdots & cdots & vdots \ e(x_{s_{i-1}}, y_1) & e(x_{s_{i-1}}, y_2) & dots & e(x_{s_{i-1}}, y_{|S|-1}) \ e(x_{s_{i+1}}, y_1) & e(x_{s_{i+1}}, y_2) & dots & e(x_{s_{i+1}}, y_{|S|-1}) \ vdots & vdots & cdots & vdots \ e(x_{s_{|S|}}, y_1) & e(x_{s_{|S|}}, y_2) & dots & e(x_{s_{|S|}}, y_{|S|-1}) end{matrix} ight| = sum_{i=1}^{|S|} (-1)^{|S|+i} e(x_{s_i}, y_{|S|}) f(Ssetminus{s_i}, b-1). $$

    综上,动态规划方程为

    $$ f(S, b) = f(S, b-1) + sum_{i=1}^{|S|} (-1)^{|S|+i} e(x_{s_i}, b) f(Ssetminus{s_i}, b-1). $$

    边界条件为$f(S, -infty) = [S = emptyset]$。

    时间复杂度为$O(k2^k(n+x_k))$。

  • 相关阅读:
    在SQLite中使用索引优化查询速度
    SQLite支持的SQL数据操作
    left (outer) join , right (outer) join, full (outer) join, (inner) join, cross join 区别
    深入理解Android内存管理原理(六)
    Merge Sorted Array
    Sort Colors
    Construct Binary Tree from Preorder and Inorder Traversal
    Binary Tree Postorder Traversal
    Symmetric Tree
    Rotate Image
  • 原文地址:https://www.cnblogs.com/TinyWong/p/15220712.html
Copyright © 2011-2022 走看看