Language: C++

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n,col[N],freq[N],ans,dp[N],ok;
vector<int>g[N];
bool vis[N];
char c[N];
queue<int>q;
void calc(int u){
	vis[u] = 1;
	int at = (g[u].size() + freq[u] + col[u])&1 ? 0 : 2,mn = 0,mn1 = 0;
	for(auto i : g[u])
		if(!vis[i]){
			calc(i);
			mn1 = max(mn1,dp[i]);
			if(mn1 > mn)swap(mn1,mn); 
		}
	dp[u] = mn + at;
	ok = max(ok,max(dp[u],mn + mn1 + ((g[u].size() + freq[u] + col[u])&1 ? 1 : -1)));
}
int //main(){
	freopen("readin.txt","r",stdin);
	scanf("%d",&n);
	for(int i = 1,a,b;i < n;i++){
		scanf("%d%d",&a,&b);
		g[--a].push_back(--b);
		g[b].push_back(a);
	}	
	scanf(" %s",c);
	for(int i = 0;i < n;i++){
		col[i] = c[i] == 'B';
		if(c[i] == 'B' && g[i].size() == 1)q.push(i);
	}
	while(!q.empty()){
		ans--;
		int x = q.front();
		q.pop();
		vis[x] = 1;
		for(auto i : g[x])
			if(!vis[i] && (++freq[i]) + 1 == g[i].size() && col[i])q.push(i);
	}
	if(!(ans - 1 + n))return puts("1"),0;
	int bad = 0;
	for(int i = 0;i < n;i++)bad+=!vis[i] && !((col[i] + g[i].size() + freq[i])&1);
	ans = 2*(n - 1 + ans) + bad;
	if(!ans)return !printf("0\n");
	for(int i = 0;i < n;i++)if(!vis[i])calc(i);
	printf("%d\n",ans - ok);
}

Compiler output:
a.cc:22:16: error: 'int freopen' redeclared as different kind of entity
   22 |         freopen("readin.txt","r",stdin);
      |                ^
In file included from /usr/include/c++/14/cstdio:42,
                 from /usr/include/c++/14/ext/string_conversions.h:45,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/stdio.h:271:14: note: previous declaration 'FILE* freopen(const char*, const char*, FILE*)'
  271 | extern FILE *freopen (const char *__restrict __filename,
      |              ^~~~~~~
a.cc:23:14: error: expected constructor, destructor, or type conversion before '(' token
   23 |         scanf("%d",&n);
      |              ^
a.cc:24:9: error: expected unqualified-id before 'for'
   24 |         for(int i = 1,a,b;i < n;i++){
      |         ^~~
a.cc:24:27: error: 'i' does not name a type
   24 |         for(int i = 1,a,b;i < n;i++){
      |                           ^
a.cc:24:33: error: 'i' does not name a type
   24 |         for(int i = 1,a,b;i < n;i++){
      |                                 ^
a.cc:29:14: error: expected constructor, destructor, or type conversion before '(' token
   29 |         scanf(" %s",c);
      |              ^
a.cc:30:9: error: expected unqualified-id before 'for'
   30 |         for(int i = 0;i < n;i++){
      |         ^~~
a.cc:30:23: error: 'i' does not name a type
   30 |         for(int i = 0;i < n;i++){
      |                       ^
a.cc:30:29: error: 'i' does not name a type
   30 |         for(int i = 0;i < n;i++){
      |                             ^
a.cc:34:9: error: expected unqualified-id before 'while'
   34 |         while(!q.empty()){
      |         ^~~~~
a.cc:42:9: error: expected unqualified-id before 'if'
   42 |         if(!(ans - 1 + n))return puts("1"),0;
      |         ^~
a.cc:44:9: error: expected unqualified-id before 'for'
   44 |         for(int i = 0;i < n;i++)bad+=!vis[i] && !((col[i] + g[i].size() + freq[i])&1);
      |         ^~~
a.cc:44:23: error: 'i' does not name a type
   44 |         for(int i = 0;i < n;i++)bad+=!vis[i] && !((col[i] + g[i].size() + freq[i])&1);
      |                       ^
a.cc:44:29: error: 'i' does not name a type
   44 |         for(int i = 0;i < n;i++)bad+=!vis[i] && !((col[i] + g[i].size() + freq[i])&1);
      |                             ^
a.cc:45:9: error: 'ans' does not name a type
   45 |         ans = 2*(n - 1 + ans) + bad;
      |         ^~~
a.cc:46:9: error: expected unqualified-id before 'if'
   46 |         if(!ans)return !printf("0\n");
      |         ^~
a.cc:47:9: error: expected unqualified-id before 'for'
   47 |         for(int i = 0;i < n;i++)if(!vis[i])calc(i);
      |         ^~~
a.cc:47:23: error: 'i' does not name a type
   47 |         for(int i = 0;i < n;i++)if(!vis[i])calc(i);
      |                       ^
a.cc:47:29: error: 'i' does not name a type
   47 |         for(int i = 0;i < n;i++)if(!vis[i])calc(i);
      |                             ^
a.cc:48:15: error: expected constructor, destructor, or type conversion before '(' token
   48 |         printf("%d\n",ans - ok);
      |               ^
a.cc:49:1: error: expected declaration before '}' token
   49 | }
      | ^