#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 222222;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> g[N];
int dp[N][2];
bool lamp[N];
void dfs(int v, int p){
dp[v][1] = lamp[v];
int sum = (lamp[v] ? -1 : 0);
for (int u : g[v]){
if (u == p) continue;
dfs(u, v);
if (lamp[u]){
sum += max({1, dp[u][0], dp[u][1] - lamp[u]});
} else sum += dp[u][0];
dp[v][0] = max({dp[v][0], dp[u][0], dp[u][1]});
dp[v][1] = max({dp[v][1], dp[u][0] + 1, dp[u][1]});
}
dp[v][0] = max(dp[v][0], sum - lamp[v]);
if (!lamp[v]) dp[v][1] = 0;
}
void solve(){
int n;
cin >> n;
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
string s;
cin >> s;
for (int i = 0; i < n; i++) lamp[i + 1] = s[i] == '1';
dfs(1, 1);
//for (int i = 1; i <= n; i++) cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << endl; cout << endl;
cout << max(dp[1][0], dp[1][1]);
}
int main(){
//freopen("output.txt", "w", stdout);
int tests = 1;
//cin >> tests;
while (tests--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5452 KB |
Output is correct |
2 |
Correct |
4 ms |
5660 KB |
Output is correct |
3 |
Correct |
5 ms |
5452 KB |
Output is correct |
4 |
Correct |
4 ms |
5532 KB |
Output is correct |
5 |
Incorrect |
4 ms |
5532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5452 KB |
Output is correct |
2 |
Correct |
4 ms |
5660 KB |
Output is correct |
3 |
Correct |
5 ms |
5452 KB |
Output is correct |
4 |
Correct |
4 ms |
5532 KB |
Output is correct |
5 |
Incorrect |
4 ms |
5532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5452 KB |
Output is correct |
2 |
Correct |
4 ms |
5660 KB |
Output is correct |
3 |
Correct |
5 ms |
5452 KB |
Output is correct |
4 |
Correct |
4 ms |
5532 KB |
Output is correct |
5 |
Incorrect |
4 ms |
5532 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |