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 fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n");
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vv vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int inf = 2e09; ll infll = 2e18; int mod = 119<<23|1;
struct graph{
vector<vector<int>> g;
vector<int> dp, pp;
void init(int n){
g.resize(n+1), dp.resize(n+1), pp.resize(n+1);
}
void add_edge(int a, int b){ g[a].emplace_back(b), g[b].emplace_back(a); }
int result = 0;
void dfs(int x, int par){
int sum = 0;
for(int u : g[x]) if(u != par) dfs(u, x), sum += dp[u];
if(!pp[x]){ dp[x] = max(sum, 0), result = max(result, dp[x]); return; }
for(int u : g[x]) result = max(result, dp[u]+1);
dp[x] = max(sum-1, 1), result = max(result, dp[x]);
}
};
void answer(){
int n; scanf("%d", &n);
graph g; g.init(n);
for(int i = 0; i < n-1; ++i){
int a, b; scanf("%d%d", &a, &b);
g.add_edge(a, b);
}
char c = getchar_unlocked();
for(int i = 1; i <= n; ++i) c = getchar_unlocked(), g.pp[i] = c-'0';
g.dfs(1, 0);
printf("%d\n", g.result);
}
int main(){
int T = 1;
for(++T; --T; ) answer();
return 0;
}
Compilation message (stderr)
power.cpp: In function 'void answer()':
power.cpp:30:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | int n; scanf("%d", &n);
| ~~~~~^~~~~~~~~~
power.cpp:33:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | int a, b; scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |