이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
 
using namespace std;
#define int long long
 
const int MAX_N = 2*1e5;
int n, dp[MAX_N][2];
string s;
vector<int> adj[MAX_N];
 
void find_dp(int id, int anc){
    dp[id][0]= 0;
    dp[id][1] = 0;
 
    bool gen = s[id] == '1';
 
    int s1=0;
    int best1= 0;
    int best0= 0;
 
    for(auto e: adj[id]){
        if(e!=anc){
            find_dp(e, id);
            s1 += dp[e][1];
            best1 = max(best1, dp[e][1]);
            best0 = max(best0, dp[e][0]);
        }
    }
    if(!gen){
        dp[id][0] = max(max(s1, best0), 0LL);
        dp[id][1] = max(s1, 0LL);
    }
    else{
        dp[id][0] = max(s1-1, max(best1+1, best0));
        dp[id][1] = max(s1-1, 1LL);
    }
 
    //cout<<id<<" "<<dp[id][0]<<" "<<dp[id][1]<<endl;
}
 
 
signed main(){
    cin>>n;
 
    for(int i = 0; i<n-1; i++){
        int a, b;
        cin>>a>>b;
        a--;b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
 
    cin>>s;
 
    find_dp(0, -1);
    cout<<dp[0][0]<<endl;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |