Submission #376255

# Submission time Handle Problem Language Result Execution time Memory
376255 2021-03-11T06:12:44 Z Kevin_Zhang_TW Designated Cities (JOI19_designated_cities) C++17
100 / 100
657 ms 61808 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 200010;
const ll inf = 1ll << 59;
#define int ll

int n, q;
ll dprt[MAX_N], dpson[MAX_N], res[MAX_N], allsum;
ll goup[MAX_N];
vector<pair<int,int>> edge[MAX_N];

ll dfs1(int x, int lst = -1) {
	ll uplen = 0;
	for (auto [u, w] : edge[x]) if (u != lst) 
		dpson[x] += dfs1(u, x);
	else uplen = w;
	return dpson[x] + uplen;
}

void dfs2(int x, int lst = -1, ll upson = 0) {
	if (lst == -1) dprt[x] = dpson[x];
	dprt[x] += upson;
	for (auto [u, w] : edge[x]) if (u == lst)
		dprt[x] -= w;
	for (auto [u, w] : edge[x]) if (u != lst)
		dfs2(u, x, dprt[x] + w);
}

pair<int,int> best[MAX_N];
ll bstsum[MAX_N];

pair<ll,ll> mxlen[MAX_N];

void dfs3(int x, int lst = -1) {
	mxlen[x] = {0, x};
	for (auto [u, w] : edge[x]) if (u != lst) {
		dfs3(u, x);
		if (chmax(bstsum[x], bstsum[u]))
			best[x] = best[u];
		mxlen[u].first += w;

		if (chmax(bstsum[x], 
					mxlen[x].first + mxlen[u].first + dprt[x]))
			best[x] = {mxlen[x].second, mxlen[u].second};
		chmax(mxlen[x], mxlen[u]);
	}
}

int pa[MAX_N], paint[MAX_N];
ll gain[MAX_N];

void dfs4(int x, int lst = -1) {
	if (paint[x]) gain[x] = 0;
	pa[x] = lst;
	int sz = 0;
	for (auto [u, w] : edge[x]) if (u != lst) {
		gain[u] = gain[x] + w;
		dfs4(u, x);
		edge[x][sz++] = {u, w};
	}
	edge[x].resize(sz);
}

void dfs6(int x, int lst = -1) {
	for (auto [u, w] : edge[x]) if (u != lst) {
		dfs6(u, x);
	} else assert(false);

	sort(AI(edge[x]), [&](pair<int,int> a, pair<int,int> b) {
		return gain[a.first] > gain[b.first];
	});

	if (edge[x].size())
		gain[x] = gain[ edge[x][0].first ];

	if (paint[x]) gain[x] = 0;
}
int rt;
void takeout(int x) {
	while (x != -1) {
		paint[x] = true;
		x = pa[x];
	}
	dfs4(rt);
}

vector<ll> cand;

void dfs5(int x, int lst = -1, ll sum = 0) {
	if (paint[x]) sum = 0;
	for (auto [u, w] : edge[x]) if (u != lst) {
		dfs5(u, x, sum + w);
		sum = 0;
	}
	cand.pb(sum);
}

