[BOJ 26194] Money Laundering
Task
Let $C:=\set{1,\cdots,c}$ and $P:=\set{1,\cdots,p}$ be the sets of companies and persons, respectively, where $1\leq c,p\leq 10^3$. For each company $i\in C$, let $k_i$ be the number of its direct owners, where
\[ 1\leq \sum_{i\in C} k_i \leq 10^4. \]
For each company $i\in C$, let $a_{i,x}$ be the ownership share of person $x\in P$ in company $i$, and let $b_{i,j}$ be the ownership share of company $j\in C$ in company $i$. If an owner is not explicitly given, its corresponding share is defined to be $0$. Every explicitly given share is positive, every direct owner of a company is distinct, and after dividing every percentage by $100$,
\[ 0\leq a_{i,x},b_{i,j}\leq1, \qquad \sum_{x\in P}a_{i,x}+ \sum_{j\in C}b_{i,j} = 1 \qquad (i\in C) \]
Companies are partitioned into industrial sectors, each containing fewer than $10$ companies. For companies $i,j$ belonging to different sectors, it is impossible that both $i$ owns a potentially indirect share of $j$ and $j$ owns a potentially indirect share of $i$.
Also, every company has at least one ultimate beneficial owner. In particular, all profit is eventually distributed to persons, so the infinite redistribution process below is guaranteed to converge.
Define $A:=(a_{i,x})\in\mathbb{R}_{\geq0}^{c\times p}$ and $B:=(b_{i,j})\in\mathbb{R}_{\geq0}^{c\times c}$. For $i\in C$, let $\b{e}_i\in\mathbb{R}^{1\times c}$ be the $i$-th unit row vector. If company $i$ initially has one unit of profit, then after $t$ redistributions, the amount remaining in the companies is $\b{e}_iB^t$, while the amount paid to the persons at the next redistribution is $\b{e}_iB^tA$.
Therefore, the matrix of ultimate ownership shares is
\[ U := \sum_{t=0}^{\infty}B^tA, \]
where $U_{i,x}$ is the ultimate ownership share of person $x$ in company $i$. We should compute every $U_{i,x}$ within absolute or relative error less than $10^{-4}$.
Solution
Consider the directed graph $G=(C,E)$, where
\[ E:=\Set{(i,j)\in C^2 \mid b_{i,j}>0}. \]
An edge $(i,j)$ means that a positive fraction of the profit of company $i$ is transferred to company $j$. By the sector condition, every strongly connected component of $G$ has size less than $10$.
Since the redistribution process converges, $\rho(B)<1$, and the Neumann series gives
\[ \sum_{t=0}^{\infty}B^t=(I-B)^{-1}. \]
Therefore,
\[ \begin{align*} U &= \sum_{t=0}^{\infty}B^tA \nl &= (I-B)^{-1}A. \end{align*} \]
Equivalently, $(I-B)U=A$, or
\[ U=A+BU \]
For each $i\in C$ and $x\in P$, this is
\[ U_{i,x} = a_{i,x} + \sum_{j\in C}b_{i,j}U_{j,x} \]
Thus, the ultimate ownership vector of company $i$ is the sum of its direct person ownership vector and the ultimate ownership vectors of its company owners weighted by their shares.
Let $\mathcal{S}$ be the set of strongly connected components of $G$, and fix $S=\set{v_1,\cdots,v_s}\in\mathcal{S}$. Let $B_S:=(b_{i,j})_{i,j\in S}$ be the submatrix of $B$ induced by $S$, and define
\[ U_S := (U_{v_t,x})_{\substack{1\leq t\leq s\nl x\in P}}, \qquad A_S := (a_{v_t,x})_{\substack{1\leq t\leq s\nl x\in P}} \]
For $S,T\in\mathcal{S}$, let $B_{S,T}$ be the submatrix of $B$ consisting of rows in $S$ and columns in $T$. Restricting $U=A+BU$ to the rows corresponding to $S$ gives
\[ U_S = A_S + B_SU_S + \sum_{\substack{T\in\mathcal{S}\nl T\neq S}} B_{S,T}U_T \]
Hence,
\[ (I-B_S)U_S = A_S + \sum_{\substack{T\in\mathcal{S}\nl T\neq S}} B_{S,T}U_T \]
If $S\to T$ is an edge of the condensation graph, then $U_S$ depends on $U_T$. Therefore, processing the strongly connected components in reverse topological order makes every $U_T$ on the right-hand side already known.
Since $\rho(B_S)<1$, $I-B_S$ is nonsingular, and therefore
\[ U_S = (I-B_S)^{-1} \left( A_S + \sum_{\substack{T\in\mathcal{S}\nl T\neq S}} B_{S,T}U_T \right) \]
Thus, each strongly connected component can be solved independently by Gaussian elimination after all components reachable from it have been processed.
Let $d:=\sum_{i\in C}k_i$ be the total number of ownership relations, and let $h:=\max_{S\in\mathcal{S}}\card{S}<10$. Finding all strongly connected components takes $O(c+d)$ time. For a component of size $s$, Gaussian elimination with $p$ right-hand sides takes $O(s^3+s^2p)$ time, while all inter-component ownership contributions can be accumulated in $O(dp)$ time.
Since
\[ \sum_{S\in\mathcal{S}}\card{S}=c, \qquad \sum_{S\in\mathcal{S}}\card{S}^2\leq hc, \qquad \sum_{S\in\mathcal{S}}\card{S}^3\leq h^2c, \]
the total time complexity is
\[ O\left( c+d+dp+hcp+h^2c \right) \]
Since $h<10$ is bounded by a constant, the problem can be solved in $O\left(p(c+d)\right)$ time and $O(cp+d)$ memory.