Submission #232856

# Submission time Handle Problem Language Result Execution time Memory
232856 2020-05-18T11:36:37 Z oolimry Designated Cities (JOI19_designated_cities) C++14
100 / 100
423 ms 38936 KB
#include <bits/stdc++.h>
#define int long long
#define cost first
#define u second
using namespace std;
typedef pair<int,int> ii;

struct edge { int to, cost, rev; };
vector<edge> adj[200005];
int degree[200005];
int flipDist[200005];
int total = 0;

bool gone[200005];
int ans[200005];

void dfs(int u, int p){
	for(edge e : adj[u]){
		if(e.to == p) continue;
		total += e.cost;
		flipDist[e.to] = flipDist[u] - e.cost + e.rev;
		dfs(e.to, u);
	}
}

ii findParent(int u){
	for(edge e : adj[u]){
		if(!gone[e.to]) return ii(e.rev, e.to);
	}
}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	int n; cin >> n;
	for(int i = 1;i < n;i++){
		int a, b, c, d; cin >> a >> b >> c >> d;
		adj[a].push_back({b,c,d});
		adj[b].push_back({a,d,c});
		degree[a]++; degree[b]++;
	}
	
	///solve the E = 1 case
	dfs(1,0);
	ans[1] = total + *min_element(flipDist + 1, flipDist + n+1);
	
	///Other cases
	priority_queue<ii, vector<ii>, greater<ii> > leaves;
	for(int i = 1;i <= n;i++){
		if(degree[i] == 1) leaves.push(ii(findParent(i).cost, i));
	}
	
	while((int) leaves.size() > 2){
		int u = leaves.top().u; int cost = leaves.top().cost;
		leaves.pop();
		
		gone[u] = true;
		int p = findParent(u).second;
		degree[p]--;
		
		
		if(degree[p] == 1){
			leaves.push(ii(findParent(p).cost + cost, p));
		}
		else{
			ans[(int) leaves.size()] = ans[(int) leaves.size()+1] + cost;
		}	
	}
	
	int Q; cin >> Q;
	while(Q--){
		int x; cin >> x; cout << ans[x] << "\n";
	}
}

Compilation message

