답안 #582564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582564 2022-06-24T05:51:21 Z 조영욱(#8372) Magic Tree (CEOI19_magictree) C++17
100 / 100
187 ms 55332 KB
#include <bits/stdc++.h>
using namespace std;

int n,m,k;
long long dp[100001][21];
vector<int> son[100001];
int t[100001];
int val[100001];
int sz[100001];
typedef pair<int,long long> P;
multiset<P> s[100001];
int ind[100001];

void dfs(int v) {
    sz[v]=1;
    if (son[v].empty()) {
        ind[v]=v;
        if (t[v]!=0) {
            s[ind[v]].insert(P(t[v],val[v]));
        }
        return;
    }
    int mx=son[v][0];
    for(int i=0;i<son[v].size();i++) {
        int nt=son[v][i];
        dfs(nt);
        sz[v]+=sz[nt];
        if (sz[son[v][i]]>sz[mx]) {
            mx=son[v][i];
        }
    }
    ind[v]=ind[mx];
    for(int i=0;i<son[v].size();i++) {
        int nt=son[v][i];
        if (ind[nt]!=ind[v]) {
            for(auto now:s[ind[nt]]) {
                s[ind[v]].insert(now);
            }
        }
    }
    if (t[v]==0) {
        return;
    }
    auto iter=s[ind[v]].lower_bound(P(t[v]+1,-1));
    vector<P> er;
    long long sum=0;
    int pr=0;
    while (iter!=s[ind[v]].end()) {
        sum+=(*iter).second;
        pr=(*iter).first;
        er.push_back(*iter);
        if (sum>=val[v]) {
            break;
        }
        iter=next(iter);
    }
    for(int i=0;i<er.size();i++) {
        s[ind[v]].erase(s[ind[v]].find(er[i]));
    }
    s[ind[v]].insert(P(t[v],val[v]));
    if (sum>val[v]) {
        s[ind[v]].insert(P(pr,sum-val[v]));
    }
}

int main() {
    scanf("%d %d %d",&n,&m,&k);
    for(int i=2;i<=n;i++) {
        int p;
        scanf("%d",&p);
        son[p].push_back(i);
    }
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<m;i++) {
        int v,d,w;
        scanf("%d %d %d",&v,&d,&w);
        t[v]=d;
        val[v]=w;
    }
    dfs(1);
    long long ret=0;
    for(auto now:s[ind[1]]) {
        ret+=now.second;
    }
    printf("%lld",ret);
    return 0;
}

Compilation message

