This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
// _vector <int, 3> a(n, m, k);: int a[n][m][k].
// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
template <typename T>
struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <typename T>
T _min(T a) {
return a;
}
template <typename T, typename... Args>
T _min(T a, Args... args) {
return min(a, _min(args...));
}
}
const int MAX = 5e5 + 3;
const ll MOD[] = {1000000007, 998244353};
int n, sz[MAX];
string s;
vector <int> adj[MAX];
int dp[MAX][2][3];
int nDp[2][3];
void dfs(int u, int pre) {
sz[u] = s[u] == '0';
for (int &v : adj[u]) {
if (v != pre) {
dfs(v, u);
sz[u] += sz[v];
}
}
if (!sz[u]) return;
int x = s[u] - '0';
dp[u][x ^ 1][0] = dp[u][x ^ 1][1] = dp[u][x ^ 1][2] = 1;
for (int &v : adj[u]) {
if (v == pre || sz[v] == 0) continue;
memset(nDp, 0x0f, sizeof nDp);
auto &dp = ::dp[u];
auto &f = ::dp[v];
for (int i = 0; i < 2; i++) {
nDp[i][0] = _min(dp[i ^ 1][0] + f[1][0] + 1, dp[i][0] + f[0][0] + 3);
nDp[i][1] = _min(dp[i ^ 1][1] + f[1][0] + 1, dp[i ^ 1][0] + f[0][1] + 2,
dp[i][1] + f[0][0] + 3, dp[i][0] + f[1][1]);
nDp[i][2] = _min(dp[i][1] + f[1][1], dp[i ^ 1][1] + f[0][1] + 2,
dp[i ^ 1][2] + f[1][0] + 1, dp[i][2] + f[0][0] + 3,
dp[i ^ 1][0] + f[1][2] + 3, dp[i][0] + f[0][2] + 1);
nDp[i][1] = _min(nDp[i][1], nDp[i][0]);
nDp[i][2] = _min(nDp[i][2], nDp[i][1]);
}
swap(dp, nDp);
}
}
void Solve() {
cin >> n;
cin >> s;
s = '#' + s;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dp, 0x0f, sizeof dp);
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
dfs(i, 0);
cout << dp[i][1][2];
return;
}
}
cout << 0;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "TASK"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Compilation message (stderr)
svjetlo.cpp: In function 'int32_t main()':
svjetlo.cpp:104:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
svjetlo.cpp:105:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | 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... |