답안 #409969

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409969 2021-05-21T19:57:17 Z LucaDantas Designated Cities (JOI19_designated_cities) C++17
100 / 100
1293 ms 52796 KB
// Se eu pegar um cara qualquer e definir que ele vai ser um resort e minha raiz
// eu posso fazer um guloso escolhendo sempre o cara mais fundo e atualizando a profundidade
// dos outros, o problema é que eu nao necessariamente preciso pegar essa raiz
// entao eu teria q brutar todas as possiveis, o que ficaria n^2, já pega n<=2000 though
// Faço um centroid e em cada nivel eu escolho sempre caras de pelo menos duas subarvores
// diferentes, se a melhor resposta n tiver ai so descer nos filhos e ta safe
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
#include <cassert>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep = ""; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

#ifdef MY_DEBUG_FLAG
void debug() { cerr << endl; }
template<typename Ini, typename... Fim> void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << ", "; debug(F...);}
#define db(...) cerr << "DEBUG (" << #__VA_ARGS__ << ") == ", debug(__VA_ARGS__)
#else
#define db(...) 1
#endif

using ll = long long;

#define int ll

#define pb push_back
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

constexpr int maxn = 2e5+10;

vector<pair<int,int>> g[maxn];

ll ans[maxn];

struct Centroid {
	bool mark[maxn]; int sz[maxn];
	void pre(int u, int p = 0) {
		sz[u] = 1;
		for(auto [v, w] : g[u]) if(!mark[v] && v != p)
			pre(v, u), sz[u] += sz[v];
	}
	int get(int u) {
		for(auto [v, w] : g[u]) if((sz[v] << 1) > sz[u] && !mark[v])
			return (sz[u] -= sz[v], sz[v] += sz[u], get(v));
		return u;
	}
	
	ll dfs(int u, int p, ll& cost, vector<pair<ll,int>>& guloso, int idx) {
		vector<ll> td;
		ll maior = 0;
		
		for(auto [v, w] : g[u]) {
			if(v == p) cost += w;
			else if(!mark[v]) {
				ll gld = dfs(v, u, cost, guloso, idx) + w;
				td.pb(gld);
				if(gld > maior) maior = gld;
			}
		}
		
		bool ok = 0;
		for(ll a : td) {
			if(a == maior && !ok) ok = 1;
			else guloso.pb({a, idx});
		}

		return maior;
	}

	void decompose(int u, ll add) {
		pre(u); u = get(u);

		mark[u] = 1;
		
		vector<ll> custo;
		
		vector<pair<ll,int>> guloso;
		
		int idx = 0;
		ll custo_tot = 0;
		
		guloso.pb({0, -1});
		for(auto [v, w] : g[u]) {
			if(mark[v]) continue;
			ll cost = 0;
			ll gld = dfs(v, u, cost, guloso, idx) + w;
			guloso.pb({gld, idx});
			custo.pb(cost); custo_tot += cost;
			++idx;
		}

		// db(u, add, guloso, custo, custo_tot);
		
		sort(all(guloso));
		reverse(all(guloso));

		ans[1] = max(ans[1], custo_tot + add);

		if(sz(guloso) > 1) {
			idx = guloso.front().second;

			int primeiro = 0;
			for(int i = 0; i < sz(guloso); i++)
				if(guloso[i].second != idx) { primeiro = i; break; }
			assert(primeiro >= 1);
			
			ll bom = custo_tot, foi = guloso[primeiro].first, ok = 1;
			
			for(int i = 0; i < sz(guloso); i++) {
				bom += guloso[i].first;
				if(primeiro == i) foi = 0, ok = 0;
				ans[i + 1 + ok] = max(ans[i + 1 + ok], bom + foi + add);
			}
		}

		ll val = 0;

		vector<ll> suf;
		for(int i = sz(custo)-1; i >= 0; i--)
			suf.pb(val), val += custo[i];
		reverse(all(suf));

		val = 0;
		int i = 0;
		for(auto [v, w] : g[u]) {
			if(mark[v]) continue;
			decompose(v, val + suf[i] + w + add);
			val += custo[i];
			++i;
		}
	}
} centroid;

ll aq[maxn];

struct AAA
{
	int v, w, back;
};

vector<AAA> g_esp[maxn];

void pre_caso1(int u, int p) {
	for(auto [v, w, back] : g_esp[u]) {
		if(v == p) continue;
		pre_caso1(v, u);
		aq[u] += aq[v] + back;
	}
}

void caso1(int u, int p) {
	ans[1] = max(ans[1], aq[u]);
	for(auto [v, w, back] : g_esp[u]) {
		if(v == p) continue;
		aq[v] = w + aq[u] - back;
		caso1(v, u);
	}
}

int32_t main() {
	int n; scanf("%lld", &n);
	ll tot = 0;
	for(int i = 1; i < n; i++) {
		int a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
		g[a].pb({b, c});
		g[b].pb({a, d});
		tot += c + d;

		g_esp[a].pb({b, c, d});
		g_esp[b].pb({a, d, c});
	}

	centroid.decompose(1, 0);

	for(int i = 1; i <= n; i++)
		ans[i] = max(ans[i], ans[i-1]);
	
	// pre_caso1(1, 0);
	// caso1(1, 0);


	int q; scanf("%lld", &q);
	while(q--) {
		int e; scanf("%lld", &e);
		printf("%lld\n", tot - ans[e]);
	}
}

Compilation message

designated_cities.cpp: In function 'int32_t main()':
designated_cities.cpp:166:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  166 |  int n; scanf("%lld", &n);
      |         ~~~~~^~~~~~~~~~~~
designated_cities.cpp:169:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |   int a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
      |                   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:187:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |  int q; scanf("%lld", &q);
      |         ~~~~~^~~~~~~~~~~~
designated_cities.cpp:189:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  189 |   int e; scanf("%lld", &e);
      |          ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9608 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 8 ms 9692 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9696 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 915 ms 39128 KB Output is correct
3 Correct 1122 ms 47620 KB Output is correct
4 Correct 868 ms 41440 KB Output is correct
5 Correct 522 ms 40860 KB Output is correct
6 Correct 1075 ms 43156 KB Output is correct
7 Correct 398 ms 41720 KB Output is correct
8 Correct 1217 ms 47820 KB Output is correct
9 Correct 273 ms 45076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9700 KB Output is correct
2 Correct 916 ms 41584 KB Output is correct
3 Correct 1154 ms 47580 KB Output is correct
4 Correct 916 ms 41456 KB Output is correct
5 Correct 472 ms 40900 KB Output is correct
6 Correct 1139 ms 43452 KB Output is correct
7 Correct 295 ms 45736 KB Output is correct
8 Correct 1293 ms 46524 KB Output is correct
9 Correct 282 ms 44972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9608 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 8 ms 9692 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9696 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 6 ms 9588 KB Output is correct
13 Correct 11 ms 10060 KB Output is correct
14 Correct 10 ms 10060 KB Output is correct
15 Correct 11 ms 10060 KB Output is correct
16 Correct 11 ms 10100 KB Output is correct
17 Correct 12 ms 10060 KB Output is correct
18 Correct 10 ms 10060 KB Output is correct
19 Correct 11 ms 10100 KB Output is correct
20 Correct 9 ms 10096 KB Output is correct
21 Correct 11 ms 10104 KB Output is correct
22 Correct 10 ms 10060 KB Output is correct
23 Correct 10 ms 10060 KB Output is correct
24 Correct 9 ms 10060 KB Output is correct
25 Correct 11 ms 10100 KB Output is correct
26 Correct 9 ms 10060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 915 ms 39128 KB Output is correct
3 Correct 1122 ms 47620 KB Output is correct
4 Correct 868 ms 41440 KB Output is correct
5 Correct 522 ms 40860 KB Output is correct
6 Correct 1075 ms 43156 KB Output is correct
7 Correct 398 ms 41720 KB Output is correct
8 Correct 1217 ms 47820 KB Output is correct
9 Correct 273 ms 45076 KB Output is correct
10 Correct 7 ms 9700 KB Output is correct
11 Correct 916 ms 41584 KB Output is correct
12 Correct 1154 ms 47580 KB Output is correct
13 Correct 916 ms 41456 KB Output is correct
14 Correct 472 ms 40900 KB Output is correct
15 Correct 1139 ms 43452 KB Output is correct
16 Correct 295 ms 45736 KB Output is correct
17 Correct 1293 ms 46524 KB Output is correct
18 Correct 282 ms 44972 KB Output is correct
19 Correct 6 ms 9716 KB Output is correct
20 Correct 929 ms 41216 KB Output is correct
21 Correct 1176 ms 47792 KB Output is correct
22 Correct 879 ms 41400 KB Output is correct
23 Correct 915 ms 41732 KB Output is correct
24 Correct 905 ms 42188 KB Output is correct
25 Correct 932 ms 45560 KB Output is correct
26 Correct 932 ms 43972 KB Output is correct
27 Correct 456 ms 43844 KB Output is correct
28 Correct 1117 ms 46860 KB Output is correct
29 Correct 905 ms 45060 KB Output is correct
30 Correct 791 ms 45376 KB Output is correct
31 Correct 348 ms 45932 KB Output is correct
32 Correct 1222 ms 50220 KB Output is correct
33 Correct 283 ms 48712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9608 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 8 ms 9692 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9696 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9676 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 915 ms 39128 KB Output is correct
14 Correct 1122 ms 47620 KB Output is correct
15 Correct 868 ms 41440 KB Output is correct
16 Correct 522 ms 40860 KB Output is correct
17 Correct 1075 ms 43156 KB Output is correct
18 Correct 398 ms 41720 KB Output is correct
19 Correct 1217 ms 47820 KB Output is correct
20 Correct 273 ms 45076 KB Output is correct
21 Correct 7 ms 9700 KB Output is correct
22 Correct 916 ms 41584 KB Output is correct
23 Correct 1154 ms 47580 KB Output is correct
24 Correct 916 ms 41456 KB Output is correct
25 Correct 472 ms 40900 KB Output is correct
26 Correct 1139 ms 43452 KB Output is correct
27 Correct 295 ms 45736 KB Output is correct
28 Correct 1293 ms 46524 KB Output is correct
29 Correct 282 ms 44972 KB Output is correct
30 Correct 6 ms 9588 KB Output is correct
31 Correct 11 ms 10060 KB Output is correct
32 Correct 10 ms 10060 KB Output is correct
33 Correct 11 ms 10060 KB Output is correct
34 Correct 11 ms 10100 KB Output is correct
35 Correct 12 ms 10060 KB Output is correct
36 Correct 10 ms 10060 KB Output is correct
37 Correct 11 ms 10100 KB Output is correct
38 Correct 9 ms 10096 KB Output is correct
39 Correct 11 ms 10104 KB Output is correct
40 Correct 10 ms 10060 KB Output is correct
41 Correct 10 ms 10060 KB Output is correct
42 Correct 9 ms 10060 KB Output is correct
43 Correct 11 ms 10100 KB Output is correct
44 Correct 9 ms 10060 KB Output is correct
45 Correct 6 ms 9716 KB Output is correct
46 Correct 929 ms 41216 KB Output is correct
47 Correct 1176 ms 47792 KB Output is correct
48 Correct 879 ms 41400 KB Output is correct
49 Correct 915 ms 41732 KB Output is correct
50 Correct 905 ms 42188 KB Output is correct
51 Correct 932 ms 45560 KB Output is correct
52 Correct 932 ms 43972 KB Output is correct
53 Correct 456 ms 43844 KB Output is correct
54 Correct 1117 ms 46860 KB Output is correct
55 Correct 905 ms 45060 KB Output is correct
56 Correct 791 ms 45376 KB Output is correct
57 Correct 348 ms 45932 KB Output is correct
58 Correct 1222 ms 50220 KB Output is correct
59 Correct 283 ms 48712 KB Output is correct
60 Correct 6 ms 9676 KB Output is correct
61 Correct 938 ms 46468 KB Output is correct
62 Correct 1181 ms 52796 KB Output is correct
63 Correct 928 ms 44112 KB Output is correct
64 Correct 884 ms 46416 KB Output is correct
65 Correct 940 ms 44608 KB Output is correct
66 Correct 933 ms 47208 KB Output is correct
67 Correct 949 ms 44976 KB Output is correct
68 Correct 527 ms 46256 KB Output is correct
69 Correct 1114 ms 48720 KB Output is correct
70 Correct 969 ms 47928 KB Output is correct
71 Correct 845 ms 45060 KB Output is correct
72 Correct 435 ms 47588 KB Output is correct
73 Correct 1285 ms 52496 KB Output is correct
74 Correct 342 ms 49816 KB Output is correct