이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #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;
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) {
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[i + j][0], h[i][0] + dp[0][v][j]);
if (i + j + 2 <= sum + sz[v]) ckmin(nh[i + j + 2][1], h[i][0] + dp[1][v][j]);
if (i + j + 1 <= sum + sz[v]) ckmin(nh[i + j + 1][1], h[i][0] + dp[2][v][j]);
ckmin(nh[i + j][1], h[i][1] + dp[0][v][j]);
if (i + j + 1 <= sum + sz[v]) ckmin(nh[i + j + 1][1], h[i][1] + dp[1][v][j]);
ckmin(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 (sz[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) {
ckmin(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);
}
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |