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 <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector <vector <int>> g(n);
for (int i = 0; i + 1 < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector <int> c(n);
for (int i = 0; i < n; ++i) {
char r;
cin >> r;
c[i] = r - '0';
}
vector <vector <int>> dp(n, vector <int> (2));
function <void(int, int)> dfs = [&] (int u, int p) {
vector <int> gs;
for (auto v : g[u]) {
if (v != p) {
gs.push_back(v);
dfs(v, u);
}
}
if (c[u] == 0) {
for (auto v : gs) {
dp[u][1] += dp[v][1];
dp[u][0] = max(dp[u][0], dp[v][0]);
}
} else {
dp[u][0] = -1;
dp[u][1] = -1;
int mx = 0;
for (auto v : gs) {
dp[u][0] += dp[v][1];
dp[u][1] += dp[v][1];
mx = max(mx, dp[v][1]);
}
dp[u][0] = max(dp[u][0], 1 + mx);
dp[u][1] = max(dp[u][1], 1);
}
};
dfs(0, -1);
int ans = 0;
for (int i = 0; i < n; ++i) ans = max(ans, max(dp[i][0], dp[i][1]));
/*for (int i = 0; i < n; ++i) {
cout << dp[i][0] << ' ' << dp[i][1] << endl;
}*/
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |