This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e3 + 7;
const ll oo = 1e18 + 69;
int n, m, a[maxn], u, v, q, s, sz[maxn];
vector<int> g[maxn];
ll dp[maxn][maxn][3], f[maxn][maxn][3], res[maxn];
bool vis[maxn];
void dfs2(int u) {
vis[u] = 1;
for(auto v:g[u]) {
if(vis[v]) continue;
dfs2(v);
}
}
void dfs(int u, int par) {
for(auto v:g[u]) {
if(v == par) continue;
dfs(v, u);
}
for(int i = 0; i <= Size(g[u]); i++)
for(int j = 0; j <= n; j++)
for(int t = 0; t < 3; t++) f[i][j][t] = oo;
if(par != -1) {
sz[u] = 1;
f[0][0][1] = a[u];
}
f[0][0][0] = 0;
for(int i = 1; i <= Size(g[u]); i++) {
int v = g[u][i - 1];
if(v == par) {
for(int j = 0; j <= n; j++)
for(int t = 0; t < 3; t++) f[i][j][t] = f[i - 1][j][t];
continue;
}
for(int cur = 0; cur <= sz[u]; cur++) {
for(int pre = 0; pre <= sz[v]; pre++) {
for(int t = 0; t < 3; t++) {
mini(f[i][cur + pre][0], f[i - 1][cur][0] + dp[v][pre][t]);
}
mini(f[i][cur + pre][1], f[i - 1][cur][1] + dp[v][pre][0]);
mini(f[i][cur + pre][2], f[i - 1][cur][2] + dp[v][pre][0]);
mini(f[i][cur + pre][2], f[i - 1][cur][2] + dp[v][pre][2]);
if(cur + pre + 1 <= n) {
mini(f[i][cur + pre + 1][2], f[i - 1][cur][2] + dp[v][pre][1]);
mini(f[i][cur + pre + 1][2], f[i - 1][cur][1] + dp[v][pre][2]);
}
if(cur + pre + 2 <= n) {
mini(f[i][cur + pre + 2][2], f[i - 1][cur][1] + dp[v][pre][1]);
}
}
}
sz[u] += sz[v];
}
for(int j = 0; j <= n; j++)
for(int t = 0; t < 3; t++) dp[u][j][t] = f[Size(g[u])][j][t];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>m;
for(int i = 1; i <= n; i++) cin>>a[i];
while(m--) {
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++)
if(!vis[i]) {
g[0].pb(i);
g[i].pb(0);
dfs2(i);
}
dfs(0, -1);
for(int i = 0; i <= n; i++) {
res[i] = oo;
for(int t = 0; t < 3; t++) mini(res[i], dp[0][i][t]);
}
for(int i = n - 1; i >= 0; i--) mini(res[i], res[i + 1]);
cin>>q;
while(q--) {
cin>>s;
int l = 2, r = n, mid, ans = 0;
while(l <= r) {
mid = l + r >> 1;
if(res[mid] <= s) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
cout<<ans<<"\n";
}
return 0;
}
Compilation message (stderr)
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:119:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
119 | mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |