Submission #683771

# Submission time Handle Problem Language Result Execution time Memory
683771 2023-01-19T10:25:07 Z nifeshe Biochips (IZhO12_biochips) C++17
100 / 100
671 ms 401744 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC target ("avx2")
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma comment (linker, "/STACK: 16777216")

#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
//#define int long long

using namespace std;
using namespace __gnu_pbds;

template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = 998244353;
const ll base = 1e6 + 5;
const int inf = 1e9;
const int MAX = 2e5 + 5;
const int LG = 31;

random_device rd;
mt19937 gen(rd());
uniform_int_distribution<ll> dis(1, inf);

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1);
    vector<vector<int>> g(n + 1);
    int root;
    for(int i = 1; i <= n; i++) {
        int p;
        cin >> p >> a[i];
        if(!p) {
            root = i;
            continue;
        }
        g[p].pb(i);
        g[i].pb(p);
    }
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, -inf));
    vector<int> tin(n + 1), tout(n + 1);
    vector<int> val(n + 1);
    dp[0][0] = 0;
    int timer = 1;
    function<void(int, int)> dfs = [&](int v, int p) {
        tin[v] = timer;
        for(auto to : g[v]) {
            if(to == p) continue;
            dfs(to, v);
        }
        tout[v] = timer++;
        val[tout[v]] = v;
        for(int j = 0; j <= m; j++) {
            dp[v][j] = max(dp[val[tout[v] - 1]][j], (j > 0? dp[val[tin[v] - 1]][j - 1] + a[v]: -inf));
        }
    };
    dfs(root, 0);
    int ans = 0;
//    for(int i = 1; i <= n; i++) {
//        for(int j = 0; j <= m; j++) {
//            cout << dp[i][j] << " ";
//        }
//        cout << endl;
//    }
    for(int i = 1; i <= n; i++) umax(ans, dp[i][m]);
    cout << ans << '\n';
}

signed main() {
//    freopen("skyline.in", "r", stdin); freopen("skyline.out", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int ttt = 1;
//    cin >> ttt;
    while(ttt--) {
        solve();
    }
    return 0;
}

Compilation message

biochips.cpp: In function 'void solve()':
biochips.cpp:44:9: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   44 |     int root;
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 7 ms 3456 KB Output is correct
5 Correct 9 ms 4436 KB Output is correct
6 Correct 10 ms 5368 KB Output is correct
7 Correct 333 ms 191932 KB Output is correct
8 Correct 368 ms 191888 KB Output is correct
9 Correct 499 ms 298804 KB Output is correct
10 Correct 671 ms 401744 KB Output is correct