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>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int(x.size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
int main(){
int n;
scanf("%d", &n);
vector<vector<int>> g(n+1);
REP(i, n-1){
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
vector<int> czy(n+1);
scanf("\n");
FOR(i, 1, n){
char c;
scanf("%c", &c);
czy[i] = c=='1';
}
vector<int> oj(n+1);
vector<int> dp = czy;
function<void(int, int)> dfs = [&](int w, int o){
int t = -czy[w];
for(int i : g[w]) if(i != o) oj[i] = w, dfs(i, w), t += dp[i];
dp[w] = max(dp[w], t);
};
dfs(1, 0);
int wyn = dp[1];
FOR(i, 2, n) wyn = max(wyn, dp[i]+czy[oj[i]]);
printf("%d", wyn);
return 0;
}
Compilation message (stderr)
power.cpp: In function 'int main()':
power.cpp:10:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
power.cpp:14:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
power.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("\n");
| ~~~~~^~~~~~
power.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%c", &c);
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |