Language: C++

#include <bits/stdc++.h>
using namespace std;

/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
const long long mod = 1000000007;

const int mxN = 100010;
int dp[mxN][2][2][2];

vector<int> tree[mxN];
int par[mxN];
int ch[mxN];
vector<int> df[mxN];
vector<int> te[mxN];
int C[mxN];
pair<int,int> eds[mxN];

int solve(int e,bool wh,bool root,bool ret) {
	int p = -1;
	int u = e;
	if(!root) {
		u = wh?eds[e].first:eds[e].second;
		p = !wh?eds[e].first:eds[e].second;
	}
	par[u] = e;
	int & res = dp[e][wh][root][ret];
	if(res != -1) return res;
	res = 0;
	ch[u] = 0;
	for(int i = 0;i < (int)tree[u].size();i++) {
		int v = tree[u][i];
		if(v == p) continue;
		int got = solve(te[u][i],eds[te[u][i]].first == v,0,true);
		if(got != 0 || C[v] == 0) {
			ch[u] ^= 1;
			res += 2 + got;
			df[u].push_back(2+got-solve(te[u][i],eds[te[u][i]].first == v,0,false)-1);
			while(df[u].size() > 2) {
				if(df[u].back() > df[u][df[u].size()-3]) {
					swap(df[u].back(),df[u][df[u].size()-3]);
				}
				if(df[u].back() > df[u][df[u].size()-2]) {
					swap(df[u].back(),df[u][df[u].size()-2]);
				}
				df[u].pop_back();
			}
		}
	}
	if(res == 0) {
		res = C[u] == 0 && root;
	} else {
		int CurCol = C[u] ^ !(root) ^ ch[u];
		res += !CurCol;
		if(!ret) {
			res -= !CurCol;
			res += CurCol;
			int other = res - df[u][0];
			res = min(res,other);
		}
	}
	//	cerr<<res<<' '<<e<<' '<<wh<<' '<<root<<' '<<ret<<'\n';
	return res;
}

int solve2(int e,bool wh,bool root,bool ret) {
	int p = -1;
	int u = e;
	if(!root) {
		u = wh?eds[e].first:eds[e].second;
		p = !wh?eds[e].first:eds[e].second;
	}
	int & res = dp[e][wh][root][ret];
	if(res != -1) return res;
	res = 0;
	bool chcur = ch[u];
	int other_res = dp[par[u]][u == 0 || u == eds[par[u]].first][u == 0][ret];
	int olp = -1;
	if(u != 0) {
		olp = u == eds[par[u]].first?eds[e].second:eds[e].first;
	}
	res = other_res;
	if(C[u] == 0 && u == 0 && res == 1) res = 0;
	else if(df[u].size()) {
		int CurCol = C[u] ^ !(u == 0) ^ ch[u];
		res -= !CurCol;
		if(!ret) {
			res += !CurCol;
			res -= CurCol;
		}
	}
	vector<int> nsdf = df[u];
	if(e != par[u]) {
		if(u != 0) { // add oldp
			int got = solve2(par[u],eds[par[u]].first == olp,0,true);
			if(got != 0 || C[olp] == 0) {
				chcur ^= 1;
				res += 2 + got;
				nsdf.push_back(2+got-solve2(par[u],eds[par[u]].first == olp,0,false)-1);
				while(nsdf.size() > 2) {
					if(nsdf.back() > nsdf[nsdf.size()-3]) {
						swap(nsdf.back(),nsdf[nsdf.size()-3]);
					}
					if(nsdf.back() > nsdf[nsdf.size()-2]) {
						swap(nsdf.back(),nsdf[nsdf.size()-2]);
					}
					nsdf.pop_back();
				}
			}
		}
		{ // rem p
			int got = solve2(e,eds[e].first == p,0,true);
			if(got != 0 || C[p] == 0) {
				chcur ^= 1;
				res -= 2 + got;
				int dfr = 2+got-solve2(par[u],eds[par[u]].first == olp,0,false)-1;
				for(int i = 0;i < (int) nsdf.size();i++) if(nsdf[i] == dfr) {
					nsdf.erase(nsdf.begin()+i);
					break;
				}
			}
		}
	}
	if(res == 0) {
		res = C[u] == 0 && root;
	} else {
		int CurCol = C[u] ^ !(root) ^ ch[u];
		res += !CurCol;
		if(!ret) {
			res -= !CurCol;
			res += CurCol;
			int other = res - nsdf[0];
			res = min(res,other);
		}
	}
	return res;
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	memset(dp,-1,sizeof dp);

	int N,u,v;
	string s;
	cin>>N;
	for(int i = 1;i < N;i++) {
		cin>>u>>v;
		u--,v--;
		tree[u].push_back(v);
		tree[v].push_back(u);
		eds[i] = {u,v};
		te[u].push_back(i);
		te[v].push_back(i);
	}
	cin>>s;
	for(int i = 0;i < N;i++) C[i] = s[i] == 'B';
	int res = solve(0,1,1,0);
	for(int i = 0;i < N;i++) res = min(res,solve2(i,1,1,0))/*, cerr<<solve(i,1,1,0)<<' '<<i<<'\n';
	cout<<res<<'\n';
	return 0;
}


Compiler output:
a.cc:172:64: error: unterminated comment
  172 |         for(int i = 0;i < N;i++) res = min(res,solve2(i,1,1,0))/*, cerr<<solve(i,1,1,0)<<' '<<i<<'\n';
      |                                                                ^
a.cc: In function 'int main()':
a.cc:172:64: error: expected ';' at end of input
  172 |         for(int i = 0;i < N;i++) res = min(res,solve2(i,1,1,0))/*, cerr<<solve(i,1,1,0)<<' '<<i<<'\n';
      |                                                                ^
      |                                                                ;
a.cc:172:64: error: expected '}' at end of input
a.cc:151:12: note: to match this '{'
  151 | int main() {
      |            ^