#include <bits/stdc++.h>
//ako ovaj vaso daso misli da me pobedjuje.....
using namespace std;
#define int long long
#define f first
#define s second
const int N = 2e5 + 69;
int n, mark[N], dp[N], up[N];
vector<int> g[N];
string s;
void dfs(int v, int p) {
int x = 0;
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
x += (dp[u] > 0);
}
sort(g[v].begin(), g[v].end(), [&](int i, int j) {
return dp[i] > dp[j];
});
if (mark[v]) {
if (x < 2) {
dp[v] = 1;
for (auto u : g[v]) {
if (u == p)
continue;
dp[v] = dp[u];
}
}
else {
for (auto u : g[v]) {
if (u == p)
continue;
dp[v] += dp[u];
}
dp[v]--;
}
}
else {
int mx = 0;
for (auto u : g[v]) {
if (u == p)
continue;
dp[v] += dp[u];
mx = max(mx, dp[u]);
}
dp[v] = max(dp[v], mx);
}
}
int ok;
void povecaj(int v, int p) {
if (ok)
return;
int x = 0;
for (auto u : g[v]) {
if (u == p)
continue;
x += (dp[u] > 0);
}
if (x >= 2)
ok = 1;
else {
for (auto u : g[v]) {
if (u == p)
continue;
if (mark[v] && dp[u] == dp[1]) {
dp[1]++;
ok = 1;
}
else if (dp[u] > 0)
povecaj(u, v);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> s;
for (int i = 0;i < n;i++) {
if (s[i] == '1')
mark[i + 1] = 1;
}
dfs(1, 0);
povecaj(1, 0);
cout << dp[1] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8840 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8840 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8840 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |