Submission #1127389

#TimeUsernameProblemLanguageResultExecution timeMemory
1127389Yang8onPower Plant (JOI20_power)C++20
100 / 100
162 ms29068 KiB
#include <bits/stdc++.h>
#define Y8o "main"
#define maxn (int) 2e5 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)

//#define f first
//#define s second

using namespace std;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
    if(fopen(Y8o".inp", "r"))
    {
        freopen(Y8o".inp", "r", stdin);
//        freopen(Y8o".out", "w", stdout);
    }
}
void ctime() {
    cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}


int n;
string S;
vector<int> o[maxn];
int dp[maxn], ans; /// dp[u] dam bao ko co chuoi 2 tu con len to tien

void dfs(int u = 1, int p = 0) {
    int sum = 0, mx = 0, ok = (S[u] == '1');
    for(int v : o[u]) if(v != p) {
        dfs(v, u);
        sum += dp[v], mx = max(mx, dp[v]);
    }
    ans = max({ ans, sum - ok, mx + ok });
    dp[u] = max({ ok, sum - ok });
}

void solve() {
    cin >> n ;
    fi(i, 1, n - 1) {
        int u, v; cin >> u >> v;
        o[u].push_back(v), o[v].push_back(u);
    }
    cin >> S, S = ' ' + S;
    dfs();
    cout << ans;
}


int main() {
    iof();

    int nTest = 1;
//    cin >> nTest;

    while(nTest --) {
        solve();
    }

    ctime();
    return 0;
}

Compilation message (stderr)

power.cpp: In function 'void iof()':
power.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen(Y8o".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...