oj mrJudge
Toggle navigation
  • Login
    • Forget Password
      Login
User Image

Hello, Stranger

Guest
  • Analysis Mode
  • Problems
    • All Problems
    • Latest Problems
  • Join Us Now
  • Registration
  • Contact Us
  • Infomation
  • About
    • Terms of Use
    • Technical Specifications
    • Credits

monochromecat Batch , stdin/stdout

Registered Users Only

Please login to utilize this feature.

Do note that this website only supports submissions in C++.

monochromecat.html

Monochrome Cat

Problem Statement

There is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex xi and yi. Each vertex is painted white or black. The initial color of Vertex i is represented by a letter ci. ci = W represents the vertex is white; ci = B represents the vertex is black.

A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly:

  • Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex.
  • Invert the color of the vertex where she is currently.

The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective?

Constraints

  • 1 ≤ N ≤ 105
  • 1 ≤ xi,yi ≤ N (1 ≤ i ≤ N-1)
  • The given graph is a tree.
  • ci = W or ci = B.

Subtask 1 (10%): N ≤ 10

Subtask 2 (90%): No additional constraints

Subtask 3 (0%): Sample

Input

Input is given from Standard Input in the following format:

N
x1 y1
x2 y2
:
xN-1 yN-1
c1c2..cN

Output

Print the minimum number of seconds required to achieve the objective.

Sample Input 1

5
1 2
2 3
2 4
4 5
WBBWW

Sample Output 1

5

The objective can be achieved in five seconds, for example, as follows:

  • Start at Vertex 1. Change the color of Vertex 1 to black.
  • Move to Vertex 2, then change the color of Vertex 2 to white.
  • Change the color of Vertex 2 to black.
  • Move to Vertex 4, then change the color of Vertex 4 to black.
  • Move to Vertex 5, then change the color of Vertex 5 to black.

Sample Input 2

6
3 1
4 5
2 6
6 1
3 4
WWBWBB

Sample Output 2

7

Sample Input 3

1
B

Sample Output 3

0

Sample Input 4

20
2 19
5 13
6 4
15 6
12 19
13 19
3 11
8 3
3 20
16 13
7 14
3 17
7 8
10 20
11 9
8 18
8 2
10 1
6 13
WBWBWBBWWWBBWWBBBBBW

Sample Output 4

21

Tags

AtCoder, Graph Theory

Subtasks and Limits

Subtask Score #TC Time Memory Scoring
110451s256MBMinimum
2901181s256MBMinimum
3041s256MBMinimum

Judge Compile Command

g++-8 ans.cpp -o monochromecat -Wall -Wshadow -static -O2 -lm -m64 -s -w -std=gnu++17 -fmax-errors=512

Accepted Submissions

subIDUserTimeMax Time

Past Submissions

subIDUserTimeScore
mrJudge 09.05.20
Copyright © 2020 mrJudge. All rights reserved.