#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 1, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';
#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILE "FILE"
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e18;
struct Modint{
int x;
Modint(){x = 0;}
Modint(int _x){ x = (_x % mod + mod) % mod; }
inline Modint operator + (const Modint &other) const{ return (x + other.x) % mod; }
inline Modint operator - (const Modint &other) const{ return (x - other.x + mod) % mod; }
inline Modint operator * (const Modint &other) const{ return (1LL * x * other.x) % mod; }
inline void operator += (const Modint &other){ *this = *this + other; }
inline void operator -= (const Modint &other){ *this = *this - other; }
inline void operator *= (const Modint &other){ *this = *this * other; }
friend ostream& operator << (ostream& os, const Modint &t){ return os << t.x; }
};
int n;
int dp[3][N];
string s;
vector<int> g[N];
void DFS(int u, int par){
dp[0][u] = 0;
if(s[u] == '1'){
dp[1][u] = 1;
}
int sum = 0, add = 0;
int w = (s[u] == '1');
FOA(v, g[u]){
if(v == par){
continue;
}
DFS(v, u);
sum += max(0LL, dp[1][v]);
}
FOA(v, g[u]){
if(v == par){
continue;
}
dp[1][u] = max(dp[1][u], sum - w);
dp[2][u] = max(dp[2][u], dp[1][v] + w);
}
}
signed main(){ quickly
if(fopen(FILE".inp", "r")){
freopen(FILE".inp", "r", stdin);
freopen(FILE".out", "w", stdout);
}
cin >> n;
FOR(i, 1, n - 1){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> s;
s = " " + s;
FOR(i, 1, n){
dp[0][i] = dp[1][i] = dp[2][i] = -oo;
}
DFS(1, -1);
int ans = 0;
FOR(i, 1, n){
ans = max({ans, dp[1][i], dp[2][i]});
}
cout << ans;
}
Compilation message (stderr)
power.cpp: In function 'int main()':
power.cpp:77:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(FILE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:78:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(FILE".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |