제출 #981468

#제출 시각아이디문제언어결과실행 시간메모리
981468sleepntsheep도로 폐쇄 (APIO21_roads)C++11
0 / 100
180 ms41076 KiB
#include <stdlib.h> #include <stdio.h> #define assert(x) if(!(x))__builtin_trap() void push(int **eh, int *eo, int i, int j) { int o = eo[i]++; if (!o) eh[i] = (int*)malloc(sizeof**eh*2); else if (0 == (o&(o-1))) eh[i] = (int*)realloc(eh[i], sizeof**eh*2*o); eh[i][o] = j; } #define NN 100000 #define MM (2*NN) #define PL (1<<22) int K[PL], L[PL], R[PL], S[PL], cur; long long Sum[PL]; unsigned P[PL]; int node(int key) { int p = ++cur; Sum[p] = K[p] = key; L[p] = R[p] = 0; S[p] = 1; P[p] = (unsigned)rand(); return p; } void pul(int v) { if (v) { S[v] = S[L[v]] + S[R[v]] + 1; Sum[v] = K[v] + Sum[L[v]] + Sum[R[v]]; } } void merge(int *v, int l, int r) { if (l == 0 || r == 0) { *v = l ^ r; return; } else if (P[l] < P[r]) { merge(L+r, l, L[r]); *v = r; } else { merge(R+l, R[l], r); *v = l; } pul(*v); } void split(int v, int *l, int *r, int key) { if (!v) *l = 0, *r = 0; else if (K[v] <= key) split(R[v], R+v, r, key), *l = v; else split(L[v], l, L+v, key), *r = v; pul(v); } void spliti(int v, int *l, int *r, int sz) { if (!v) *l = 0, *r = 0; else if (S[L[v]] < sz) split(R[v], R+v, r, sz - 1 - S[L[v]]), *l = v; else split(L[v], l, L+v, sz), *r = v; pul(v); } void add(int *v, int key) { int t1, t2, t3, t4; split(*v, &t2, &t3, key); split(t2, &t1, &t2, key - 1); merge(&t2, t2, node(key)); merge(&t4, t1, t2); merge(v, t4, t3); } void remove(int *v, int key) { int t1, t2, t3, t4, t5; split(*v, &t2, &t3, key); split(t2, &t1, &t2, key - 1); spliti(t2, &t4, &t5, 1); merge(&t1, t1, t5); merge(v, t1, t3); } long long min_k_sum(int v, int k) { if (!v || !k) return 0; if (S[L[v]] >= k) return min_k_sum(L[v], k); return K[v] + Sum[L[v]] + min_k_sum(R[v], k - 1 - S[L[v]]); } #include "roads.h" #include <vector> long long dp_loose[NN], dp_tight[NN], answer, delsum[NN]; int deg[NN], *eh[NN], eo[NN], heap[NN], interesting, time_visited[NN]; #include<set> #include<utility> std::set<std::pair<int, int> > gg[NN]; void forest_remove(int i) { int k, ii; for (auto [w, j] : gg[i]) { delsum[j] += w; add(heap + j, -w); gg[j].erase(std::make_pair(w, i)); } remove(&interesting, i); } void dfs(int u, int p, int k) { time_visited[u] = k; int i, j, v, w; long long base, base2; for (auto [w, v] : gg[u]) if (v != p) dfs(v, u, k); base = delsum[u]; for (auto [w, v] : gg[u]) if (v != p) { base += w + dp_tight[v]; long long xx = dp_loose[v] - w - dp_tight[v]; if (xx <= 0) add(heap + u, xx); } dp_tight[u] = base + min_k_sum(heap[u], k); dp_loose[u] = base + min_k_sum(heap[u], k - 1); for (auto [w, v] : gg[u]) { long long xx = dp_loose[v] - w - dp_tight[v]; if (xx <= 0) remove(heap + u, xx); } } std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) { srand(85); int i, k, ii, jj, j; std::vector<long long> ans(N); for (i = 0; i < N - 1; ++i) { ++deg[U[i]], ++deg[V[i]]; gg[U[i]].emplace(W[i], V[i]); gg[V[i]].emplace(W[i], U[i]); } std::set<int> interesting; for (i = 0; i < N; ++i) { time_visited[i] = -1; push(eh, eo, deg[i], i); interesting.insert(i); } for (k = 0; k < N; ++k) { for (ii = 0; ii < eo[k]; ++ii) { forest_remove(eh[k][ii]); interesting.erase(eh[k][ii]); } free(eh[k]); for (auto ii : interesting) if (time_visited[ii] != k) { dfs(ii, -1, k); ans[k] += dp_tight[ii]; } ans[k] = answer; } return ans; } /* * sum deg(v) = 2m = 2n-2 * * let f(x) = count of vertices with degree >= x * * Lemma: * sum f(x) for all x in [0..n-1] = sum deg(v) = 2m * * Proof: * - Contribution of node u to sum f(x) is deg(u) * * for each edge (u, v), degree u and v increases by one * subsequently, each edge contribute two to sum f(x) * * * * N = 100000: * sort vertices by deg * * maintain induced subgraph of vertices with deg >= k for each k * do dp on the forest, need to maintain heap of edges connecting node going outside the forest too */

컴파일 시 표준 에러 (stderr) 메시지

roads.cpp: In function 'void forest_remove(int)':
roads.cpp:104:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  104 |     for (auto [w, j] : gg[i]) {
      |               ^
roads.cpp:103:9: warning: unused variable 'k' [-Wunused-variable]
  103 |     int k, ii;
      |         ^
roads.cpp:103:12: warning: unused variable 'ii' [-Wunused-variable]
  103 |     int k, ii;
      |            ^~
roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:117:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  117 |     for (auto [w, v] : gg[u])
      |               ^
roads.cpp:123:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  123 |     for (auto [w, v] : gg[u]) if (v != p) {
      |               ^
roads.cpp:133:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  133 |     for (auto [w, v] : gg[u]) {
      |               ^
roads.cpp:114:9: warning: unused variable 'i' [-Wunused-variable]
  114 |     int i, j, v, w;
      |         ^
roads.cpp:114:12: warning: unused variable 'j' [-Wunused-variable]
  114 |     int i, j, v, w;
      |            ^
roads.cpp:114:15: warning: unused variable 'v' [-Wunused-variable]
  114 |     int i, j, v, w;
      |               ^
roads.cpp:114:18: warning: unused variable 'w' [-Wunused-variable]
  114 |     int i, j, v, w;
      |                  ^
roads.cpp:115:21: warning: unused variable 'base2' [-Wunused-variable]
  115 |     long long base, base2;
      |                     ^~~~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:142:19: warning: unused variable 'jj' [-Wunused-variable]
  142 |     int i, k, ii, jj, j;
      |                   ^~
roads.cpp:142:23: warning: unused variable 'j' [-Wunused-variable]
  142 |     int i, k, ii, jj, j;
      |                       ^
#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...