void solve() {
	dfs3(1);
	res[2] = bstsum[1];
	rt = best[1].first;
	dfs4(rt);
	takeout(best[1].second);

	dfs6(rt);
	dfs5(rt);
	sort(AI(cand));
	for (int i = 3;i <= n;++i) {
		res[i] = res[i-1];
		if (cand.size()) {
			res[i] += cand.back();
			cand.pop_back();
		}
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int a, b, c, d, i = 1;i < n;++i) {
		cin >> a >> b >> c >> d;
		edge[a].pb(b, c);
		edge[b].pb(a, d);
		allsum += c + d;
	}

	dfs1(1);
	dfs2(1);

	res[1] = *max_element(dprt+1, dprt+1+n);

	solve();

	cin >> q;

	for (int i = 0, v;i < q;++i) {
		cin >> v;
		cout << allsum - res[v] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5228 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5228 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5248 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5132 KB Output is correct
2 Correct 485 ms 33664 KB Output is correct
3 Correct 602 ms 53212 KB Output is correct
4 Correct 501 ms 32864 KB Output is correct
5 Correct 446 ms 32868 KB Output is correct
6 Correct 504 ms 36828 KB Output is correct
7 Correct 372 ms 32356 KB Output is correct
8 Correct 602 ms 52864 KB Output is correct
9 Correct 256 ms 27476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 466 ms 33888 KB Output is correct
3 Correct 628 ms 55376 KB Output is correct
4 Correct 466 ms 33376 KB Output is correct
5 Correct 440 ms 32864 KB Output is correct
6 Correct 544 ms 37596 KB Output is correct
7 Correct 292 ms 33760 KB Output is correct
8 Correct 542 ms 46428 KB Output is correct
9 Correct 274 ms 27612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5228 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5228 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5248 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 6 ms 5484 KB Output is correct
14 Correct 6 ms 5740 KB Output is correct
15 Correct 7 ms 5484 KB Output is correct
16 Correct 6 ms 5484 KB Output is correct
17 Correct 6 ms 5484 KB Output is correct
18 Correct 6 ms 5484 KB Output is correct
19 Correct 6 ms 5484 KB Output is correct
20 Correct 6 ms 5484 KB Output is correct
21 Correct 6 ms 5612 KB Output is correct
22 Correct 6 ms 5484 KB Output is correct
23 Correct 7 ms 5612 KB Output is correct
24 Correct 7 ms 5484 KB Output is correct
25 Correct 6 ms 5740 KB Output is correct
26 Correct 6 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5132 KB Output is correct
2 Correct 485 ms 33664 KB Output is correct
3 Correct 602 ms 53212 KB Output is correct
4 Correct 501 ms 32864 KB Output is correct
5 Correct 446 ms 32868 KB Output is correct
6 Correct 504 ms 36828 KB Output is correct
7 Correct 372 ms 32356 KB Output is correct
8 Correct 602 ms 52864 KB Output is correct
9 Correct 256 ms 27476 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 466 ms 33888 KB Output is correct
12 Correct 628 ms 55376 KB Output is correct
13 Correct 466 ms 33376 KB Output is correct
14 Correct 440 ms 32864 KB Output is correct
15 Correct 544 ms 37596 KB Output is correct
16 Correct 292 ms 33760 KB Output is correct
17 Correct 542 ms 46428 KB Output is correct
18 Correct 274 ms 27612 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 486 ms 33632 KB Output is correct
21 Correct 654 ms 61808 KB Output is correct
22 Correct 454 ms 38116 KB Output is correct
23 Correct 474 ms 40540 KB Output is correct
24 Correct 477 ms 38280 KB Output is correct
25 Correct 499 ms 40540 KB Output is correct
26 Correct 474 ms 38140 KB Output is correct
27 Correct 456 ms 39260 KB Output is correct
28 Correct 495 ms 43488 KB Output is correct
29 Correct 480 ms 41484 KB Output is correct
30 Correct 470 ms 37980 KB Output is correct
31 Correct 329 ms 38628 KB Output is correct
32 Correct 559 ms 53372 KB Output is correct
33 Correct 289 ms 34260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5228 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5228 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5248 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5132 KB Output is correct
13 Correct 485 ms 33664 KB Output is correct
14 Correct 602 ms 53212 KB Output is correct
15 Correct 501 ms 32864 KB Output is correct
16 Correct 446 ms 32868 KB Output is correct
17 Correct 504 ms 36828 KB Output is correct
18 Correct 372 ms 32356 KB Output is correct
19 Correct 602 ms 52864 KB Output is correct
20 Correct 256 ms 27476 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 466 ms 33888 KB Output is correct
23 Correct 628 ms 55376 KB Output is correct
24 Correct 466 ms 33376 KB Output is correct
25 Correct 440 ms 32864 KB Output is correct
26 Correct 544 ms 37596 KB Output is correct
27 Correct 292 ms 33760 KB Output is correct
28 Correct 542 ms 46428 KB Output is correct
29 Correct 274 ms 27612 KB Output is correct
30 Correct 4 ms 5100 KB Output is correct
31 Correct 6 ms 5484 KB Output is correct
32 Correct 6 ms 5740 KB Output is correct
33 Correct 7 ms 5484 KB Output is correct
34 Correct 6 ms 5484 KB Output is correct
35 Correct 6 ms 5484 KB Output is correct
36 Correct 6 ms 5484 KB Output is correct
37 Correct 6 ms 5484 KB Output is correct
38 Correct 6 ms 5484 KB Output is correct
39 Correct 6 ms 5612 KB Output is correct
40 Correct 6 ms 5484 KB Output is correct
41 Correct 7 ms 5612 KB Output is correct
42 Correct 7 ms 5484 KB Output is correct
43 Correct 6 ms 5740 KB Output is correct
44 Correct 6 ms 5484 KB Output is correct
45 Correct 4 ms 5100 KB Output is correct
46 Correct 486 ms 33632 KB Output is correct
47 Correct 654 ms 61808 KB Output is correct
48 Correct 454 ms 38116 KB Output is correct
49 Correct 474 ms 40540 KB Output is correct
50 Correct 477 ms 38280 KB Output is correct
51 Correct 499 ms 40540 KB Output is correct
52 Correct 474 ms 38140 KB Output is correct
53 Correct 456 ms 39260 KB Output is correct
54 Correct 495 ms 43488 KB Output is correct
55 Correct 480 ms 41484 KB Output is correct
56 Correct 470 ms 37980 KB Output is correct
57 Correct 329 ms 38628 KB Output is correct
58 Correct 559 ms 53372 KB Output is correct
59 Correct 289 ms 34260 KB Output is correct
60 Correct 4 ms 5100 KB Output is correct
61 Correct 550 ms 42276 KB Output is correct
62 Correct 657 ms 60636 KB Output is correct
63 Correct 500 ms 40420 KB Output is correct
64 Correct 513 ms 42632 KB Output is correct
65 Correct 485 ms 40668 KB Output is correct
66 Correct 496 ms 42676 KB Output is correct
67 Correct 515 ms 40600 KB Output is correct
68 Correct 481 ms 41692 KB Output is correct
69 Correct 528 ms 45364 KB Output is correct
70 Correct 543 ms 43228 KB Output is correct
71 Correct 487 ms 40672 KB Output is correct
72 Correct 395 ms 41048 KB Output is correct
73 Correct 609 ms 55260 KB Output is correct
74 Correct 336 ms 37728 KB Output is correct