Submission #294001

#TimeUsernameProblemLanguageResultExecution timeMemory
294001muhammad_hokimiyonPower Plant (JOI20_power)C++14
100 / 100
209 ms30968 KiB
#include <bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int N = 5e5 + 7;
const int M = 20 + 7;
const ll mod = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
int ans;
int a[N];
int d[N];
vector < int > v[N];

void dfs( int x , int p )
{
    vector < int > g;
    for( auto y : v[x] ){
        if( y == p )continue;
        dfs( y , x );
        d[x] += d[y];
    }
    if( a[x] ){
        d[x] -= 1;
        d[x] = max( d[x] , 1 );
    }
    if( x != p )ans = max( ans , d[x] + a[p] );
    ans = max( ans , d[x] );
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n;
    for( int i = 1; i < n; i++ ){
        int x,y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for( int i = 1; i <= n; i++ ){
        char c;
        cin >> c;
        a[i] = c - '0';
    }
    dfs( 1 , 1 );
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...