Submission #220167

# Submission time Handle Problem Language Result Execution time Memory
220167 2020-04-07T07:43:03 Z manh9203 Designated Cities (JOI19_designated_cities) C++17
100 / 100
456 ms 55416 KB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second
typedef pair<ll, ll> pii;
const int N = 2e5 + 5;

int n, q;
vector<pair<int, pii> > adj[N];
ll curSum, Max, root;
ll ans[N], sum[N];
pii maxChild[N];
int par[N], val[N];
int check[N];

ll dfs1(int u, int p) {
	ll res = 0;
	for (auto e : adj[u]) {
		int v = e.fi;
		if (v != p) {
			res += dfs1(v, u) + e.se.se;
		}
	}
	return res;
}

void dfs2(int u, int p) {
	Max = max(Max, curSum);
	sum[u] = curSum;
	for (auto e : adj[u]) {
		int v = e.fi;
		if (v != p) {
			curSum += e.se.fi - e.se.se;
			dfs2(v, u);
			curSum += e.se.se - e.se.fi;
		}
	}
}

ll dist[N], distVal[N];
ll maxVer[N];
ll mx;
pair<int, int> best;

void dfs3(int u, int p) {
	distVal[u] = sum[u] + dist[u];
	maxVer[u] = u;
	for (auto e : adj[u]) {
		int v = e.fi;
		if (v != p) {
			dist[v] = dist[u] + e.se.fi + e.se.se;
			dfs3(v, u);
			if (distVal[maxVer[v]] + distVal[maxVer[u]] - 2 * dist[u] > mx) {
				mx = distVal[maxVer[v]] + distVal[maxVer[u]] - 2 * dist[u];
				best = {maxVer[u], maxVer[v]};
			}
			if (distVal[maxVer[v]] > distVal[maxVer[u]]) {
				maxVer[u] = maxVer[v];
			}
		}
	}
}

