답안 #723810

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
723810 2023-04-14T10:39:52 Z Mr_Husanboy Magic Tree (CEOI19_magictree) C++17
61 / 100
1121 ms 1048576 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int inf = 1e9;

template<typename T>
int len(T &a){
    return a.size();
}

struct Segtree{
    struct node{
        int mx = 0;
        void set(int x){
            mx = max(mx, x);
        }
        node(int x){mx = x;}
        node(){mx = 0;};
    };

    node merge(node a, node b){
        return node(max(a.mx, b.mx));
    }

    vector<node> t;
    int n;
    void init(int _n){
        n = _n;
        t.resize(4 * n, node());
    }

    void upd(int x, int lx, int rx, int ind, int val){
        if(lx == rx){
            t[x].set(val); return;
        }
        int m = (lx + rx) / 2;
        if(ind <= m){
            upd(x * 2, lx, m, ind, val);
        }else
            upd(x * 2 + 1, m + 1, rx, ind, val);
        t[x] = merge(t[x * 2], t[x * 2 + 1]);
    }

    node get(int x, int lx, int rx, int l, int r){
        if(l <= lx && rx <= r) return t[x];
        if(r < lx || rx < l){
            return {0};
        }
        int m = (lx + rx) / 2;
        return merge(get(x * 2, lx, m, l, r), get(x * 2 + 1, m + 1, rx, l, r));
    }

    void upd(int ind, int val){
        upd(1, 0, n - 1, ind, val);
    }

    node get(int l, int r){
        return get(1, 0, n - 1, l, r);
    }
};

void sub3(int &n, int &m, int &k, vector<int> p,
    vector<int> &v, vector<int> &day, vector<int> &cost){
    //cout << "start" << endl;
    for(int i = 1; i < n; i ++){
        if(p[i] != i - 1) return;
    }
    for(int i = 0; i < m; i ++){
        if(cost[i] != 1) return;
    }
    vector<int> fr(n);
    for(int i = 0; i < m; i ++){
        fr[v[i]] = day[i];
    }
    Segtree t; t.init(k + 1);
    int ans = 0;
    for(int i = n - 1; i > 0; i --){
        if(fr[i] == 0) continue;
        int mx = t.get(1, fr[i]).mx + 1;
        ans = max(ans, mx);
        t.upd(fr[i], mx);
    }
    cout << ans; exit(0); 
    cout << endl;
}

void sub2(int &n, int &m, int &k, vector<int> p,
    vector<int> &v, vector<int> &day, vector<int> &cost){
    //cout << "start" << endl;
    vector<vector<int>> g(n);
    for(int i = 1; i < n; i ++){
        g[p[i]].push_back(i);
    }
    ll ans = 0;
    for(int i = 0; i < m; i ++){
        if(len(g[v[i]])){return;}
        ans += cost[i];
    }
    cout << ans; exit(0);

}


void sub5(int &n, int &m, int &k, vector<int> p,
    vector<int> &v, vector<int> &day, vector<int> &cost){
    //cout << "start" << endl;

    vector<vector<int>> ng(n);
    vector<int> np(n, -1);
    for(int i = 1; i < n; i ++){
        ng[p[i]].push_back(i);
    }
    vector<int> nfr(n);
    nfr[0] = 1;
    for(int i =0; i < m; i++){
        nfr[v[i]] = 1;
    }
    auto compress = [&](auto &compress, int i,int near)->void{
        for(auto u : ng[i]){
            if(nfr[u]) np[u] = near;
            if(nfr[u]){
                compress(compress, u, u);
            }else
                compress(compress, u, near);
        }
    };
    vector<int> id(n, -1);

    compress(compress, 0, 0);
    for(int i = 0; i < m; i ++){
        id[v[i]] = i + 1;
    } 
    id[0] = 0;
    //for(auto u : id) cout << u << ' '; cout << endl;
    vector<vector<int>> g(m + 1);
    for(int i = 0; i < n; i ++){
        if(np[i] == -1) continue;
        g[id[np[i]]].push_back(id[i]);
    }
    // for(int i = 0; i < n; i ++){
    //     if(id[i] == -1) continue;
    //     cout << id[i] << ": ";
    //     for(auto u : g[id[i]]) cout << u << ' ';
    //         cout << endl;
    // }
    vector<ll> fr(n), c(n);
    for(int i = 0; i < m; i ++){
        fr[id[v[i]]] = day[i];
        c[id[v[i]]] = cost[i];
    }
    
    vector<vector<ll>> dp(m + 1, vector<ll> (k + 1, 0));
    auto dfs = [&](auto &dfs, int i)->void{
        //cout << i + 1 << endl;
        for(auto u : g[i]){
            dfs(dfs, u);
        }
        for(int j = 1; j <= k; j ++){
            ll s = (j == fr[i] ? c[i] : 0);
            for(auto u : g[i]){
                s += dp[u][j];
            }
            dp[i][j] = s;
            dp[i][j] = max(dp[i][j - 1], dp[i][j]);
        }
    };
    dfs(dfs, 0);
    //cout << "d" << endl;
    ll mx = 0;
    for(int i = 0; i <= m; i ++){
        for(int j = 1; j <= k; j++){
            mx = max(mx, dp[i][j]);
        }
    }
    cout << mx << endl;
    exit(0);
}

