#include<bits/stdc++.h>
// #define int long long
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back
#define eb emplace_back
#define For(i,a,b) for(int i=(a); i<=(b); ++i)
#define roF(i,a,b) for(int i=(a); i>=(b); --i)
#define fi first
#define se second
#define mod 998244353
using namespace std;
// using namespace atcoder;
#ifdef DEBUG__
struct db_os{
ostream& os;
bool chk;
template<class T> auto operator<<(T&& x){
if(!chk) os << ", ";
chk=0;
os << x;
return *this;
}
};
template<class... T> void db_out(T&&... t){
(db_os{cerr, 1} << ... << t);
}
#define dbg(...) \
do{ \
cerr << __LINE__ << ":" << #__VA_ARGS__ << "="; \
db_out(__VA_ARGS__); \
cerr << "\n"; \
}while(0);
#else
#define dbg(...)
#endif
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef long double ld;
const int N=200005;
const ll inf=0x3f3f3f3f;
mt19937 rng(random_device {}());
int rand(int a){
return rng()%a;
}
int n, par[N], le[N], dp[N];
vi adj[N], vc;
char s[N];
void dfs(int u, int p=-1){
vc.pb(u);
for(auto &i: adj[u]){
if(i == p) continue;
par[i]=u;
dfs(i, u);
}
}
void rmain(){
cin >> n;
For(i,2,n){
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
//string s; cin >> s;
int cnt=0;
For(i,1,n){
cin >> s[i];
cnt+=(s[i] == 1);
s[i]-='0';
}
int res=min(cnt, 2);
dfs(1);
reverse(all(vc));
for(auto &v: vc){
dp[v]=-s[v];
for(auto &u: adj[v]){
dp[v]+=(u != par[v] ? dp[u] : 0);
}
dp[v]=max(dp[v], (int)s[v]);
}
reverse(all(vc));
for(auto &v: vc){
int ne=-s[v];
for(auto &i: adj[v]){
ne+=(i == par[v] ? le[v] : dp[i]);
}
for(auto &i: adj[v]){
if(i == par[v]) continue;
le[i]=max((int)s[v], ne-dp[i]);
}
}
For(i,1,n){
int ne=-s[i];
for(auto &j: adj[i]){
ne+=(j == par[i] ? le[i] : dp[j]);
}
res=max(res, ne);
}
cout << res;
}
signed main(int argc, char *argv[]){
iostream::sync_with_stdio(0);
int T=1;
while(T--) rmain();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |