#include <bits/stdc++.h>
using namespace std;
#ifndef _LOCAL
//#pragma GCC optimize("O3,Ofast")
#else
#pragma GCC optimize("O0")
#endif
template<typename t> inline void umin(t &a, const t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, const t b) {a = max(a, b);}
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef int8_t byte;
ll time() {return chrono::system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
#define ft first
#define sd second
#define len(f) int((f).size())
#define bnd(f) (f).begin(), (f).end()
#define _ <<' '<<
const int inf = 1e9 + 5;
const ll inf64 = 4e18 + 5;
const int md = 998244353;
namespace MD {
void add(int &a, const int b) {if((a += b) >= md) a -= md;}
void sub(int &a, const int b) {if((a -= b) < 0) a += md;}
int prod(const int a, const int b) {return ll(a) * b % md;}
};
int n;
const int N = 5e5 + 5;
vector<int> g[N], h;
byte p[N];
bool unlit[N], mk[N];
int s[2][N], t[2][N], f[2][N], e[N], T[2][2][N], S[2][3][N], rT[2][2][N];
int min(int a, int b, int c) {return min(a, min(b, c));}
int min(int a, int b, int c, int d) {return min(min(a, d), min(b, c));}
void solve() {
cin >> n;
#ifdef KEK
memset(mk, 0, N);
for(int i = 0; i < n; ++i)
g[i].clear();
#endif
for(int i = 0; i < n; ++i) {
char c; cin >> c;
p[i] = c == '0';
e[i] = 0;
}
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
++e[u], ++e[v];
}
queue<int> q;
for(int i = 0; i < n; ++i)
if(e[i] == 1) q.push(i);
int cnt = 0;
for(;;) {
++cnt;
assert(!q.empty());
int v = q.front();
q.pop();
mk[v] = true;
unlit[v] = p[v];
h.clear();
for(int u : g[v]) {
if(--e[u] == 1)
q.push(u);
if(mk[u] && unlit[u]) {
unlit[v] = true;
h.push_back(u);
}
}
if(!unlit[v]) continue;
int k = len(h);
f[0][v] = +inf;
f[1][v] = 1;
for(int u : h) {
int f0 = min(f[1][v] + f[0][u] + 1,
f[0][v] + f[1][u] + 3);
int f1 = min(f[0][v] + f[0][u] + 1,
f[1][v] + f[1][u] + 3);
f[0][v] = min(+inf, f0);
f[1][v] = min(+inf, f1);
}
for(int i : {0, 1}) for(int j : {0, 1})
fill(T[i][j], T[i][j] + k + 1, +inf);
T[1][0][0] = 1;
for(int i = 0; i < k; ++i) {
for(int p : {0, 1}) {
for(int z : {0, 1})
for(int ff : {0, 1})
umin(T[p ^ ff ^ 1][z][i + 1],
T[p][z][i] + f[ff][h[i]] + 1 + (ff << 1));
for(int e : {0, 1})
umin(T[p ^ e][1][i + 1],
T[p][0][i] + t[e][h[i]] + (e << 1));
}
}
t[0][v] = min(T[0][0][k], T[0][1][k], f[0][v]);
t[1][v] = min(T[1][0][k], T[1][1][k], f[1][v]);
for(int i : {0, 1}) for(int j : {0, 1, 2})
fill(S[i][j], S[i][j] + k + 1, +inf);
S[1][0][0] = 1;
for(int i = 0; i < k; ++i) {
for(int p : {0, 1}) {
for(int z : {0, 1, 2})
for(int ff : {0, 1})
umin(S[p ^ ff ^ 1][z][i + 1],
S[p][z][i] + f[ff][h[i]] + 1 + (ff << 1));
for(int z : {0, 1})
for(int e : {0, 1})
umin(S[p ^ e][z + 1][i + 1],
S[p][z][i] + t[e][h[i]] + (e << 1));
}
}
s[0][v] = min(S[0][0][k], S[0][1][k], S[0][2][k], t[0][v]);
s[1][v] = min(S[1][0][k], S[1][1][k], S[1][2][k], t[1][v]);
for(int i : {0, 1}) for(int j : {0, 1})
fill(rT[i][j], rT[i][j] + k + 1, +inf);
rT[0][0][0] = 1;
for(int i = 0; i < k; ++i) {
for(int p : {0, 1}) {
for(int z : {0, 1})
for(int ff : {0, 1})
umin(rT[p ^ ff ^ 1][z][i + 1],
rT[p][z][i] + f[ff][h[k - 1 - i]] + 1 + (ff << 1));
for(int e : {0, 1})
umin(rT[p ^ e][1][i + 1],
rT[p][0][i] + t[e][h[k - 1 - i]] + (e << 1));
}
}
for(int i = 0; i < k; ++i) {
for(int fi : {0, 1}) {
for(int se : {0, 1}) {
ll v = s[1][h[i]];
v += T[fi][0][i];
v += rT[se][0][k - 1 - i];
if(v > +inf) continue;
umin(s[fi ^ se][v], int(v));
}
}
}
if(p[v]) {
swap(f[0][v], f[1][v]);
swap(t[0][v], t[1][v]);
swap(s[0][v], s[1][v]);
}
if(cnt == n) {
cout << s[0][v] << endl;
return;
}
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef _LOCAL
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
#else
system("color a");
freopen("in.txt", "r", stdin);
int t; cin >> t;
while(t--)
#endif
solve();
}
Compilation message
svjetlo.cpp:33:1: error: reference to 'byte' is ambiguous
33 | byte p[N];
| ^~~~
In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:45,
from svjetlo.cpp:1:
/usr/include/c++/9/cstddef:69:14: note: candidates are: 'enum class std::byte'
69 | enum class byte : unsigned char {};
| ^~~~
svjetlo.cpp:13:16: note: 'typedef int8_t byte'
13 | typedef int8_t byte;
| ^~~~
svjetlo.cpp: In function 'void solve()':
svjetlo.cpp:49:9: error: 'p' was not declared in this scope
49 | p[i] = c == '0';
| ^
svjetlo.cpp:70:20: error: 'p' was not declared in this scope
70 | unlit[v] = p[v];
| ^