답안 #1007352

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1007352 2024-06-24T16:42:58 Z Alihan_8 Power Plant (JOI20_power) C++17
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int inf = 1e9;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;

    vector <vector<int>> adj(n);

    for ( int i = 1; i < n; i++ ){
        int u, v; cin >> u >> v;

        --u, --v;

        adj[u].pb(v);
        adj[v].pb(u);
    }

    string s; cin >> s;

    int cnt = 0;

    for ( auto &u: s ){
        u -= '0';

        cnt += 1;
    }

    vector <int> dp(n, -inf), d(n);

    int ans = min(2LL, cnt);

    auto dfs = [&](auto dfs, int u, int p) -> void{
        vector <int> child;

        for ( auto &v: adj[u] ){
            if ( v != p ){
                d[v] = d[u] + s[u];

                dfs(dfs, v, u);

                child.pb(dp[v]);

                if ( s[u] > 0 ){
                    chmax(ans, s[u] * 2 + dp[v] + d[u]);
                }
            }
        }

        if ( s[u] > 0 ){
            dp[u] = s[u] - d[u];
        }

        sort(all(child), greater <int> ());

        int cnt = 0;

        for ( int i = 0; i < child.size(); i++ ){
            cnt += child[i];

            chmax(dp[u], cnt + d[u] * (i + 1) + s[u] * i - d[u]);
        }

        chmax(ans, dp[u]);
    };

    dfs(dfs, 0, -1);

    cout << ans << ln;

    cout << '\n';
}

Compilation message

power.cpp: In lambda function:
power.cpp:89:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for ( int i = 0; i < child.size(); i++ ){
      |                          ~~^~~~~~~~~~~~~~
power.cpp: In instantiation of 'main()::<lambda(auto:23, long long int, long long int)> [with auto:23 = main()::<lambda(auto:23, long long int, long long int)>]':
power.cpp:98:19:   required from here
power.cpp:89:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -