// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
#define prev ___prev
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 5e5 + 5;
int n, f[N][2][3], c[N], prev[2][3];
vector<int> G[N];
int sumOff[N];
// f[u][j][k] : min values to set all subtrees of u, j is state of u after having done,
// k is number of children that in important path
void dfs(int u, int par) {
sumOff[u] = (c[u] == 0);
for(int v : G[u]) if (v != par) {
dfs(v, u);
sumOff[u] += sumOff[v];
}
if (sumOff[u] == 0) return; // useless
REP(k, 3) f[u][c[u] ^ 1][k] = 1; // base string
for(int v : G[u]) if (v != par && sumOff[v] > 0) {
REP(j, 2) REP(k, 3) {
prev[j][k] = f[u][j][k];
f[u][j][k] = inf;
}
REP(j, 2) {
// first edge doesn't affect to u, v and answer
minimize(f[u][j][0], prev[j^1][0] + f[v][1][0] + 1); // u -> v -> [u]
minimize(f[u][j][0], prev[j][0] + f[v][0][0] + 3); // u -> v -> [u -> v -> u]
minimize(f[u][j][1], prev[j][0] + f[v][1][1]); // v -> u
minimize(f[u][j][1], prev[j^1][0] + f[v][0][1] + 2); // v -> u -> [v -> u]
minimize(f[u][j][1], prev[j^1][1] + f[v][1][0] + 1); // u -> v -> [u]
minimize(f[u][j][1], prev[j][1] + f[v][0][0] + 3); // u -> v -> [u -> v -> u]
minimize(f[u][j][2], prev[j^1][0] + f[v][1][2] + 3); // v -> u -> [v -> u -> v]
minimize(f[u][j][2], prev[j][0] + f[v][0][2] + 1); // v -> u -> [v]
minimize(f[u][j][2], prev[j][1] + f[v][1][1]); // u -> v
minimize(f[u][j][2], prev[j^1][1] + f[v][0][1] + 2); // u -> v -> [u -> v]
minimize(f[u][j][2], prev[j^1][2] + f[v][1][0] + 1); // u -> v -> [u]
minimize(f[u][j][2], prev[j][2] + f[v][0][0] + 3); // u -> v -> [u -> v -> u]
}
REP(j, 2) {
minimize(f[u][j][1], f[u][j][0]);
minimize(f[u][j][2], f[u][j][1]);
}
}
}
void init(void) {
cin >> n;
FOR(i, 1, n) {
char x; cin >> x;
c[i] = (x == '1');
}
FOR(i, 2, n) {
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
}
void process(void) {
memset(f, 0x3f, sizeof f);
FOR(i, 1, n) if (c[i] == 0) {
dfs(i, -1);
cout << f[i][1][2] << '\n';
break;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
svjetlo.cpp: In function 'int main()':
svjetlo.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
svjetlo.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |