Problem

Description

You are given a binary string SS of length NN.

Construct a directed graph based on this sequence: for any pair of indices (i,j)(i, j), if Si=0S_i = 0, Sj=1S_j = 1, and i<ji < j, add a directed edge from node ii to node jj.

这个图有很多性质:

  • VC:前缀 0 + 后缀 1,这个数量的最小值就是 MVC
  • IS:前缀 1 + 后缀 0

Initially, each node ii has a score of WiW_i, represented by the array W=[W1,W2,,WN]W = [W_1, W_2, \dots, W_N]. You can perform the following two types of operations any number of times:

  1. Choose a node ii satisfying Si=0S_i = 0: Node ii gives exactly 1 point to each of its adjacent nodes. As a result, the score of node ii decreases by its out-degree, and the score of each of its out-neighbors increases by 1.
  2. Choose a node jj satisfying Sj=1S_j = 1: Node jj takes exactly 1 point from each of its adjacent nodes. As a result, the score of node jj increases by its in-degree, and the score of each of its in-neighbors decreases by 1.

In other words, the flow of points follows the direction of the edges in the graph.

Determine whether there exists a sequence of operations such that ultimately every node has a score of exactly 0 (i.e., Wi=0W_i = 0 holds for all ii).

Constraints

  • 2N1062 \le N \le 10^6
  • 109Wi109-10^9 \le W_i \le 10^9

给定一个长度为 NN 的 01 字符串 SS

根据该序列构造一个有向图:对于任意 (i,j)(i, j),如果 Si=0S_i = 0Sj=1S_j = 1,且 i<ji < j,则添加一条从 ii 指向 jj 的有向边。

初始时,每个节点 ii 都有一个分数值 WiW_i,由数组 W=[W1,W2,,WN]W = [W_1, W_2, \dots, W_N] 表示。你可以执行以下两种操作任意次:

  1. 选择一个满足 Si=0S_i = 0 的节点 ii:节点 ii 向其每个出边邻居各给予 1 分。
  2. 选择一个满足 Sj=1S_j = 1 的节点 jj:节点 jj 从其每个入边邻居各拿取 1 分。

换言之,分数流向就是图的箭头流向。

判断是否存在一种操作序列,使得最终每个节点的分数都恰好为 0(即对于所有 ii,均满足 Wi=0W_i = 0)。

Solution

特判:

  • 在第一个 0 之前的所有 1,初始分数 WiW_i 必须为 0。
  • 在最后一个 1 之后的所有 0,初始分数 WiW_i 必须为 0。
  • 总和 Wi\sum W_i 必须等于 0。

下面不妨设 S1=0,SN=1S_1=0,S_N=1

ViV_i 为节点 ii (Si=0S_i = 0) 的操作次数,Vj-V_j 为节点 jj (Sj=1S_j = 1) 的操作次数。边 iji \to j 上的流量为 ViVjV_i - V_j。每个节点的流量之和满足:

  • Sk=0S_k = 0Wk=j>k,Sj=1(VkVj)W_k = \sum_{j > k, S_j = 1} (V_k - V_j)
  • Sk=1S_k = 1Wk=i<k,Si=0(VkVi)W_k = \sum_{i < k, S_i = 0} (V_k - V_i)

fk=i=1kWif_k = \sum_{i=1}^k W_iWW 的前缀和。

c0(k)c_0(k) 为前缀 S[1k]S[1 \dots k] 中 0 的个数,c1(k)c_1(k) 为后缀 S[k+1N]S[k+1 \dots N] 中 1 的个数。

WW11 累加到 kk,所有内部的流量会相互抵消,只剩下跨越边界 kk 的流量。我们得到:

fk=i=1kWi=c1(k)ik,Si=0Vic0(k)j>k,Sj=1Vjf_k = \sum_{i=1}^k W_i = c_1(k) \sum_{i \leqslant k, S_i=0} V_i - c_0(k) \sum_{j>k, S_j=1} V_j

不妨假设 v1=0v_1 = 0(因为 VkV_k 的绝对大小并不重要,整体加上一个常数 CC 并不会改变流量 ViVjV_i - V_j,所以我们可以从左到右递推构造出一个相对数列 vkv_k),并设 aka_k 为直到下标 kk 为止所有 0 对应的 viv_i 之和 (ak=ik,Si=0via_k = \sum_{i \le k, S_i=0} v_i)。

通过代数变形,我们可以严格从左到右求解 vkv_k

  • 如果 Sk=0S_k = 0

    vk=c1(k)ak1+c0(k1)Wkfk1c0(k1)c1(k)v_k = \frac{c_1(k) a_{k-1} + c_0(k-1) W_k - f_{k-1}}{c_0(k-1) c_1(k)}

    ak=ak1+vka_k = a_{k-1} + v_k

  • 如果 Sk=1S_k = 1

    ak=ak1a_k = a_{k-1}

    vk=Wk+ak1c0(k)v_k = \frac{W_k + a_{k-1}}{c_0(k)}

如果在任何一步计算中,分子 不能被分母整除,说明 vkv_k 不是整数(无法用离散次数的操作达成),直接输出 NO

即使我们求出了整数序列 vkv_k,我们还必须保证操作次数是非负的(xi0,yj0x_i \ge 0, y_j \ge 0)。这隐含地要求每条边上的流量必须满足 ViVj0V_i - V_j \ge 0

因为我们已经证明了 ViVj=vivjV_i - V_j = v_i - v_j,所以我们必须保证对于所有的 Si=0S_i = 0 和所有的 Sj=1S_j = 1,都有 vivj0v_i - v_j \ge 0

这等价于检查:

mini:Si=0vimaxj:Sj=1vj\min_{i: S_i=0} v_i \ge \max_{j: S_j=1} v_j

如果该不等式成立,输出 YES;否则输出 NO