Codechef SUMCUBE

SUMCUBE code

给定无向简单图 G = (V, E)(即不存在自环和重边),以及 k = 1, 2, 或3 。

$$ \sum_{S \subseteq V} f(S)^k, $$
其中 $f(S)$ 是两个端点都在 S 中的边的数量,即
$$ f(S) = \frac 1 2 \sum_{x \in S} \sum_{y \in S} [(x, y) \in E]. $$

解:

我们注意到 k 的取值只有 1, 2, 3,因此我们针对每一种取值单独考虑。

为了方便,我们把 $[(x, y) \in E]$ 简记为 $e_{xy}$。
由于是无向图,因此有 $e_{xy} = e_{yx}$。
由于 G 是无自环,因此有 $e_{xx} = 0$。
我们把 $[x \in S]$ 简记为 $s_x$。
我们记 $d_x$ 为节点 x 的度数,具体定义为
$$ d_x = \sum_{y \in V} e_{xy}. $$
为了方便计算,我们只考虑$ 2^k \sum_{S \subseteq V} f(S)^k $。

当 k = 1 时,
$$ 2 \sum_{S \subseteq V} f(S) = \sum_{S \subseteq V} \sum_{x \in S} \sum_{y \in S} e_{xy}. $$
交换求和顺序可得
$$ \sum_{S \subseteq V} \sum_{x \in S} \sum_{y \in S} e_{xy} = \sum_{x \in V} \sum_{y \in V} e_{xy} \sum_{S \subseteq V} s_x s_y = 2^{|V|-2} \sum_{x \in V} \sum_{y \in V} e_{xy} = 2^{|V|-2} 2|E|. $$
为了方便,我们记 $c_{11} = \sum_{x \in V} \sum_{y \in V} e_{xy} = 2|E|$ 。
时间复杂度 O(|V|+|E|) 。

当 k = 2 时,
$$ 4 \sum_{S \subseteq V} f(S)^2 = \sum_{S \subseteq V} \sum_{x \in S} \sum_{y \in S} \sum_{x' \in S} \sum_{y' \in S} e_{xy} e_{x'y'}. $$
注意到
$$ \sum_{S \subseteq V} \sum_{x \in S} \sum_{y \in S} \sum_{x' \in S} \sum_{y' \in S} e_{xy} e_{x'y'} = 2^{|V|-2} 2 c_{22} + 2^{|V|-3} 4 c_{211} + 2^{|V|-4} c_{1111}, $$
其中
$$ c_{22} = c_{11}, $$
$$ c_{211} = \sum_{x \in V} \sum_{y \in V} \sum_{y' \in V \setminus \{x, y\}} e_{xy} e_{xy'} = \sum_{x \in V} d_x (d_x-1), $$
$$ c_{1111} = \sum_{x \in V} \sum_{y \in V} \sum_{x' \in V \setminus \{x, y\}} \sum_{y' \in S \setminus \{x, y\}} e_{xy} e_{x'y'} = 4\left( |E|^2 + |E| - \sum_{x \in V} d_x^2 \right). $$
时间复杂度 O(|V|+|E|) 。

当 k = 3 时,
$$ 8 \sum_{S \subseteq V} f(S)^3 = \sum_{S \subseteq V} \sum_{x \in S} \sum_{y \in S} \sum_{x' \in S} \sum_{y' \in S} \sum_{x'' \in S} \sum_{y'' \in S} e_{xy} e_{x'y'} e_{x''y''}. $$
注意到
$$ \begin{aligned}
8 \sum_{S \subseteq V} f(S)^3
= & 2^{|V|-2} 4 c_{33} + 2^{|V|-3} ( 24 c_{321} + 8 c_{222} ) + \\
& 2^{|V|-4} ( 8 c_{3111} + 6 c_{2211_0} + 24 c_{2211_1} ) + \\
& 2^{|V|-5} 12 c_{21111} + 2^{|V|-6} c_{111111}.
\end{aligned} $$

