#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];
ll dp[MAX][2][3];
ll 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
svjetlo.cpp: In function 'int32_t main()':
svjetlo.cpp:102:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
svjetlo.cpp:103:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
37468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
176 ms |
80808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
55408 KB |
Output is correct |
2 |
Correct |
276 ms |
60064 KB |
Output is correct |
3 |
Correct |
241 ms |
60700 KB |
Output is correct |
4 |
Correct |
142 ms |
56692 KB |
Output is correct |
5 |
Correct |
163 ms |
60464 KB |
Output is correct |
6 |
Incorrect |
133 ms |
57868 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
37468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |