이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 1e3 + 23;
const ll inf = 1e18;
#define F first
#define S second
#define pb push_back
#define kill(x) cout<<x<<endl, exit(0);
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lc (v << 1)
#define rc ((v<<1) |1)
#define int ll
int n,m;
int a[N];
int dp[N][N][3];
int pd[N][3];
int val[N][N];
vector<int> G[N];
int s[N];
bool mark[N];
void dfs(int v,int p = -1) {
mark[v] = true;
s[v] = 1;
dp[v][0][1] = 0;
dp[v][0][2] = a[v];
for(int u : G[v]) if(u != p) {
dfs(u,v);
for(int i = 0 ; i < N ; i++) {
pd[i][1] = inf;
pd[i][0] = inf;
pd[i][2] = inf;
}
pd[0][1] = 0;
pd[0][2] = a[v];
// 0-> kharidim ba yeki match shode
// 1-> nakharidim
// 2-> kharidim ba kasi match nashode
for(int i = 0 ;i <= s[v]; i ++) for(int j = 0; j <= s[u]; j ++) {
//00
pd[i+j][0] = min(pd[i+j][0],dp[v][i][0] + dp[u][j][0]);
//01
pd[i+j][0] = min(pd[i+j][0],dp[v][i][0] + dp[u][j][1]);
//02
pd[i+j+1][0] = min(pd[i+j+1][0],dp[v][i][0] + dp[u][j][2]);
//10
pd[i+j][1] = min(pd[i+j][1],dp[v][i][1]+ dp[u][j][0]);
//11
pd[i+j][1] = min(pd[i+j][1],dp[v][i][1]+dp[u][j][1]);
//12
pd[i+j][1] = min(pd[i+j][1],dp[v][i][1]+dp[u][j][2]);
//20
pd[i+j+1][0] = min(pd[i+j+1][0],dp[v][i][2] + dp[u][j][0]);
//21
pd[i+j][2] = min(pd[i+j][2],dp[v][i][2] + dp[u][j][1]);
//22
pd[i+j+2][0] =min(pd[i+j+2][0], dp[v][i][2] + dp[u][j][2]);
}
for(int i = N-2; i >= 0 ; i--) {
pd[i][0] = min(pd[i][0],pd[i+1][0]);
pd[i][1] = min(pd[i][1],pd[i+1][1]);
pd[i][2] = min(pd[i][2],pd[i+1][2]);
for(int k = 0; k < 3; k ++) dp[v][i][k] = pd[i][k];
}
s[v] += s[u];
}
}
int DP[N][N];
vector<int> vs;
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
for(int i = 0 ; i < N ; i ++) for(int j = 0; j < N ; j++) dp[i][j][0] = dp[i][j][1] = dp[i][j][2] = inf;
cin>>n>>m;
for(int i= 1; i<= n ; i++) cin>>a[i];
for(int i =1; i <= m ; i++) {
int v,u; cin>>v>>u;
G[v].pb(u);
G[u].pb(v);
}
vs.pb(-69);
for(int i = 1; i <= n ; i++) {
if(!mark[i]) {
dfs(i);
vs.pb(i);
}
}
int k = sz(vs)-1;
for(int i = 0; i < N ; i ++) for(int j = 0 ; j <N ; j++) DP[i][j]= inf;
DP[0][0] = 0;
for(int i = 1; i <= k; i ++) {
for(int j = 0; j <= n ; j ++) {
for(int tedad = 0; tedad <= min(j,s[vs[i]]) ; tedad ++) {
int x = min({dp[vs[i]][tedad][0], dp[vs[i]][tedad][1],dp[vs[i]][tedad][2]});
DP[i][j] = min(DP[i][j],DP[i-1][j-tedad] + x);
}
}
}
int q; cin>>q;
while(q--) {
int S; cin>>S;
int ans =0;
for(ans= 0; ans <= n; ans ++) {
if(DP[k][ans] >S) break;
}
cout<<ans-1 << '\n';
}
return 0;
}
// Jumpsuit, Jumpsuit cover me!
// Jumpsuit, Jumpsuit cover me!
# | 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... |