CC's

Back

You are given a tree (an undirected connected acyclic graph) consisting of n vertices and n−1 edges. A number is written on each edge, each number is either 0 (let’s call such edges 0-edges) or 1 (those are 1-edges).

Let’s call an ordered pair of vertices (x,y) (x≠y) valid if, while traversing the simple path from x to y, we never go through a 0-edge after going through a 1-edge. Your task is to calculate the number of valid pairs in the tree.

分析#

当经过一条1,就不能再经过0。所以路径只会有(称1为黑,0为白)

  • 0
  • 1
  • 从某处划分后一半为1一半为0

维护从点u到其子树任意节点的白色路径,黑色路径总数。那么答案就可以统计了。

  1. 从子树到自己的黑,白路径
  2. 子树到子树的黑,白路径
  3. u为分界点的黑白路径。包括子树到子树,子树到非子树。

子树到非子树的黑白节点已经包括在第1,2部分。

代码#

[CF 1156D] 0-1 Tree
https://astro-pure.js.org/blog/cf-1156d-0-1-tree
Author Cheng Chen
Published at 2020年1月21日
Comment seems to stuck. Try to refresh?✨