magictree.cpp: In function 'void dfs(int)':
magictree.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i=0;i<son[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
magictree.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for(int i=0;i<son[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
magictree.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i=0;i<er.size();i++) {
      |                 ~^~~~~~~~~~
magictree.cpp: In function 'int main()':
magictree.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d %d %d",&n,&m,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
magictree.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         scanf("%d",&p);
      |         ~~~~~^~~~~~~~~
magictree.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         scanf("%d %d %d",&v,&d,&w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23744 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 13 ms 23756 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 11 ms 23720 KB Output is correct
7 Correct 14 ms 23964 KB Output is correct
8 Correct 13 ms 23716 KB Output is correct
9 Correct 11 ms 23772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 34992 KB Output is correct
2 Correct 60 ms 37144 KB Output is correct
3 Correct 168 ms 52716 KB Output is correct
4 Correct 118 ms 37000 KB Output is correct
5 Correct 136 ms 38940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23988 KB Output is correct
2 Correct 12 ms 24020 KB Output is correct
3 Correct 11 ms 24020 KB Output is correct
4 Correct 90 ms 45584 KB Output is correct
5 Correct 116 ms 51820 KB Output is correct
6 Correct 120 ms 45604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 50432 KB Output is correct
2 Correct 172 ms 55332 KB Output is correct
3 Correct 124 ms 40492 KB Output is correct
4 Correct 88 ms 36944 KB Output is correct
5 Correct 95 ms 48388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23744 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 13 ms 23756 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 11 ms 23720 KB Output is correct
7 Correct 14 ms 23964 KB Output is correct
8 Correct 13 ms 23716 KB Output is correct
9 Correct 11 ms 23772 KB Output is correct
10 Correct 187 ms 46968 KB Output is correct
11 Correct 170 ms 47444 KB Output is correct
12 Correct 110 ms 37884 KB Output is correct
13 Correct 82 ms 36996 KB Output is correct
14 Correct 101 ms 45944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24532 KB Output is correct
2 Correct 52 ms 27708 KB Output is correct
3 Correct 53 ms 27756 KB Output is correct
4 Correct 55 ms 27732 KB Output is correct
5 Correct 22 ms 26016 KB Output is correct
6 Correct 44 ms 30728 KB Output is correct
7 Correct 43 ms 35256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23744 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 13 ms 23756 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 11 ms 23720 KB Output is correct
7 Correct 14 ms 23964 KB Output is correct
8 Correct 13 ms 23716 KB Output is correct
9 Correct 11 ms 23772 KB Output is correct
10 Correct 13 ms 23988 KB Output is correct
11 Correct 12 ms 24020 KB Output is correct
12 Correct 11 ms 24020 KB Output is correct
13 Correct 90 ms 45584 KB Output is correct
14 Correct 116 ms 51820 KB Output is correct
15 Correct 120 ms 45604 KB Output is correct
16 Correct 187 ms 46968 KB Output is correct
17 Correct 170 ms 47444 KB Output is correct
18 Correct 110 ms 37884 KB Output is correct
19 Correct 82 ms 36996 KB Output is correct
20 Correct 101 ms 45944 KB Output is correct
21 Correct 33 ms 28212 KB Output is correct
22 Correct 110 ms 39636 KB Output is correct
23 Correct 125 ms 39764 KB Output is correct
24 Correct 151 ms 48424 KB Output is correct
25 Correct 109 ms 38376 KB Output is correct
26 Correct 133 ms 40372 KB Output is correct
27 Correct 112 ms 39236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23744 KB Output is correct
2 Correct 12 ms 23700 KB Output is correct
3 Correct 13 ms 23756 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 11 ms 23800 KB Output is correct
6 Correct 11 ms 23720 KB Output is correct
7 Correct 14 ms 23964 KB Output is correct
8 Correct 13 ms 23716 KB Output is correct
9 Correct 11 ms 23772 KB Output is correct
10 Correct 80 ms 34992 KB Output is correct
11 Correct 60 ms 37144 KB Output is correct
12 Correct 168 ms 52716 KB Output is correct
13 Correct 118 ms 37000 KB Output is correct
14 Correct 136 ms 38940 KB Output is correct
15 Correct 13 ms 23988 KB Output is correct
16 Correct 12 ms 24020 KB Output is correct
17 Correct 11 ms 24020 KB Output is correct
18 Correct 90 ms 45584 KB Output is correct
19 Correct 116 ms 51820 KB Output is correct
20 Correct 120 ms 45604 KB Output is correct
21 Correct 180 ms 50432 KB Output is correct
22 Correct 172 ms 55332 KB Output is correct
23 Correct 124 ms 40492 KB Output is correct
24 Correct 88 ms 36944 KB Output is correct
25 Correct 95 ms 48388 KB Output is correct
26 Correct 187 ms 46968 KB Output is correct
27 Correct 170 ms 47444 KB Output is correct
28 Correct 110 ms 37884 KB Output is correct
29 Correct 82 ms 36996 KB Output is correct
30 Correct 101 ms 45944 KB Output is correct
31 Correct 17 ms 24532 KB Output is correct
32 Correct 52 ms 27708 KB Output is correct
33 Correct 53 ms 27756 KB Output is correct
34 Correct 55 ms 27732 KB Output is correct
35 Correct 22 ms 26016 KB Output is correct
36 Correct 44 ms 30728 KB Output is correct
37 Correct 43 ms 35256 KB Output is correct
38 Correct 33 ms 28212 KB Output is correct
39 Correct 110 ms 39636 KB Output is correct
40 Correct 125 ms 39764 KB Output is correct
41 Correct 151 ms 48424 KB Output is correct
42 Correct 109 ms 38376 KB Output is correct
43 Correct 133 ms 40372 KB Output is correct
44 Correct 112 ms 39236 KB Output is correct
45 Correct 39 ms 28736 KB Output is correct
46 Correct 107 ms 41272 KB Output is correct
47 Correct 116 ms 40988 KB Output is correct
48 Correct 167 ms 51916 KB Output is correct
49 Correct 117 ms 39048 KB Output is correct
50 Correct 187 ms 41256 KB Output is correct
51 Correct 161 ms 40248 KB Output is correct