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 <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];
int ans = 1;
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 += dp[g[node][i]];
maxi = max(maxi, dp[g[node][i]]);
}
if(power_plants[node - 1] == '0') {
dp[node] = sum;
ans = max(ans, sum);
}
else {
dp[node] = max(1LL, sum - 1);
ans = max(ans, sum - 1);
ans = max(ans, maxi + 1);
dp[node] = max(dp[node], maxi - 1);
}
//cout << "node = " << node << ", dp[node][0] = " << dp[node][0] << ", dp[node][1] = " << dp[node][1] << "\n";
}
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 << ans << "\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 (stderr)
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++) {
| ~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |