Submission #765802

#TimeUsernameProblemLanguageResultExecution timeMemory
765802chanhchuong123Džumbus (COCI19_dzumbus)C++14
110 / 110
55 ms17100 KiB
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i) const int INF = 1e9 + 7; const int MAX = 1010; int n, m, q; int D[MAX]; int sz[MAX]; int res[MAX]; bool vis[MAX]; vector<int> adj[MAX]; int dp[MAX][MAX][2][2]; void dfs(int u, int p) { FOR(j, 0, n) FOR(k, 0, 1) FOR(l, 0, 1) dp[u][j][k][l] = INF; sz[u] = 1; dp[u][0][0][0] = 0; dp[u][0][1][0] = D[u]; for (int v: adj[u]) if (!vis[v]) { vis[v] = true; dfs(v, u); FORD(j, 0, sz[u]) FOR(k, 0, 1) FOR(l, 0, 1) { FORD(n, 0, sz[v]) FOR(m, 0, 1) FOR(p, 0, 1) { int J = j + n + (k & m) * (!l + !p), K = k, L = l | (k & m); minimize(dp[u][J][K][L], dp[u][j][k][l] + dp[v][n][m][p]); } } sz[u] += sz[v]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> D[i]; adj[0].push_back(i); } for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } res[n + 1] = INF; D[0] = INF; vis[0] = 1; dfs(0, 0); FORD(j, 0, n) { res[j] = INF; FOR(k, 0, 1) FOR(l, 0, 1) minimize(res[j], dp[0][j][k][l]); minimize(res[j], res[j + 1]); } cin >> q; while (q--) { int S; cin >> S; cout << (upper_bound(res, res + 1 + n, S) - res - 1) << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...