void dfs(int u, int p) {
	maxChild[u] = {val[u], u};
	for (auto e : adj[u]) {
		int v = e.fi;
		if (v != p) {
			par[v] = u;
			val[v] = e.se.fi;
			dfs(v, u);
			if (maxChild[v].fi + val[u] > maxChild[u].fi) {
				maxChild[u].fi = maxChild[v].fi + val[u];
				maxChild[u].se = maxChild[v].se;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	cin >> n;
	ll sumEdge = 0;
	for (int i = 1; i < n; i++) {
		int u, v, a, b;
		cin >> u >> v >> a >> b;
		adj[u].push_back({v, {a, b}});
		adj[v].push_back({u, {b, a}});
		sumEdge += a + b;
	}
	curSum = dfs1(1, 1); 
	dfs2(1, 1);
	
	dfs3(1, 1);
	root = best.fi;
	ans[1] = sum[root];
	dfs(root, root);
	priority_queue<pii> pq;
	pq.push(maxChild[root]);
	for (int i = 2; i <= n; i++) {
		ans[i] = ans[i - 1];
		if (pq.size() > 0) {
			ans[i] += pq.top().fi;
			int u = pq.top().se;
			pq.pop();
			while (u != 0 && check[u] == 0) {
				check[u] = 1;
				for (auto e : adj[u]) {
					if (e.fi != par[u] && check[e.fi] == 0) {
						pq.push(maxChild[e.fi]);
					}
				}
				u = par[u];
			}
		}
	}
	ans[1] = Max;
	
	cin >> q;
	while (q--) {
		int x;
		cin >> x;
		cout << sumEdge - ans[x] << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 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 7 ms 5120 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 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 402 ms 38816 KB Output is correct
3 Correct 442 ms 53496 KB Output is correct
4 Correct 352 ms 36600 KB Output is correct
5 Correct 367 ms 39604 KB Output is correct
6 Correct 390 ms 41040 KB Output is correct
7 Correct 312 ms 40480 KB Output is correct
8 Correct 411 ms 53112 KB Output is correct
9 Correct 264 ms 43036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 374 ms 38844 KB Output is correct
3 Correct 425 ms 53568 KB Output is correct
4 Correct 338 ms 36472 KB Output is correct
5 Correct 365 ms 39852 KB Output is correct
6 Correct 390 ms 41576 KB Output is correct
7 Correct 291 ms 42924 KB Output is correct
8 Correct 403 ms 49004 KB Output is correct
9 Correct 261 ms 42776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 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 7 ms 5120 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 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 9 ms 5504 KB Output is correct
14 Correct 9 ms 5632 KB Output is correct
15 Correct 9 ms 5504 KB Output is correct
16 Correct 9 ms 5504 KB Output is correct
17 Correct 9 ms 5504 KB Output is correct
18 Correct 10 ms 5504 KB Output is correct
19 Correct 9 ms 5504 KB Output is correct
20 Correct 9 ms 5504 KB Output is correct
21 Correct 9 ms 5504 KB Output is correct
22 Correct 11 ms 5504 KB Output is correct
23 Correct 9 ms 5504 KB Output is correct
24 Correct 9 ms 5504 KB Output is correct
25 Correct 9 ms 5632 KB Output is correct
26 Correct 9 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 402 ms 38816 KB Output is correct
3 Correct 442 ms 53496 KB Output is correct
4 Correct 352 ms 36600 KB Output is correct
5 Correct 367 ms 39604 KB Output is correct
6 Correct 390 ms 41040 KB Output is correct
7 Correct 312 ms 40480 KB Output is correct
8 Correct 411 ms 53112 KB Output is correct
9 Correct 264 ms 43036 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 374 ms 38844 KB Output is correct
12 Correct 425 ms 53568 KB Output is correct
13 Correct 338 ms 36472 KB Output is correct
14 Correct 365 ms 39852 KB Output is correct
15 Correct 390 ms 41576 KB Output is correct
16 Correct 291 ms 42924 KB Output is correct
17 Correct 403 ms 49004 KB Output is correct
18 Correct 261 ms 42776 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 373 ms 38876 KB Output is correct
21 Correct 400 ms 53496 KB Output is correct
22 Correct 360 ms 36600 KB Output is correct
23 Correct 365 ms 39128 KB Output is correct
24 Correct 361 ms 38000 KB Output is correct
25 Correct 372 ms 39152 KB Output is correct
26 Correct 360 ms 37864 KB Output is correct
27 Correct 351 ms 39144 KB Output is correct
28 Correct 381 ms 41200 KB Output is correct
29 Correct 375 ms 39452 KB Output is correct
30 Correct 344 ms 38312 KB Output is correct
31 Correct 306 ms 40604 KB Output is correct
32 Correct 421 ms 49516 KB Output is correct
33 Correct 269 ms 43292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 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 7 ms 5120 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 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 402 ms 38816 KB Output is correct
14 Correct 442 ms 53496 KB Output is correct
15 Correct 352 ms 36600 KB Output is correct
16 Correct 367 ms 39604 KB Output is correct
17 Correct 390 ms 41040 KB Output is correct
18 Correct 312 ms 40480 KB Output is correct
19 Correct 411 ms 53112 KB Output is correct
20 Correct 264 ms 43036 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 374 ms 38844 KB Output is correct
23 Correct 425 ms 53568 KB Output is correct
24 Correct 338 ms 36472 KB Output is correct
25 Correct 365 ms 39852 KB Output is correct
26 Correct 390 ms 41576 KB Output is correct
27 Correct 291 ms 42924 KB Output is correct
28 Correct 403 ms 49004 KB Output is correct
29 Correct 261 ms 42776 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 9 ms 5504 KB Output is correct
32 Correct 9 ms 5632 KB Output is correct
33 Correct 9 ms 5504 KB Output is correct
34 Correct 9 ms 5504 KB Output is correct
35 Correct 9 ms 5504 KB Output is correct
36 Correct 10 ms 5504 KB Output is correct
37 Correct 9 ms 5504 KB Output is correct
38 Correct 9 ms 5504 KB Output is correct
39 Correct 9 ms 5504 KB Output is correct
40 Correct 11 ms 5504 KB Output is correct
41 Correct 9 ms 5504 KB Output is correct
42 Correct 9 ms 5504 KB Output is correct
43 Correct 9 ms 5632 KB Output is correct
44 Correct 9 ms 5504 KB Output is correct
45 Correct 7 ms 5120 KB Output is correct
46 Correct 373 ms 38876 KB Output is correct
47 Correct 400 ms 53496 KB Output is correct
48 Correct 360 ms 36600 KB Output is correct
49 Correct 365 ms 39128 KB Output is correct
50 Correct 361 ms 38000 KB Output is correct
51 Correct 372 ms 39152 KB Output is correct
52 Correct 360 ms 37864 KB Output is correct
53 Correct 351 ms 39144 KB Output is correct
54 Correct 381 ms 41200 KB Output is correct
55 Correct 375 ms 39452 KB Output is correct
56 Correct 344 ms 38312 KB Output is correct
57 Correct 306 ms 40604 KB Output is correct
58 Correct 421 ms 49516 KB Output is correct
59 Correct 269 ms 43292 KB Output is correct
60 Correct 7 ms 5120 KB Output is correct
61 Correct 398 ms 41576 KB Output is correct
62 Correct 456 ms 55416 KB Output is correct
63 Correct 391 ms 39288 KB Output is correct
64 Correct 409 ms 41812 KB Output is correct
65 Correct 409 ms 40428 KB Output is correct
66 Correct 409 ms 41840 KB Output is correct
67 Correct 445 ms 40412 KB Output is correct
68 Correct 393 ms 42028 KB Output is correct
69 Correct 418 ms 43756 KB Output is correct
70 Correct 430 ms 41988 KB Output is correct
71 Correct 381 ms 41000 KB Output is correct
72 Correct 354 ms 43812 KB Output is correct
73 Correct 445 ms 51692 KB Output is correct
74 Correct 303 ms 47268 KB Output is correct