void solve(){
    int n, m, k; cin >> n >> m >> k;
    vector<int> p(n), v(m), day(m), cost(m);
    for(int i = 1; i < n; i ++){
        cin >> p[i]; p[i] --;
    }
    for(int i = 0; i < m; i ++){
        cin >> v[i] >> day[i] >> cost[i];
        v[i] --;
    }
    sub2(n, m, k, p, v, day, cost);
    sub3(n, m, k, p, v, day, cost);
    sub5(n, m, k, p, v, day, cost);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int testcases = 1;

    while(testcases --){
        solve();
        if(testcases) cout << '\n';
        #ifdef LOCAL
        else cout << '\n';
        cout << "__________________________" << endl;
        #endif
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 5104 KB Output is correct
2 Correct 29 ms 6100 KB Output is correct
3 Correct 40 ms 5088 KB Output is correct
4 Correct 37 ms 5092 KB Output is correct
5 Correct 45 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 84 ms 7756 KB Output is correct
5 Correct 70 ms 7652 KB Output is correct
6 Correct 89 ms 7648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 17336 KB Output is correct
2 Correct 92 ms 17336 KB Output is correct
3 Correct 78 ms 26288 KB Output is correct
4 Correct 62 ms 14996 KB Output is correct
5 Correct 88 ms 38508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 140 ms 30016 KB Output is correct
11 Correct 112 ms 22972 KB Output is correct
12 Correct 128 ms 39068 KB Output is correct
13 Correct 108 ms 27728 KB Output is correct
14 Correct 44 ms 7520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 457 ms 378444 KB Output is correct
2 Correct 486 ms 370448 KB Output is correct
3 Correct 636 ms 551744 KB Output is correct
4 Correct 989 ms 713792 KB Output is correct
5 Correct 1121 ms 791072 KB Output is correct
6 Correct 811 ms 792460 KB Output is correct
7 Correct 750 ms 794068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 84 ms 7756 KB Output is correct
14 Correct 70 ms 7652 KB Output is correct
15 Correct 89 ms 7648 KB Output is correct
16 Correct 140 ms 30016 KB Output is correct
17 Correct 112 ms 22972 KB Output is correct
18 Correct 128 ms 39068 KB Output is correct
19 Correct 108 ms 27728 KB Output is correct
20 Correct 44 ms 7520 KB Output is correct
21 Correct 303 ms 285252 KB Output is correct
22 Correct 807 ms 748900 KB Output is correct
23 Runtime error 423 ms 1048576 KB Execution killed with signal 9
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 32 ms 5104 KB Output is correct
11 Correct 29 ms 6100 KB Output is correct
12 Correct 40 ms 5088 KB Output is correct
13 Correct 37 ms 5092 KB Output is correct
14 Correct 45 ms 5228 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 84 ms 7756 KB Output is correct
19 Correct 70 ms 7652 KB Output is correct
20 Correct 89 ms 7648 KB Output is correct
21 Correct 92 ms 17336 KB Output is correct
22 Correct 92 ms 17336 KB Output is correct
23 Correct 78 ms 26288 KB Output is correct
24 Correct 62 ms 14996 KB Output is correct
25 Correct 88 ms 38508 KB Output is correct
26 Correct 140 ms 30016 KB Output is correct
27 Correct 112 ms 22972 KB Output is correct
28 Correct 128 ms 39068 KB Output is correct
29 Correct 108 ms 27728 KB Output is correct
30 Correct 44 ms 7520 KB Output is correct
31 Correct 457 ms 378444 KB Output is correct
32 Correct 486 ms 370448 KB Output is correct
33 Correct 636 ms 551744 KB Output is correct
34 Correct 989 ms 713792 KB Output is correct
35 Correct 1121 ms 791072 KB Output is correct
36 Correct 811 ms 792460 KB Output is correct
37 Correct 750 ms 794068 KB Output is correct
38 Correct 303 ms 285252 KB Output is correct
39 Correct 807 ms 748900 KB Output is correct
40 Runtime error 423 ms 1048576 KB Execution killed with signal 9
41 Halted 0 ms 0 KB -