21614_Tree

2022-5-16 18:19| 发布者: Hocassian| 查看: 26| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505691094800-Problem List-采集的数据-后羿采集器.html

Pro.ID

21614

Title

Tree

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21614

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    Given a tree with n nodes, each of which is colored black or white, you are allowed to swap any two nodes at a time, now your task is to make those black nodes connected to each other, using as few swaps as possible.

    输入

    Input may contain several test cases. The first line is a positive integer T, the number of test cases below. For each test case, the first line is a positive integer n, ( 1 ≤ n ≤ 100 ) the number of nodes in the tree. The nodes are numbered from 1 to n. The second line consists of n letters ( 'b' or 'w' ) separated by spaces, denoting the color of node 1, 2, ..., n. From the 3rd line to the n+2-th line are the descriptions of n-1 edges in the tree. Each line contains two integer x, y, ( 1 ≤ x, yn ), denoting edge (x, y)

    输出

    Description

    Given a tree with n nodes, each of which is colored black or white, you are allowed to swap any two nodes at a time, now your task is to make those black nodes connected to each other, using as few swaps as possible.

    Input

    Input may contain several test cases. The first line is a positive integer T, the number of test cases below. For each test case, the first line is a positive integer n, ( 1 ≤ n ≤ 100 ) the number of nodes in the tree. The nodes are numbered from 1 to n. The second line consists of n letters ( 'b' or 'w' ) separated by spaces, denoting the color of node 1, 2, ..., n. From the 3rd line to the n+2-th line are the descriptions of n-1 edges in the tree. Each line contains two integer x, y, ( 1 ≤ x, yn ), denoting edge (x, y)

    Output

    For each test case, output the minimum number of swaps to make black nodes connected to each other.

    Sample Input

    1
    4
    b b b w
    1 4
    2 4
    3 4

    Sample Output

    1

    Source

    样例输入

    1
    4
    b b b w
    1 4
    2 4
    3 4

    样例输出

    1

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部