designated_cities.cpp: In function 'ii findParent(long long int)':
designated_cities.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 347 ms 23512 KB Output is correct
3 Correct 283 ms 29048 KB Output is correct
4 Correct 338 ms 23524 KB Output is correct
5 Correct 338 ms 24484 KB Output is correct
6 Correct 361 ms 24816 KB Output is correct
7 Correct 313 ms 25252 KB Output is correct
8 Correct 267 ms 29944 KB Output is correct
9 Correct 254 ms 28184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 341 ms 23528 KB Output is correct
3 Correct 259 ms 36652 KB Output is correct
4 Correct 346 ms 28528 KB Output is correct
5 Correct 351 ms 30880 KB Output is correct
6 Correct 383 ms 31592 KB Output is correct
7 Correct 267 ms 34200 KB Output is correct
8 Correct 315 ms 34688 KB Output is correct
9 Correct 249 ms 34340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 10 ms 5376 KB Output is correct
14 Correct 9 ms 5376 KB Output is correct
15 Correct 9 ms 5376 KB Output is correct
16 Correct 9 ms 5376 KB Output is correct
17 Correct 10 ms 5376 KB Output is correct
18 Correct 9 ms 5376 KB Output is correct
19 Correct 9 ms 5376 KB Output is correct
20 Correct 9 ms 5376 KB Output is correct
21 Correct 9 ms 5376 KB Output is correct
22 Correct 9 ms 5376 KB Output is correct
23 Correct 9 ms 5376 KB Output is correct
24 Correct 9 ms 5376 KB Output is correct
25 Correct 9 ms 5376 KB Output is correct
26 Correct 10 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 347 ms 23512 KB Output is correct
3 Correct 283 ms 29048 KB Output is correct
4 Correct 338 ms 23524 KB Output is correct
5 Correct 338 ms 24484 KB Output is correct
6 Correct 361 ms 24816 KB Output is correct
7 Correct 313 ms 25252 KB Output is correct
8 Correct 267 ms 29944 KB Output is correct
9 Correct 254 ms 28184 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 341 ms 23528 KB Output is correct
12 Correct 259 ms 36652 KB Output is correct
13 Correct 346 ms 28528 KB Output is correct
14 Correct 351 ms 30880 KB Output is correct
15 Correct 383 ms 31592 KB Output is correct
16 Correct 267 ms 34200 KB Output is correct
17 Correct 315 ms 34688 KB Output is correct
18 Correct 249 ms 34340 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 350 ms 29928 KB Output is correct
21 Correct 266 ms 36728 KB Output is correct
22 Correct 339 ms 28776 KB Output is correct
23 Correct 357 ms 30192 KB Output is correct
24 Correct 359 ms 29044 KB Output is correct
25 Correct 354 ms 30192 KB Output is correct
26 Correct 350 ms 29256 KB Output is correct
27 Correct 343 ms 30376 KB Output is correct
28 Correct 344 ms 31336 KB Output is correct
29 Correct 380 ms 30320 KB Output is correct
30 Correct 326 ms 29988 KB Output is correct
31 Correct 285 ms 32416 KB Output is correct
32 Correct 307 ms 34924 KB Output is correct
33 Correct 259 ms 34864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 347 ms 23512 KB Output is correct
14 Correct 283 ms 29048 KB Output is correct
15 Correct 338 ms 23524 KB Output is correct
16 Correct 338 ms 24484 KB Output is correct
17 Correct 361 ms 24816 KB Output is correct
18 Correct 313 ms 25252 KB Output is correct
19 Correct 267 ms 29944 KB Output is correct
20 Correct 254 ms 28184 KB Output is correct
21 Correct 7 ms 4992 KB Output is correct
22 Correct 341 ms 23528 KB Output is correct
23 Correct 259 ms 36652 KB Output is correct
24 Correct 346 ms 28528 KB Output is correct
25 Correct 351 ms 30880 KB Output is correct
26 Correct 383 ms 31592 KB Output is correct
27 Correct 267 ms 34200 KB Output is correct
28 Correct 315 ms 34688 KB Output is correct
29 Correct 249 ms 34340 KB Output is correct
30 Correct 7 ms 4992 KB Output is correct
31 Correct 10 ms 5376 KB Output is correct
32 Correct 9 ms 5376 KB Output is correct
33 Correct 9 ms 5376 KB Output is correct
34 Correct 9 ms 5376 KB Output is correct
35 Correct 10 ms 5376 KB Output is correct
36 Correct 9 ms 5376 KB Output is correct
37 Correct 9 ms 5376 KB Output is correct
38 Correct 9 ms 5376 KB Output is correct
39 Correct 9 ms 5376 KB Output is correct
40 Correct 9 ms 5376 KB Output is correct
41 Correct 9 ms 5376 KB Output is correct
42 Correct 9 ms 5376 KB Output is correct
43 Correct 9 ms 5376 KB Output is correct
44 Correct 10 ms 5376 KB Output is correct
45 Correct 7 ms 4992 KB Output is correct
46 Correct 350 ms 29928 KB Output is correct
47 Correct 266 ms 36728 KB Output is correct
48 Correct 339 ms 28776 KB Output is correct
49 Correct 357 ms 30192 KB Output is correct
50 Correct 359 ms 29044 KB Output is correct
51 Correct 354 ms 30192 KB Output is correct
52 Correct 350 ms 29256 KB Output is correct
53 Correct 343 ms 30376 KB Output is correct
54 Correct 344 ms 31336 KB Output is correct
55 Correct 380 ms 30320 KB Output is correct
56 Correct 326 ms 29988 KB Output is correct
57 Correct 285 ms 32416 KB Output is correct
58 Correct 307 ms 34924 KB Output is correct
59 Correct 259 ms 34864 KB Output is correct
60 Correct 7 ms 4992 KB Output is correct
61 Correct 401 ms 32624 KB Output is correct
62 Correct 310 ms 37112 KB Output is correct
63 Correct 371 ms 31212 KB Output is correct
64 Correct 423 ms 32872 KB Output is correct
65 Correct 411 ms 31460 KB Output is correct
66 Correct 394 ms 32872 KB Output is correct
67 Correct 384 ms 31724 KB Output is correct
68 Correct 411 ms 33192 KB Output is correct
69 Correct 405 ms 33768 KB Output is correct
70 Correct 384 ms 32616 KB Output is correct
71 Correct 362 ms 32680 KB Output is correct
72 Correct 348 ms 35220 KB Output is correct
73 Correct 343 ms 36588 KB Output is correct
74 Correct 286 ms 38936 KB Output is correct