# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1106136 | TrendBattles | Džumbus (COCI19_dzumbus) | C++14 | 52 ms | 35400 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;
#define INFILE "DZUMBUS.INP"
#define OUTFILE "DZUMBUS.OUT"
const int MAX_N = 1000;
lli dp[MAX_N + 10][MAX_N + 10][2][2];
vector <int> graph[MAX_N + 10];
int visited[MAX_N + 10];
void Trace(int u) {
if (visited[u]) return;
visited[u] = true;
for (int v : graph[u]) Trace(v);
}
int cost[MAX_N + 10];
void minimise(lli& x, lli y) {
if (x > y) x = y;
}
int subtree_size[MAX_N + 10];
void DFS(int u) {
for (int v : graph[u]) {
graph[v].erase(find(graph[v].begin(), graph[v].end(), u));
DFS(v);
}
subtree_size[u] = 1;
dp[u][0][0][0] = 0;
dp[u][0][1][0] = cost[u];
for (int v : graph[u]) {
for (int i = subtree_size[u]; i >= 0; --i) {
lli tmp[2][2];
for (int c_u = 0; c_u < 2; ++c_u) {
for (int adj_u = 0; adj_u < 2; ++adj_u) {
tmp[c_u][adj_u] = dp[u][i][c_u][adj_u];
}
}
for (int j = 0; j <= subtree_size[v]; ++j) {
for (int c_u = 0; c_u < 2; ++c_u) {
for (int adj_u = 0; adj_u < 2; ++adj_u) {
for (int c_v = 0; c_v < 2; ++c_v) {
for (int adj_v = 0; adj_v < 2; ++adj_v) {
int new_size = i + j;
if (c_u and c_v) new_size += (adj_u == 0) + (adj_v == 0);
minimise(dp[u][new_size][c_u][adj_u | c_v], tmp[c_u][adj_u] + dp[v][j][c_v][adj_v]);
}
}
}
}
}
}
subtree_size[u] += subtree_size[v];
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
if (fopen(INFILE, "r")) {
freopen(INFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
}
memset(dp, 0x3f, sizeof dp);
int N, M; cin >> N >> M;
for (int i = 1; i <= N; ++i) cin >> cost[i];
for (int i = 1; i <= M; ++i) {
int u, v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
visited[0] = true;
for (int i = 1; i <= N; ++i) {
if (visited[i]) continue;
graph[0].push_back(i);
graph[i].push_back(0);
Trace(i);
}
DFS(0);
vector <lli> value;
for (int i = 0; i <= N; ++i) {
value.push_back(min(dp[0][i][0][0], dp[0][i][0][1]));
}
for (int i = N - 1; i >= 0; --i) {
value[i] = min(value[i], value[i + 1]);
}
int Q; cin >> Q;
for (int qu = 1; qu <= Q; ++qu) {
lli x; cin >> x;
cout << upper_bound(value.begin(), value.end(), x) - value.begin() - 1 << '\n';
}
return 0;
}
Compilation message (stderr)
# | 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... |