Submission #993837

#TimeUsernameProblemLanguageResultExecution timeMemory
993837prvocisloDesignated Cities (JOI19_designated_cities)C++17
7 / 100
173 ms41500 KiB
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> typedef long long ll; typedef long double ld; using namespace std; struct edge { int u, v; ll cuv, cvu, c2; }; vector<vector<edge> > g; vector<ll> dps, dp, d, v; void dfs1a(int u, int p = -1) { for (edge e : g[u]) if (e.v != p) { dfs1a(e.v, u); dps[u] += dps[e.v] + e.cvu; } } void dfs1b(int u, int p = -1, ll nad = 0) { dp[u] = dps[u] + nad; for (edge e : g[u]) if (e.v != p) { dfs1b(e.v, u, nad + dps[u] - dps[e.v] - e.cvu + e.cuv); } } pair<ll, int> aa = {-1, -1}; ll dfs(int u, int p = -1) { vector<ll> my; for (edge e : g[u]) if (e.v != p) { my.push_back(dfs(e.v, u) + e.cuv); } sort(my.begin(), my.end(), greater<ll>()); for (int i = 1; i < my.size(); i++) v.push_back(my[i]); my.push_back(0), my.push_back(0); aa = max(aa, make_pair(dp[u] + my[0] + my[1], u)); return my[0]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; g.assign(n, {}), dps.assign(n, 0), dp.assign(n, 0), d.assign(n, 0), v.clear(); ll sum = 0; for (int i = 0; i < n - 1; i++) { edge e; cin >> e.u >> e.v >> e.cuv >> e.cvu; e.u--, e.v--, e.c2 = e.cuv + e.cvu; g[e.u].push_back(e); swap(e.u, e.v), swap(e.cuv, e.cvu); g[e.u].push_back(e); sum += e.c2; } dfs1a(0); dfs1b(0); vector<ll> ans(n + 1, 0); ans[1] = *max_element(dp.begin(), dp.end()); bool f = false; int q; cin >> q; while (q--) { int x; cin >> x; if (x == 1) cout << (sum - ans[x]) << "\n"; else { if (!f) { dfs(0); int a = aa.second; v.clear(); v.push_back(dfs(a)); sort(v.begin(), v.end(), greater<ll>()); ll s = dp[a]; int f = (g[a].size() == 1 ? 1 : 0); for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 1 + f] = max(ans[i + 1 + f], s); for (int i = 1; i <= n; i++) ans[i] = max(ans[i], ans[i - 1]); f = true; } cout << (sum - ans[x]) << "\n"; } } return 0; }

Compilation message (stderr)

designated_cities.cpp: In function 'll dfs(int, int)':
designated_cities.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int i = 1; i < my.size(); i++) v.push_back(my[i]);
      |                     ~~^~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:98:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |                 for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 1 + f] = max(ans[i + 1 + f], s);
      |                                 ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...