이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;
struct no{
vector<int> ch;
int t;
int dp=0;
};
vector<no> v;
int ans=1;
void dfs(int r,int f){
for(auto h:v[r].ch){
if(h!=f){
dfs(h,r);
v[r].dp+=v[h].dp;
}
}
if(v[r].t==1){
v[r].dp=max(v[r].dp-1,1);
}
}
void dfs2(int r,int f,int x){
v[r].dp+=x;
ans=max(ans,v[r].dp);
for(auto h:v[r].ch){
if(h!=f){
dfs2(h,r,max(1,v[r].dp-v[h].dp));
}
}
}
int main(){
AquA;
int n;
cin >> n;
v.resize(n);
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
a--;
b--;
v[a].ch.push_back(b);
v[b].ch.push_back(a);
}
string s;
cin >> s;
for(int i=0;i<n;i++){
v[i].t=s[i]-'0';
}
dfs(0,0);
dfs2(0,0,0);
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |