# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
199493 | 2020-02-01T15:43:18 Z | SamAnd | Džumbus (COCI19_dzumbus) | C++17 | 78 ms | 35808 KB |
#include <bits/stdc++.h> using namespace std; const int N = 1003, Q = 200005; const long long INF = 1000000009000000009; int n, m; int d[N]; vector<int> a[N]; bool c[N]; long long dp[N][N][2][2]; int q[N]; long long ndp[N][2][2]; void dfs(int x, int p) { c[x] = true; q[x] = 1; dp[x][0][0][0] = 0; dp[x][0][1][0] = d[x]; for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i]; if (h == p) continue; dfs(h, x); for (int j = 0; j <= q[x] + q[h]; ++j) { for (int z1 = 0; z1 < 2; ++z1) { for (int z2 = 0; z2 < 2; ++z2) ndp[j][z1][z2] = INF; } } for (int j = 0; j <= q[x]; ++j) { for (int jj = 0; jj <= q[h]; ++jj) { ndp[j + jj][0][0] = min(ndp[j + jj][0][0], dp[x][j][0][0] + dp[h][jj][0][0]); ndp[j + jj][0][0] = min(ndp[j + jj][0][0], dp[x][j][0][0] + dp[h][jj][1][0]); ndp[j + jj][0][0] = min(ndp[j + jj][0][0], dp[x][j][0][0] + dp[h][jj][1][1]); ndp[j + jj][1][0] = min(ndp[j + jj][1][0], dp[x][j][1][0] + dp[h][jj][0][0]); ndp[j + jj + 2][1][1] = min(ndp[j + jj + 2][1][1], dp[x][j][1][0] + dp[h][jj][1][0]); ndp[j + jj + 1][1][1] = min(ndp[j + jj + 1][1][1], dp[x][j][1][0] + dp[h][jj][1][1]); ndp[j + jj][1][1] = min(ndp[j + jj][1][1], dp[x][j][1][1] + dp[h][jj][0][0]); ndp[j + jj + 1][1][1] = min(ndp[j + jj + 1][1][1], dp[x][j][1][1] + dp[h][jj][1][0]); ndp[j + jj][1][1] = min(ndp[j + jj][1][1], dp[x][j][1][1] + dp[h][jj][1][1]); } } q[x] += q[h]; for (int j = 0; j <= q[x]; ++j) { for (int z1 = 0; z1 < 2; ++z1) { for (int z2 = 0; z2 < 2; ++z2) { dp[x][j][z1][z2] = ndp[j][z1][z2]; } } } } } vector<vector<long long> > vv; struct ban { int i; int d; }; bool operator<(const ban& a, const ban& b) { return a.d < b.d; } int qq; ban b[Q]; struct ban1 { int i, j; long long d; ban1(int i, int j, long long d) { this->i = i; this->j = j; this->d = d; } }; bool operator<(const ban1& a, const ban1& b) { return a.d > b.d; } int ans[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &d[i]); for (int i = 0; i < m; ++i) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } for (int i = 1; i <= n; ++i) { for (int j = 0; j <= n; ++j) { for (int z1 = 0; z1 < 2; ++z1) { for (int z2 = 0; z2 < 2; ++z2) { dp[i][j][z1][z2] = INF; } } } } for (int i = 1; i <= n; ++i) { if (!c[i]) { dfs(i, i); vector<long long> v; for (int j = 0; j <= q[i]; ++j) { v.push_back(min(min(dp[i][j][0][0], dp[i][j][0][1]), min(dp[i][j][1][0], dp[i][j][1][1]))); } for (int j = q[i] - 1; j >= 0; --j) v[j] = min(v[j], v[j + 1]); vv.push_back(v); } } scanf("%d", &qq); for (int i = 0; i < qq; ++i) { scanf("%d", &b[i].d); b[i].i = i; } sort(b, b + qq); priority_queue<ban1> q; for (int i = 0; i < vv.size(); ++i) { q.push(ban1(i, 0, vv[i][1] - vv[i][0])); } long long dd = 0; int yans = 0; for (int i = 0; i < qq; ++i) { while (1) { if (q.empty()) break; ban1 t = q.top(); q.pop(); if (dd + t.d > b[i].d) { q.push(t); break; } dd += t.d; ++yans; if (t.j + 1 != vv[t.i].size() - 1) q.push(ban1(t.i, t.j + 1, vv[t.i][t.j + 2] - vv[t.i][t.j + 1])); } ans[b[i].i] = yans; } for (int i = 0; i < qq; ++i) printf("%d\n", ans[i]); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 28 ms | 31992 KB | Output is correct |
2 | Correct | 28 ms | 31996 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 28 ms | 31992 KB | Output is correct |
2 | Correct | 28 ms | 31996 KB | Output is correct |
3 | Incorrect | 78 ms | 35808 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Incorrect | 55 ms | 4856 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 28 ms | 31992 KB | Output is correct |
2 | Correct | 28 ms | 31996 KB | Output is correct |
3 | Incorrect | 78 ms | 35808 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |