Submission #365355

#TimeUsernameProblemLanguageResultExecution timeMemory
365355IsaacMorisDžumbus (COCI19_dzumbus)C++14
110 / 110
160 ms13164 KiB
// Sometimes, the questions are complicated - and the answers are simple. // #include<iostream> #include <bits/stdc++.h> #define ll long long #define ld long double #define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; const int N = 1e3 + 5, inf = 1e9 + 2; int n, m, q, D[N]; vector<int> adj[N]; bool visited[N]; int dp[N][N][3]; int sz[N]; void dfs(int node, int p) { if(visited[node]) return; visited[node] = true; sz[node] = 1; for(int i = 0; i <= n; i++) for(int j = 0; j < 3; j++) dp[node][i][j] = inf; dp[node][0][0] = 0; dp[node][1][1] = D[node]; dp[node][0][1] = inf; for(auto i : adj[node]) if(i != p) { int child = i; dfs(child, node); for(int j = sz[node]; j >= 0; j--) { for(int k = 1; k <= sz[child]; k++) { dp[node][j + k][0] = min(dp[node][j + k][0], dp[node][j][0] + dp[child][k][0]); dp[node][j + k][0] = min(dp[node][j + k][0], dp[node][j][0] + dp[child][k][2]); //---------------------------------------------------------------------------------------- dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][2] + dp[child][k][0]); dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][2] + dp[child][k][1]); dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][2] + dp[child][k][2]); dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][1] + dp[child][k][1]); dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][1] + dp[child][k][2]); dp[node][j + k][1] = min(dp[node][j + k][1], dp[node][j][1] + dp[child][k][0]); dp[node][j + k][1] = min(dp[node][j + k][1], dp[node][j][1] + dp[child][k][2]); } } sz[node] += sz[child]; } } int main() { IO cin >> n >> m; for(int i = 1; i <= n; i++) cin >> D[i]; for(int i = 1; i <= m; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } D[0] = inf; for(int i = 1; i <= n; i++) if(!visited[i]) { dfs(i, 0); adj[0].push_back(i); } dfs(0, 0); cin >> q; while(q--) { int s; cin >> s; for(int i = n; i >= 0; i--) if(dp[0][i][0] <= s) { cout << i << "\n"; break; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...