/* * In the name of GOD */
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define F first
#define S second
#define mk make_pair
const int N = 201234;
vector <int> g[N];
int dp[N][2];
bool f[N];
void dfs (int v, int p = -1) {
dp[v][0] = 0;
dp[v][1] = 0;
if (f[v]) {
dp[v][1] = 1;
}
int sum1 = 0;
for (int u : g[v]) {
if (u != p) {
dfs (u, v);
dp[v][0] = max(dp[u][0], dp[v][0]);
if (f[v])
dp[v][0] = max(dp[u][1] + 1, dp[v][0]);
sum1 += dp[u][1];
}
}
if (f[v]) {
dp[v][0] = max(dp[v][0], sum1 - 1);
dp[v][1] = max(dp[v][1], sum1 - 1);
} else {
dp[v][1] = max(dp[v][1], sum1);
dp[v][0] = max(dp[v][0], sum1);
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
if (n > 2000)
return 0;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
f[i] = (c - '0');
}
int mx = 0;
for (int i = 1; i <= n; i++) {
if (!f[i])
continue;
dfs (i);
for (int j : g[i]) {
mx = max(mx, dp[j][1] + 1);
}
}
cout << mx << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |