CF2219C.Coloring a Red Black Tree

提高+/省选-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree of $ n $ nodes numbered from 1 to $ n $ and a binary string $ s $ of length $ n $ where $ s_i = 1 $ means that the $ i $ -th node is colored red and $ s_i = 0 $ means that the $ i $ -th node is colored black. You are allowed to perform the following operation:

  • Select a node $ a $ ( $ 1 \le a \le n $ ).
  • A neighbor $ b $ of $ a $ is selected uniformly at random.
  • Recolor node $ a $ with the color of $ b $ . Note that this changes the color of the vertex you selected, not the one that was uniformly chosen.

You want to color all the nodes in the tree red by performing the operation any number of times (possibly zero). Please compute the minimum expected number of operations needed to do so if you perform them optimally. There will be at least one red node initially.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 1000 $ ). The description of the test cases follows.

The first line of each test case contains an integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ).

The second line contains a binary string $ s $ of size $ n $ .

The following $ n-1 $ lines each contain two integers, the $ i $ -th of them contains $ u_i $ and $ v_i $ ( $ 1 \le u_i, v_i \le n $ ), which means that there is an edge between nodes $ u_i $ and $ v_i $ .

It is guaranteed that the given graph is a tree, there is at least one red node, and the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output a single real number representing the minimum expected number of operations.

Your answer is considered correct if its absolute or relative error does not exceed $ 10^{-6} $ .

Formally, let your answer be $ x $ , and the jury's answer be $ y $ . Your answer is accepted if and only if $ \frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6} $ .

输入输出样例

  • 输入#1

    3
    3
    101
    1 2
    2 3
    5
    10010
    1 2
    2 3
    3 4
    2 5
    3
    010
    1 2
    2 3

    输出#1

    1.00000000000000000000
    4.50000000000000000000
    2.00000000000000000000

说明/提示

In the first test case, there is only one node colored in black, which has only two red colored neighbors, thus, if we first apply the operation to the second node, we will have a red colored tree in just one operation.

输入解题思路,AI测评打分。不知道怎么写?

首页