Submission #438741

#TimeUsernameProblemLanguageResultExecution timeMemory
438741cuiaoxiangDžumbus (COCI19_dzumbus)C++17
0 / 110
42 ms4172 KiB
// #define LOCAL #define _USE_MATH_DEFINES #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <vector> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <complex> #include <cmath> #include <numeric> #include <bitset> #include <functional> #include <random> #include <ctime> using namespace std; template <typename A, typename B> ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } template <class T> auto vect(const T& v, int n) { return vector<T>(n, v); } template <class T, class... D> auto vect(const T& v, int n, D... m) { return vector<decltype(vect(v, m...))>(n, vect(v, m...)); } typedef long long int64; typedef pair<int, int> ii; #define SZ(x) (int)((x).size()) template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2; const int MOD = 1e9 + 7; mt19937 mrand(random_device{}()); int rnd(int x) { return mrand() % x; } template <class T> void out(const vector<T>& a) { for (int i = 0; i < SZ(a); ++i) cout << a[i] << " \n"[i + 1 == SZ(a)]; } struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_; const int N = 1e3 + 10; vector<int> a[N]; bool visit[N]; int sz[N], w[N]; // 0: unmark & uncover // 1: mark & uncover // 2: mark & cover vector<int> dp[3][N]; void update(int& x, int y) { x = min(x, y); } void DFS(int u, int parent) { visit[u] = true; sz[u] = 1; for (auto& v : a[u]) { if (v == parent) continue; DFS(v, u); sz[u] += sz[v]; } for (int k = 0; k < 3; ++k) dp[k][u].resize(sz[u] + 1, inf<int>); auto& f = dp[0][u]; auto& g = dp[1][u]; vector<array<int, 2>> h(sz[u] + 1, {inf<int>, inf<int>}); f[0] = 0; g[0] = w[u]; h[0] = {w[u], inf<int>}; int sum = 1; for (auto& v : a[u]) { if (v == parent) continue; vector<int> nf(sz[u] + 1, inf<int>); vector<int> ng(sz[u] + 1, inf<int>); vector<array<int, 2>> nh(sz[u] + 1, {inf<int>, inf<int>}); for (int i = 0; i <= sum; ++i) { for (int j = 0; j <= sz[v]; ++j) { update(nf[i + j], f[i] + min(min(dp[0][v][j], dp[1][v][j]), dp[2][v][j])); update(ng[i + j], g[i] + dp[0][v][j]); update(nh[i + j][0], h[i][0] + dp[0][v][j]); if (i + j + 2 <= sum + sz[v]) update(nh[i + j + 2][1], h[i][0] + dp[1][v][j]); if (i + j + 1 <= sum + sz[v]) update(nh[i + j + 1][1], h[i][0] + dp[2][v][j]); update(nh[i + j][1], h[i][1] + dp[0][v][j]); if (i + j + 1 <= sum + sz[v]) update(nh[i + j + 1][1], h[i][1] + dp[1][v][j]); update(nh[i + j][1], h[i][1] + dp[2][v][j]); } } swap(f, nf); swap(g, ng); swap(h, nh); sum += sz[v]; } for (int i = 0; i <= sz[u]; ++i) dp[2][u][i] = h[i][1]; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> w[i]; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; --x; --y; a[x].push_back(y); a[y].push_back(x); } vector<int> v(n + 1, inf<int>); v[0] = 0; int sum = 0; for (int k = 0; k < n; ++k) { if (visit[k]) continue; trace("DFS", k); DFS(k, -1); vector<int> nv(n + 1, inf<int>); for (int i = 0; i <= sum; ++i) { for (int j = 0; j <= sz[k]; ++j) { update(nv[i + j], v[i] + min(min(dp[0][k][j], dp[1][k][j]), dp[2][k][j])); } } sum += sz[k]; swap(v, nv); } trace(v); int q; cin >> q; while (q--) { int x; cin >> x; int ret = upper_bound(v.begin(), v.end(), x) - v.begin() - 1; cout << ret << '\n'; } return 0; }

Compilation message (stderr)

dzumbus.cpp: In function 'int main()':
dzumbus.cpp:62:20: warning: statement has no effect [-Wunused-value]
   62 | #define trace(...) 42
      |                    ^~
dzumbus.cpp:165:5: note: in expansion of macro 'trace'
  165 |     trace("DFS", k);
      |     ^~~~~
dzumbus.cpp:62:20: warning: statement has no effect [-Wunused-value]
   62 | #define trace(...) 42
      |                    ^~
dzumbus.cpp:177:3: note: in expansion of macro 'trace'
  177 |   trace(v);
      |   ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...