제출 #1127215

#제출 시각아이디문제언어결과실행 시간메모리
1127215steveonalexDžumbus (COCI19_dzumbus)C++20
110 / 110
67 ms27460 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){ while(a > 0 && b > 0){ if (a > b) a %= b; else b %= a; } return a + b; } ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return __lg(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n"){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct DSU{ int n; vector<int> parent, sz; DSU(int _n){ n = _n; parent.resize(n+1); sz.resize(n+1, 1); for(int i = 1; i<=n; ++i) parent[i] = i; } int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));} bool same_set(int u, int v){return find_set(u) == find_set(v);} bool join_set(int u, int v){ u = find_set(u), v = find_set(v); if (u != v){ if (sz[u] < sz[v]) swap(u, v); parent[v] = u; sz[u] += sz[v]; return true; } return false; } int get_size(int u){return sz[find_set(u)];} }; const int N = 1100; const ll INF = 1e18 + 69; int n, m; vector<int> graph[N]; ll a[N]; int sz[N]; ll dp[N][N][3]; void dfs(int u, int p){ sz[u] = 1; dp[u][0][0] = 0; dp[u][1][1] = a[u]; for(int v: graph[u]) if (v != p){ dfs(v, u); ll f[sz[u] + sz[v] + 1][3]; for(int i = 0; i <= sz[u] + sz[v]; ++i) for(int j = 0; j < 3; ++j) f[i][j] = INF; for(int i = 0; i <= sz[u]; ++i) for(int x = 0; x < 3; ++x) for(int j = 0; j <= sz[v]; ++j) for(int y = 0; y < 3; ++y){ if (x == 0 && y == 1) continue; int nxt = x; if (x > 0 && y > 0) nxt = 2; minimize(f[i + j][nxt], dp[u][i][x] + dp[v][j][y]); } for(int i = 0; i <= sz[u] + sz[v]; ++i) for(int j = 0; j < 3; ++j) dp[u][i][j] = f[i][j]; sz[u] += sz[v]; } // cout << "Node: " << u << "\n"; // for(int j = 0; j <= sz[u]; ++j){ // for(int x = 0; x < 3; ++x) cout << dp[u][j][x] << " "; // cout << "\n"; // } } void solve(){ cin >> n >> m; for(int i = 1; i<=n; ++i) cin >> a[i]; DSU mst(n); for(int i = 0; i<m; ++i){ int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); mst.join_set(u, v); } for(int i = 1; i<=n; ++i) if (mst.find_set(i) == i) { graph[i].push_back(0); graph[0].push_back(i); mst.join_set(i, 0); } a[0] = INF; for(int i = 0; i <= n; ++i) for(int j = 0; j <= n; ++j) for(int x = 0; x < 3; ++x) dp[i][j][x] = INF; dfs(0, 0); vector<pair<ll, ll>> best; for(int j = 0; j <= n; ++j) { while(best.size() && best.back().first >= dp[0][j][0]) best.pop_back(); best.push_back({dp[0][j][0], j}); } int q; cin >> q; while(q--){ int x; cin >> x; pair<ll, ll> cur = make_pair(x + 1, -1); int idx = upper_bound(ALL(best), cur) - best.begin() - 1; cout << best[idx].second << "\n"; } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\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...