제출 #199493

#제출 시각아이디문제언어결과실행 시간메모리
199493SamAndDžumbus (COCI19_dzumbus)C++17
20 / 110
78 ms35808 KiB
#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; }

컴파일 시 표준 에러 (stderr) 메시지

dzumbus.cpp: In function 'void dfs(int, int)':
dzumbus.cpp:21:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:143:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vv.size(); ++i)
                     ~~^~~~~~~~~~~
dzumbus.cpp:164:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (t.j + 1 != vv[t.i].size() - 1)
                 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
dzumbus.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &d[i]);
         ~~~~~^~~~~~~~~~~~~
dzumbus.cpp:103:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
dzumbus.cpp:135:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &qq);
     ~~~~~^~~~~~~~~~~
dzumbus.cpp:138:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &b[i].d);
         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...