Submission #1119371

# Submission time Handle Problem Language Result Execution time Memory
1119371 2024-11-27T01:45:34 Z yeediot Magic Tree (CEOI19_magictree) C++17
100 / 100
146 ms 35932 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void CHECK();
void setio(){
    freopen("/Users/iantsai/cpp/input.txt","r",stdin);
    freopen("/Users/iantsai/cpp/output.txt","w",stdout);
}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
#define TOI_is_so_de ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);setio();
const int mxn = 1e5 + 5;
vector<int>adj[mxn];
int n, m, k, d[mxn], w[mxn], ans = 0;
vector<vector<int>>dp;
map<int, int>mp[mxn];
void dfs(int v, int pa){
    for(auto u : adj[v]){
        if(u == pa) continue;
        dfs(u, v);
        /*for(int i = 0; i <= k; i++){
            dp[v][i] += dp[u][i];
            chmax(ans, dp[v][i]);
        }*/
        if(sz(mp[v]) < sz(mp[u])){
            swap(mp[v], mp[u]);
        }
        for(auto x : mp[u]){
            mp[v][x.F] += x.S;
        }
    }
    /*cout << v << ' ';
    for(int i = 0; i <= k; i++){
        cout << dp[v][i] << ' ';
    }
    cout << '\n';*/
    int cur = 0;
    if(w[v] == 0) return;
    auto it = mp[v].lower_bound(d[v]+1);
    for(; it != mp[v].end() and cur <= w[v];){
        cur += it->S;
        if(cur <= w[v])it = mp[v].erase(it);
        else{
            //cur-=it->S;
            it->S = cur-w[v];
            break;
        }
    }
    mp[v][d[v]] += w[v];
    // for(int i = 0; i <= k; i++){
    //     cur = max(cur, dp[v][i]);
    //     if(i == d[v]) cur = max(cur, dp[v][i] + w[v]);
    //     dp[v][i] = cur;
    // }
}
void solve(){
    cin >> n >> m >> k;
    for(int i = 2; i <= n; i++){
        int p;
        cin >> p;
        adj[p].pb(i);
    }
    for(int i = 1; i <= m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        d[a] = b;
        w[a] = c;
    }
    dfs(1, 1);
    ans = 0;
    for(auto x : mp[1]){
        ans += x.S;
    }
    cout << ans << '\n';
}
signed main(){
    TOI_is_so_de;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    #ifdef local
    CHECK();
    #endif
}
/*
input:
 
*/
#ifdef local
void CHECK(){
    cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    function<bool(string,string)> compareFiles = [](string p1, string p2)->bool {
        std::ifstream file1(p1);
        std::ifstream file2(p2);
        if(!file1.is_open() || !file2.is_open()) return false;
        std::string line1, line2;
        while (getline(file1, line1) && getline(file2, line2)) {
            if (line1 != line2)return false;
        }
        int cnta = 0, cntb = 0;
        while(getline(file1,line1))cnta++;
        while(getline(file2,line2))cntb++;
        return cntb - cnta <= 1;
    };
    bool check = compareFiles("output.txt","expected.txt");
    if(check) cerr<<"ACCEPTED\n";
    else cerr<<"WRONG ANSWER!\n";
}
#else
#endif
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7504 KB Output is correct
2 Correct 6 ms 7508 KB Output is correct
3 Correct 6 ms 7504 KB Output is correct
4 Correct 8 ms 7504 KB Output is correct
5 Correct 7 ms 7524 KB Output is correct
6 Correct 7 ms 7504 KB Output is correct
7 Correct 7 ms 7504 KB Output is correct
8 Correct 6 ms 7504 KB Output is correct
9 Correct 7 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 19676 KB Output is correct
2 Correct 47 ms 19536 KB Output is correct
3 Correct 146 ms 35932 KB Output is correct
4 Correct 78 ms 19908 KB Output is correct
5 Correct 101 ms 20952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7516 KB Output is correct
2 Correct 6 ms 7512 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 52 ms 29692 KB Output is correct
5 Correct 87 ms 33612 KB Output is correct
6 Correct 74 ms 29788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 16052 KB Output is correct
2 Correct 61 ms 15944 KB Output is correct
3 Correct 55 ms 21064 KB Output is correct
4 Correct 42 ms 17100 KB Output is correct
5 Correct 58 ms 29768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7504 KB Output is correct
2 Correct 6 ms 7508 KB Output is correct
3 Correct 6 ms 7504 KB Output is correct
4 Correct 8 ms 7504 KB Output is correct
5 Correct 7 ms 7524 KB Output is correct
6 Correct 7 ms 7504 KB Output is correct
7 Correct 7 ms 7504 KB Output is correct
8 Correct 6 ms 7504 KB Output is correct
9 Correct 7 ms 7504 KB Output is correct
10 Correct 76 ms 19076 KB Output is correct
11 Correct 90 ms 17864 KB Output is correct
12 Correct 56 ms 20304 KB Output is correct
13 Correct 44 ms 16508 KB Output is correct
14 Correct 60 ms 29212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8272 KB Output is correct
2 Correct 37 ms 11600 KB Output is correct
3 Correct 27 ms 11544 KB Output is correct
4 Correct 26 ms 11600 KB Output is correct
5 Correct 12 ms 9944 KB Output is correct
6 Correct 30 ms 14416 KB Output is correct
7 Correct 31 ms 18344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7504 KB Output is correct
2 Correct 6 ms 7508 KB Output is correct
3 Correct 6 ms 7504 KB Output is correct
4 Correct 8 ms 7504 KB Output is correct
5 Correct 7 ms 7524 KB Output is correct
6 Correct 7 ms 7504 KB Output is correct
7 Correct 7 ms 7504 KB Output is correct
8 Correct 6 ms 7504 KB Output is correct
9 Correct 7 ms 7504 KB Output is correct
10 Correct 6 ms 7516 KB Output is correct
11 Correct 6 ms 7512 KB Output is correct
12 Correct 5 ms 7764 KB Output is correct
13 Correct 52 ms 29692 KB Output is correct
14 Correct 87 ms 33612 KB Output is correct
15 Correct 74 ms 29788 KB Output is correct
16 Correct 76 ms 19076 KB Output is correct
17 Correct 90 ms 17864 KB Output is correct
18 Correct 56 ms 20304 KB Output is correct
19 Correct 44 ms 16508 KB Output is correct
20 Correct 60 ms 29212 KB Output is correct
21 Correct 28 ms 11092 KB Output is correct
22 Correct 75 ms 19952 KB Output is correct
23 Correct 96 ms 23120 KB Output is correct
24 Correct 119 ms 31288 KB Output is correct
25 Correct 75 ms 20572 KB Output is correct
26 Correct 82 ms 22872 KB Output is correct
27 Correct 75 ms 22324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7504 KB Output is correct
2 Correct 6 ms 7508 KB Output is correct
3 Correct 6 ms 7504 KB Output is correct
4 Correct 8 ms 7504 KB Output is correct
5 Correct 7 ms 7524 KB Output is correct
6 Correct 7 ms 7504 KB Output is correct
7 Correct 7 ms 7504 KB Output is correct
8 Correct 6 ms 7504 KB Output is correct
9 Correct 7 ms 7504 KB Output is correct
10 Correct 72 ms 19676 KB Output is correct
11 Correct 47 ms 19536 KB Output is correct
12 Correct 146 ms 35932 KB Output is correct
13 Correct 78 ms 19908 KB Output is correct
14 Correct 101 ms 20952 KB Output is correct
15 Correct 6 ms 7516 KB Output is correct
16 Correct 6 ms 7512 KB Output is correct
17 Correct 5 ms 7764 KB Output is correct
18 Correct 52 ms 29692 KB Output is correct
19 Correct 87 ms 33612 KB Output is correct
20 Correct 74 ms 29788 KB Output is correct
21 Correct 71 ms 16052 KB Output is correct
22 Correct 61 ms 15944 KB Output is correct
23 Correct 55 ms 21064 KB Output is correct
24 Correct 42 ms 17100 KB Output is correct
25 Correct 58 ms 29768 KB Output is correct
26 Correct 76 ms 19076 KB Output is correct
27 Correct 90 ms 17864 KB Output is correct
28 Correct 56 ms 20304 KB Output is correct
29 Correct 44 ms 16508 KB Output is correct
30 Correct 60 ms 29212 KB Output is correct
31 Correct 14 ms 8272 KB Output is correct
32 Correct 37 ms 11600 KB Output is correct
33 Correct 27 ms 11544 KB Output is correct
34 Correct 26 ms 11600 KB Output is correct
35 Correct 12 ms 9944 KB Output is correct
36 Correct 30 ms 14416 KB Output is correct
37 Correct 31 ms 18344 KB Output is correct
38 Correct 28 ms 11092 KB Output is correct
39 Correct 75 ms 19952 KB Output is correct
40 Correct 96 ms 23120 KB Output is correct
41 Correct 119 ms 31288 KB Output is correct
42 Correct 75 ms 20572 KB Output is correct
43 Correct 82 ms 22872 KB Output is correct
44 Correct 75 ms 22324 KB Output is correct
45 Correct 26 ms 11380 KB Output is correct
46 Correct 73 ms 20964 KB Output is correct
47 Correct 84 ms 24408 KB Output is correct
48 Correct 131 ms 34348 KB Output is correct
49 Correct 81 ms 21192 KB Output is correct
50 Correct 93 ms 23908 KB Output is correct
51 Correct 73 ms 23080 KB Output is correct