답안 #401023

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401023 2021-05-09T07:59:14 Z AriaH Magic Tree (CEOI19_magictree) C++11
100 / 100
205 ms 100676 KB
/** I can do this all day **/

#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define SZ(x)			    		(int)x.size()
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);

const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

int n, m, k, P[N], f[N], w[N], d[N];

vector < int > G[N];

map < int, ll > dp[N];

void Merge(int v, int u)
{
	if(SZ(dp[v]) < SZ(dp[u]))
	{
		dp[v].swap(dp[u]);
	}
	for(auto cu : dp[u])
	{
		if(cu.S == 0) continue;
		dp[v][cu.F] += cu.S;
	}
}

void dfs(int v)
{
	for(auto u : G[v])
	{
		dfs(u);
		Merge(v, u);
	}
	if(d[v])
	{
		dp[v][d[v]] += w[v];
		int last = d[v], x = w[v];
		while(1)
		{
			auto it = dp[v].upper_bound(last);
			if(it == dp[v].end()) break;
			last = it->F;
			int now = min(1ll * x, it->S);
			x -= now;
			it->S -= now;
			if(it->S == 0)
			{
				dp[v].erase(it);
			}
			if(x == 0) break;
		}
	}
	/*printf("v = %d\n", v);
	for(auto now : dp[v])
	{
		printf("now = %d %d\n", now.F, now.S);
	}
	*/
}

int main()
{
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 2; i <= n ;i ++)
	{
		scanf("%d", &P[i]);
		G[P[i]].push_back(i);
	}
	for(int i = 1; i <= m; i ++)
	{
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		w[a] = c;
		d[a] = b;
	}
	dfs(1);
	ll tot = 0;
	for(auto x : dp[1]) tot += x.S;
	printf("%lld", tot);
    return 0;
}

/** test corner cases(n = 1?) watch for overflow or minus indices **/

Compilation message

magictree.cpp: In function 'int main()':
magictree.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |  scanf("%d%d%d", &n, &m, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |   scanf("%d", &P[i]);
      |   ~~~~~^~~~~~~~~~~~~
magictree.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |   scanf("%d%d%d", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 70720 KB Output is correct
2 Correct 38 ms 70688 KB Output is correct
3 Correct 37 ms 70732 KB Output is correct
4 Correct 37 ms 70720 KB Output is correct
5 Correct 37 ms 70732 KB Output is correct
6 Correct 36 ms 70748 KB Output is correct
7 Correct 37 ms 70860 KB Output is correct
8 Correct 38 ms 70724 KB Output is correct
9 Correct 37 ms 70780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 82188 KB Output is correct
2 Correct 88 ms 81452 KB Output is correct
3 Correct 190 ms 100676 KB Output is correct
4 Correct 125 ms 83684 KB Output is correct
5 Correct 167 ms 85304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 70800 KB Output is correct
2 Correct 38 ms 70844 KB Output is correct
3 Correct 38 ms 70852 KB Output is correct
4 Correct 94 ms 86348 KB Output is correct
5 Correct 140 ms 90272 KB Output is correct
6 Correct 110 ms 86364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 79300 KB Output is correct
2 Correct 121 ms 78532 KB Output is correct
3 Correct 110 ms 81428 KB Output is correct
4 Correct 88 ms 79624 KB Output is correct
5 Correct 91 ms 86468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 70720 KB Output is correct
2 Correct 38 ms 70688 KB Output is correct
3 Correct 37 ms 70732 KB Output is correct
4 Correct 37 ms 70720 KB Output is correct
5 Correct 37 ms 70732 KB Output is correct
6 Correct 36 ms 70748 KB Output is correct
7 Correct 37 ms 70860 KB Output is correct
8 Correct 38 ms 70724 KB Output is correct
9 Correct 37 ms 70780 KB Output is correct
10 Correct 126 ms 81476 KB Output is correct
11 Correct 124 ms 80452 KB Output is correct
12 Correct 102 ms 80744 KB Output is correct
13 Correct 84 ms 79000 KB Output is correct
14 Correct 90 ms 85832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 71528 KB Output is correct
2 Correct 75 ms 74256 KB Output is correct
3 Correct 75 ms 74436 KB Output is correct
4 Correct 71 ms 74236 KB Output is correct
5 Correct 50 ms 72600 KB Output is correct
6 Correct 68 ms 75844 KB Output is correct
7 Correct 69 ms 78796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 70720 KB Output is correct
2 Correct 38 ms 70688 KB Output is correct
3 Correct 37 ms 70732 KB Output is correct
4 Correct 37 ms 70720 KB Output is correct
5 Correct 37 ms 70732 KB Output is correct
6 Correct 36 ms 70748 KB Output is correct
7 Correct 37 ms 70860 KB Output is correct
8 Correct 38 ms 70724 KB Output is correct
9 Correct 37 ms 70780 KB Output is correct
10 Correct 38 ms 70800 KB Output is correct
11 Correct 38 ms 70844 KB Output is correct
12 Correct 38 ms 70852 KB Output is correct
13 Correct 94 ms 86348 KB Output is correct
14 Correct 140 ms 90272 KB Output is correct
15 Correct 110 ms 86364 KB Output is correct
16 Correct 126 ms 81476 KB Output is correct
17 Correct 124 ms 80452 KB Output is correct
18 Correct 102 ms 80744 KB Output is correct
19 Correct 84 ms 79000 KB Output is correct
20 Correct 90 ms 85832 KB Output is correct
21 Correct 61 ms 74156 KB Output is correct
22 Correct 128 ms 82500 KB Output is correct
23 Correct 129 ms 85704 KB Output is correct
24 Correct 176 ms 94032 KB Output is correct
25 Correct 136 ms 83260 KB Output is correct
26 Correct 143 ms 84208 KB Output is correct
27 Correct 125 ms 82616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 70720 KB Output is correct
2 Correct 38 ms 70688 KB Output is correct
3 Correct 37 ms 70732 KB Output is correct
4 Correct 37 ms 70720 KB Output is correct
5 Correct 37 ms 70732 KB Output is correct
6 Correct 36 ms 70748 KB Output is correct
7 Correct 37 ms 70860 KB Output is correct
8 Correct 38 ms 70724 KB Output is correct
9 Correct 37 ms 70780 KB Output is correct
10 Correct 111 ms 82188 KB Output is correct
11 Correct 88 ms 81452 KB Output is correct
12 Correct 190 ms 100676 KB Output is correct
13 Correct 125 ms 83684 KB Output is correct
14 Correct 167 ms 85304 KB Output is correct
15 Correct 38 ms 70800 KB Output is correct
16 Correct 38 ms 70844 KB Output is correct
17 Correct 38 ms 70852 KB Output is correct
18 Correct 94 ms 86348 KB Output is correct
19 Correct 140 ms 90272 KB Output is correct
20 Correct 110 ms 86364 KB Output is correct
21 Correct 125 ms 79300 KB Output is correct
22 Correct 121 ms 78532 KB Output is correct
23 Correct 110 ms 81428 KB Output is correct
24 Correct 88 ms 79624 KB Output is correct
25 Correct 91 ms 86468 KB Output is correct
26 Correct 126 ms 81476 KB Output is correct
27 Correct 124 ms 80452 KB Output is correct
28 Correct 102 ms 80744 KB Output is correct
29 Correct 84 ms 79000 KB Output is correct
30 Correct 90 ms 85832 KB Output is correct
31 Correct 44 ms 71528 KB Output is correct
32 Correct 75 ms 74256 KB Output is correct
33 Correct 75 ms 74436 KB Output is correct
34 Correct 71 ms 74236 KB Output is correct
35 Correct 50 ms 72600 KB Output is correct
36 Correct 68 ms 75844 KB Output is correct
37 Correct 69 ms 78796 KB Output is correct
38 Correct 61 ms 74156 KB Output is correct
39 Correct 128 ms 82500 KB Output is correct
40 Correct 129 ms 85704 KB Output is correct
41 Correct 176 ms 94032 KB Output is correct
42 Correct 136 ms 83260 KB Output is correct
43 Correct 143 ms 84208 KB Output is correct
44 Correct 125 ms 82616 KB Output is correct
45 Correct 63 ms 74568 KB Output is correct
46 Correct 130 ms 83616 KB Output is correct
47 Correct 147 ms 86980 KB Output is correct
48 Correct 205 ms 97024 KB Output is correct
49 Correct 132 ms 84032 KB Output is correct
50 Correct 154 ms 85188 KB Output is correct
51 Correct 146 ms 83524 KB Output is correct