Submission #533749

#TimeUsernameProblemLanguageResultExecution timeMemory
5337491binRoad Closures (APIO21_roads)C++14
12 / 100
231 ms61360 KiB
#include <bits/stdc++.h> #include <cassert> using namespace std; #define all(v) v.begin(), v.end() typedef long long ll; const int NMAX = 1e5 + 5; set<pair<ll, ll>> adj[NMAX]; int dg[NMAX], vis[NMAX], lf[NMAX]; ll dp[NMAX][2], Sum[NMAX]; vector<int> D[NMAX]; set<int> X; multiset<ll> C[NMAX], L[NMAX], R[NMAX]; /* 5 0 1 1 0 2 4 0 3 3 2 4 2 */ /* 4 0 1 5 0 3 5 0 2 10 */ /* 5 0 1 5 1 2 6 2 3 1 3 4 8 */ void dfs(int now, int k) { vis[now] = 1; //e[now]--; ll& ret = dp[now][0] = Sum[now]; int e = lf[now], cd = 0; for (auto& p : adj[now]) { ll nx = p.first, w = p.second; if (vis[nx]) continue; dfs(nx, k); ll t = dp[nx][0] + w - dp[nx][1]; if (t <= 0) ret += dp[nx][0] + w; else { e++; cd++; ret += dp[nx][1]; C[now].emplace(t); } } e -= k; while (e - cd > L[now].size() && R[now].size()) { assert(R[now].size()); auto it = R[now].begin(); //cout << "put : " << now << ' ' << *it << '\n'; L[now].emplace(*it); Sum[now] += *it; ret += *it; R[now].erase(it); } while (e - cd < L[now].size() && L[now].size()) { assert(L[now].size()); auto it = --L[now].end(); Sum[now] -= *it; ret -= *it; //cout << "unput : " << now << ' ' << *it << '\n'; R[now].emplace(*it); L[now].erase(it); } //cout << "node is : " << now << ' ' << e << ' ' << ret << ' ' << L[now].size() << '\n'; // dp[now][0] auto it = R[now].begin(); for (int i = 0; i < cd && it != R[now].end(); i++) C[now].emplace(*it++); it = C[now].begin(); /*for (ll x : L[now]) cout << x << ' '; cout << '\n'; for (ll x : R[now]) cout << x << ' '; cout << '\n'; for (ll x : C[now]) cout << x << ' '; cout << '\n';*/ int sz = max(e - (int)L[now].size(), 0); while(sz--)/*assert(it != C[now].end()),*/ret += *it++; // dp[now][1] ll p = 0; //assert(C[now].size() || R[now].size()); if (it != C[now].end()) p = *it; else if(R[now].size()) p = *R[now].begin(); dp[now][1] = ret + p; //cout << "ans : " << dp[now][0] << ' ' << dp[now][1] << '\n'; C[now].clear(); return; } vector<ll> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) { vector<ll> ans(N, 0); for (int i = 0; i < N - 1; i++) { adj[U[i]].emplace(V[i], W[i]); adj[V[i]].emplace(U[i], W[i]); dg[U[i]]++; dg[V[i]]++; ans[0] += W[i]; } for (int i = 0; i < N; i++) { D[dg[i]].emplace_back(i); //e[i] = dg[i] - 1; X.emplace(i); } for (int k = 1; k < N; k++) { // 노드 삭제하기 //cout << "NOW TURN IS : " << k << '\n'; for (int x : D[k]) { X.erase(x); //cout << "erase : " << x << '\n'; for (auto& p : adj[x]) { ll nx = p.first, w = p.second; adj[nx].erase({ x, w }); lf[nx]++; R[nx].emplace(w); if (L[nx].size()) { auto l = --L[nx].end(); auto r = R[nx].begin(); ll lv = *l, rv = *r; if (lv > rv) { L[nx].erase(l); L[nx].emplace(rv); R[nx].erase(r); R[nx].emplace(lv); } } } } // 각 트리에 대해서 탐색 for (int x : X) if (!vis[x]) { dfs(x, k); ans[k] += dp[x][0]; //cout << "ans : " << dp[x][0] << '\n'; } for (int x : X) vis[x] = 0; } return ans; } //int main(void) { // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // // freopen("input.txt", "r", stdin); // int N; // cin >> N; // vector<int> U(N), V(N), W(N); // for (int i = 0; i < N - 1; i++) cin >> U[i] >> V[i] >> W[i]; // vector<ll> ans = minimum_closure_costs(N, U, V, W); // for (int i = 0; i < N; i++) cout << ans[i] << ' '; // return 0; //}

Compilation message (stderr)

roads.cpp: In function 'void dfs(int, int)':
roads.cpp:58:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |  while (e - cd > L[now].size() && R[now].size()) {
      |         ~~~~~~~^~~~~~~~~~~~~~~
roads.cpp:65:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  while (e - cd < L[now].size() && L[now].size()) {
      |         ~~~~~~~^~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...