답안 #387902

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
387902 2021-04-09T11:47:15 Z oolimry Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
436 ms 101656 KB
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) x.size())
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x, y) cerr << #x << " is " << x << ", " << #y << " is " << y << endl;
typedef long long lint;
typedef pair<lint, lint> ii;

int p[200005];
int vis[200005];
int collapse[200005];

vector<int> adj[200005];
map<int, lint> dp[200005];
lint cost[200005];
lint original[200005];

void dfs(int u){
	for(int v : adj[u]){
		dfs(v);
		
		if(sz(dp[u]) < sz(dp[v])) swap(dp[u], dp[v]);
		
		map<int, lint> &cur = dp[u];
		
		for(ii x : dp[v]){
			if(cur.count(x.first)) cur[x.first] += x.second;
			else cur[x.first] = x.second;
		}
	}
	
	
	lint O = original[u]; lint C = cost[u];
	map<int, lint> &cur = dp[u];
	if(cur.count(O)) cur[O] += C ;
	else cur[O] = C;
	
	while(true){
		auto it = cur.upper_bound(O);
		if(it == cur.end()) break;
		if(it->second > C){
			it->second -= C;
			break;
		}
		else{
			C -= it->second;
			cur.erase(it);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	lint ans = 0;
	int n; cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> p[i] >> original[i] >> cost[i];
		ans += cost[i];
	}
	
	vector<ii> compress;
	
	for(int s = 1;s <= n;s++){
		int u = s;
		int cycle = -1;
		
		while(true){
			if(vis[u] != 0){
				if(vis[u] == s) cycle = u;
				break;
			}
			vis[u] = s;
			u = p[u];
		}
		
		if(cycle == -1) continue;
		
		vector<ii> nodes = {ii(1e9+1,0)};
		u = cycle;
		while(true){
			//cout << u << " ";
			nodes.push_back(ii(original[u], u));
			u = p[u];
			if(u == cycle) break;
		}
		//cout << "\n";
				
		sort(all(nodes));
		for(int i = 0;i < sz(nodes)-1;i++){
			collapse[nodes[i].second] = nodes[0].second;
			compress.push_back(ii(nodes[i].second, nodes[i+1].second));
		//	show2(nodes[i].second, nodes[i+1].second);
		}
	}
	
	
	
	for(int i = 1;i <= n;i++){
		if(collapse[p[i]] != 0) p[i] = collapse[p[i]];
	}
	
	for(ii c : compress) p[c.first] = c.second;
	
//	for(int i = 1;i <= n;i++) show2(i, p[i]);
	
	for(int i = 1;i <= n;i++) adj[p[i]].push_back(i);
	
	for(int i = 0;i <= n;i++) original[i] *= -1;
	dfs(0);
	for(ii x : dp[0]) ans -= x.second;
	
	cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 11 ms 14388 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14424 KB Output is correct
5 Correct 15 ms 15528 KB Output is correct
6 Correct 13 ms 15160 KB Output is correct
7 Correct 12 ms 14836 KB Output is correct
8 Correct 16 ms 15660 KB Output is correct
9 Correct 14 ms 15052 KB Output is correct
10 Correct 13 ms 14948 KB Output is correct
11 Correct 11 ms 14684 KB Output is correct
12 Correct 13 ms 15312 KB Output is correct
13 Correct 11 ms 15308 KB Output is correct
14 Correct 12 ms 14924 KB Output is correct
15 Correct 12 ms 14920 KB Output is correct
16 Correct 16 ms 15864 KB Output is correct
17 Correct 13 ms 15308 KB Output is correct
18 Correct 11 ms 14720 KB Output is correct
19 Correct 13 ms 15248 KB Output is correct
20 Correct 12 ms 15052 KB Output is correct
21 Correct 11 ms 15052 KB Output is correct
22 Correct 13 ms 15180 KB Output is correct
23 Correct 11 ms 14796 KB Output is correct
24 Correct 14 ms 15280 KB Output is correct
25 Correct 12 ms 15012 KB Output is correct
26 Correct 11 ms 15308 KB Output is correct
27 Correct 14 ms 15304 KB Output is correct
28 Correct 13 ms 15340 KB Output is correct
29 Correct 16 ms 15444 KB Output is correct
30 Correct 14 ms 15488 KB Output is correct
31 Correct 14 ms 15460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 15564 KB Output is correct
2 Correct 436 ms 76812 KB Output is correct
3 Correct 334 ms 51896 KB Output is correct
4 Correct 433 ms 73640 KB Output is correct
5 Correct 335 ms 51892 KB Output is correct
6 Correct 219 ms 32912 KB Output is correct
7 Correct 198 ms 28540 KB Output is correct
8 Correct 174 ms 50372 KB Output is correct
9 Correct 160 ms 50508 KB Output is correct
10 Correct 108 ms 50364 KB Output is correct
11 Correct 171 ms 38104 KB Output is correct
12 Correct 152 ms 37828 KB Output is correct
13 Correct 412 ms 101656 KB Output is correct
14 Correct 302 ms 66104 KB Output is correct
15 Correct 100 ms 28380 KB Output is correct
16 Correct 294 ms 47940 KB Output is correct
17 Correct 139 ms 41160 KB Output is correct
18 Correct 105 ms 40908 KB Output is correct
19 Correct 338 ms 44880 KB Output is correct
20 Correct 117 ms 32448 KB Output is correct
21 Correct 344 ms 48572 KB Output is correct
22 Correct 130 ms 41456 KB Output is correct
23 Correct 117 ms 50348 KB Output is correct
24 Correct 236 ms 50472 KB Output is correct
25 Correct 204 ms 55440 KB Output is correct
26 Correct 194 ms 58336 KB Output is correct
27 Correct 236 ms 56640 KB Output is correct
28 Correct 244 ms 56576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 11 ms 14388 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14424 KB Output is correct
5 Correct 15 ms 15528 KB Output is correct
6 Correct 13 ms 15160 KB Output is correct
7 Correct 12 ms 14836 KB Output is correct
8 Correct 16 ms 15660 KB Output is correct
9 Correct 14 ms 15052 KB Output is correct
10 Correct 13 ms 14948 KB Output is correct
11 Correct 11 ms 14684 KB Output is correct
12 Correct 13 ms 15312 KB Output is correct
13 Correct 11 ms 15308 KB Output is correct
14 Correct 12 ms 14924 KB Output is correct
15 Correct 12 ms 14920 KB Output is correct
16 Correct 16 ms 15864 KB Output is correct
17 Correct 13 ms 15308 KB Output is correct
18 Correct 11 ms 14720 KB Output is correct
19 Correct 13 ms 15248 KB Output is correct
20 Correct 12 ms 15052 KB Output is correct
21 Correct 11 ms 15052 KB Output is correct
22 Correct 13 ms 15180 KB Output is correct
23 Correct 11 ms 14796 KB Output is correct
24 Correct 14 ms 15280 KB Output is correct
25 Correct 12 ms 15012 KB Output is correct
26 Correct 11 ms 15308 KB Output is correct
27 Correct 14 ms 15304 KB Output is correct
28 Correct 13 ms 15340 KB Output is correct
29 Correct 16 ms 15444 KB Output is correct
30 Correct 14 ms 15488 KB Output is correct
31 Correct 14 ms 15460 KB Output is correct
32 Correct 15 ms 15564 KB Output is correct
33 Correct 436 ms 76812 KB Output is correct
34 Correct 334 ms 51896 KB Output is correct
35 Correct 433 ms 73640 KB Output is correct
36 Correct 335 ms 51892 KB Output is correct
37 Correct 219 ms 32912 KB Output is correct
38 Correct 198 ms 28540 KB Output is correct
39 Correct 174 ms 50372 KB Output is correct
40 Correct 160 ms 50508 KB Output is correct
41 Correct 108 ms 50364 KB Output is correct
42 Correct 171 ms 38104 KB Output is correct
43 Correct 152 ms 37828 KB Output is correct
44 Correct 412 ms 101656 KB Output is correct
45 Correct 302 ms 66104 KB Output is correct
46 Correct 100 ms 28380 KB Output is correct
47 Correct 294 ms 47940 KB Output is correct
48 Correct 139 ms 41160 KB Output is correct
49 Correct 105 ms 40908 KB Output is correct
50 Correct 338 ms 44880 KB Output is correct
51 Correct 117 ms 32448 KB Output is correct
52 Correct 344 ms 48572 KB Output is correct
53 Correct 130 ms 41456 KB Output is correct
54 Correct 117 ms 50348 KB Output is correct
55 Correct 236 ms 50472 KB Output is correct
56 Correct 204 ms 55440 KB Output is correct
57 Correct 194 ms 58336 KB Output is correct
58 Correct 236 ms 56640 KB Output is correct
59 Correct 244 ms 56576 KB Output is correct
60 Correct 9 ms 14412 KB Output is correct
61 Correct 9 ms 14412 KB Output is correct
62 Correct 10 ms 14412 KB Output is correct
63 Correct 9 ms 14356 KB Output is correct
64 Correct 409 ms 62372 KB Output is correct
65 Correct 332 ms 50600 KB Output is correct
66 Correct 421 ms 69680 KB Output is correct
67 Correct 336 ms 50312 KB Output is correct
68 Correct 223 ms 37048 KB Output is correct
69 Correct 204 ms 33628 KB Output is correct
70 Correct 260 ms 51584 KB Output is correct
71 Correct 243 ms 47176 KB Output is correct
72 Correct 277 ms 59404 KB Output is correct
73 Correct 243 ms 59360 KB Output is correct
74 Correct 337 ms 56460 KB Output is correct
75 Correct 193 ms 48920 KB Output is correct
76 Correct 154 ms 48896 KB Output is correct
77 Correct 319 ms 56596 KB Output is correct
78 Correct 186 ms 48892 KB Output is correct
79 Correct 391 ms 71920 KB Output is correct
80 Correct 292 ms 59924 KB Output is correct
81 Correct 212 ms 49304 KB Output is correct
82 Correct 263 ms 59584 KB Output is correct
83 Correct 386 ms 53948 KB Output is correct
84 Correct 339 ms 58932 KB Output is correct
85 Correct 336 ms 58916 KB Output is correct
86 Correct 345 ms 58776 KB Output is correct
87 Correct 319 ms 58936 KB Output is correct
88 Correct 315 ms 58904 KB Output is correct