#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
string power_plants;
const int N = 2e5 + 5;
vector<int> g[N];
int dp[N][2];
int ans = 0;
void dfs(int node, int par) {
int sum = 0, maxi = 0;
for(int i = 0; i < g[node].size(); i++) {
if(g[node][i] == par) continue;
dfs(g[node][i], node);
sum += max(dp[g[node][i]][0], dp[g[node][i]][1] - 2);
maxi = max({maxi, dp[g[node][i]][0], dp[g[node][i]][1] - 2});
}
if(power_plants[node - 1] == '0') {
dp[node][0] = sum;
dp[node][1] = 0;
}
else {
dp[node][1] = maxi + 1;
dp[node][0] = max(1LL, sum - 1);
}
}
void Solve() {
int n;
cin >> n;
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> power_plants;
dfs(1, -1);
cout << max({ans, dp[1][0], dp[1][1]}) << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
power.cpp: In function 'void dfs(long long int, long long int)':
power.cpp:15:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5020 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |