Submission #438813

# Submission time Handle Problem Language Result Execution time Memory
438813 2021-06-28T16:27:45 Z cuiaoxiang Džumbus (COCI19_dzumbus) C++17
110 / 110
66 ms 6980 KB
#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;
// const int MOD = 998244353;
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)]; }
template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> void dedup(vector<T>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); }

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];
int sz[N], w[N];
// 0: unmark & uncover
// 1: mark & uncover
// 2: mark & cover
vector<int> dp[3][N];

void DFS(int u, int parent) {
  sz[u] = 1;
  vector<int> f = {0, inf<int>};
  vector<int> g = {w[u], inf<int>};
  vector<vector<int>> h{{w[u], inf<int>}, {inf<int>, inf<int>}};
  for (auto& v : a[u]) {
    if (v == parent) continue;
    DFS(v, u);
    vector<int> nf(sz[u] + sz[v] + 1, inf<int>);
    vector<int> ng(sz[u] + sz[v] + 1, inf<int>);
    vector<vector<int>> nh(2, vector<int>(sz[u] + sz[v] + 1, inf<int>));
    for (int i = 0; i <= sz[u]; ++i) {
      for (int j = 0; j <= sz[v]; ++j) {
        ckmin(nf[i + j], f[i] + min(min(dp[0][v][j], dp[1][v][j]), dp[2][v][j]));
        ckmin(ng[i + j], g[i] + dp[0][v][j]);

        ckmin(nh[0][i + j], h[0][i] + dp[0][v][j]);
        if (i + j + 2 <= sz[u] + sz[v]) ckmin(nh[1][i + j + 2], h[0][i] + dp[1][v][j]);
        if (i + j + 1 <= sz[u] + sz[v]) ckmin(nh[1][i + j + 1], h[0][i] + dp[2][v][j]);

        ckmin(nh[1][i + j], h[1][i] + dp[0][v][j]);
        if (i + j + 1 <= sz[u] + sz[v]) ckmin(nh[1][i + j + 1], h[1][i] + dp[1][v][j]);
        ckmin(nh[1][i + j], h[1][i] + dp[2][v][j]);
      }
    }
    swap(f, nf); swap(g, ng); swap(h, nh);
    sz[u] += sz[v];
  }
  dp[0][u] = f;
  dp[1][u] = g;
  dp[2][u] = h[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(1, 0);
  for (int k = 0; k < n; ++k) {
    if (sz[k]) continue;
    DFS(k, -1);
    vector<int> nv(SZ(v) + sz[k], inf<int>);
    for (int i = 0; i < SZ(v); ++i) {
      for (int j = 0; j <= sz[k]; ++j) {
        ckmin(nv[i + j], v[i] + min(min(dp[0][k][j], dp[1][k][j]), dp[2][k][j]));
      }
    }
    swap(v, nv);
  }

  for (int i = SZ(v) - 2; i >= 0; --i) ckmin(v[i], v[i + 1]);

  // 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;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 4428 KB Output is correct
2 Correct 19 ms 5620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 4428 KB Output is correct
2 Correct 19 ms 5620 KB Output is correct
3 Correct 66 ms 6980 KB Output is correct
4 Correct 63 ms 6008 KB Output is correct
5 Correct 58 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 1456 KB Output is correct
2 Correct 43 ms 1332 KB Output is correct
3 Correct 45 ms 1036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 4428 KB Output is correct
2 Correct 19 ms 5620 KB Output is correct
3 Correct 66 ms 6980 KB Output is correct
4 Correct 63 ms 6008 KB Output is correct
5 Correct 58 ms 4944 KB Output is correct
6 Correct 42 ms 1456 KB Output is correct
7 Correct 43 ms 1332 KB Output is correct
8 Correct 45 ms 1036 KB Output is correct
9 Correct 46 ms 1876 KB Output is correct
10 Correct 53 ms 1632 KB Output is correct
11 Correct 62 ms 1352 KB Output is correct