其中
$$ c_{33} = c_{11} $$
$$ c_{321} = c_{211} $$
$$ c_{222} = \sum_{x \in V} \sum_{y \in V} \sum_{z \in V} e_{xy} e_{yz} e_{zx} $$
$$ c_{3111} = \sum_{x \in V} \sum_{y \in V} \sum_{y' \in V \setminus \{x, y\}} \sum_{y'' \in V \setminus \{x, y, y'\}} e_{xy} e_{xy'} e_{xy''} = \sum_{x \in V} d_x(d_x-1)(d_x-2) $$
$$ c_{2211_0} = c_{1111} $$
$$ c_{2211_1} = \sum_{x \in V} \sum_{y \in V} \sum_{y' \in V \setminus \{x, y\}} \sum_{y'' \in V \setminus \{x, y\}} e_{xy} e_{xy'} e_{yy''} = 4|E|^2 - c_{222} $$
$$ \begin{aligned}
c_{21111}
& = \sum_{x \in V} \sum_{y \in V} \sum_{y' \in V \setminus \{x, y\}} \sum_{x'' \in V \setminus \{x, y, y'\}} \sum_{y'' \in V \setminus \{x, y, y'\}} e_{xy} e_{xy'} e_{x''y''} \\
& = (2|E|+4) c_{211} + 2 c_{222} - 2 \sum_{x \in V} d_x^2(d_x-1) - 4 \sum_{x \in V} \sum_{y \in V} e_{xy} d_y (d_x-1)
\end{aligned}
$$
$$ \begin{aligned}
c_{111111}
& = \sum_{x \in V} \sum_{y \in V} \sum_{x' \in V \setminus \{x, y\}} \sum_{y' \in V \setminus \{x, y\}} \sum_{x'' \in V \setminus \{x, y, x', y'\}} \sum_{y'' \in V \setminus \{x, y, x', y'\}} e_{xy} e_{x'y'} e_{x''y''} \\
& = (2|E|)^3 - ( 4 c_{33} + 24 c_{321} + 8 c_{222} + 8 c_{3111} + 6 c_{2211_0} + 24 c_{2211_1} + 12 c_{21111} + c_{111111} )
\end{aligned} $$

剩下的问题即是求解 $c_{222}$ 。求出 $c_{222}$ 之后,其余值皆可在 O(|V|+|E|) 时间内求出。
$c_{222}$ 本质上是求简单无向图 G 的有序三元环个数,其值是 6 倍简单无向图 G 的无序三元环的个数。
对于三元环,可以用以下算法求得。

0. 记 $t = \sqrt{|E|}$。
1. 我们按照节点度数把所有节点 x 分成两类。一类节点度数 $d_x \le t$,剩下不满足条件的为另一类。
2. 对于节点度数 $d_x \le t$ 的节点 x ,我们枚举其连接的两个不同节点 y 和 z。判断 $e_{yz}$ 是否为 1,若是,则找到一个三元环。
我们根据 y 和 z 的不同情况进行讨论。
2.1 若$d_y \le t$ 且 $d_z \le t$。对于这种情况,我们的算法会计算到这个三元环 3 次,因此每找到一次,贡献为 2 。
2.2 否则若$d_y \le t$ 或 $d_z \le t$。对于这种情况,我们的算法会计算到这个三元环 2 次,因此每找到一次,贡献为 3 。
2.3 否则,即$d_y > t$ 且 $d_z > t$。对于这种情况,我们的算法会计算到这个三元环 1 次,因此每找到一次,贡献为 6 。
3. 节点度数 $d_x > t$ 的节点,最多有$|E|/t$个,我们暴力枚举节点度数大于 t 的三个节点,判断他们是否组成三元环。对于这种情况,我们的算法会计算到这个三元环 1 次,因此每找到一次,贡献为 6 。

可以发现, $c_{222}$ 的计算是本问题的关键,时间复杂度为 $O(|E|^{1.5})$。

posted @ 2017-09-11 20:04  liouzhou_101  阅读(425)  评论(0编辑  收藏  举报