Submission #902549

# Submission time Handle Problem Language Result Execution time Memory
902549 2024-01-10T16:35:27 Z arashMLG Džumbus (COCI19_dzumbus) C++17
110 / 110
169 ms 38132 KB
#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
1 Correct 15 ms 35420 KB Output is correct
2 Correct 15 ms 35312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 35420 KB Output is correct
2 Correct 15 ms 35312 KB Output is correct
3 Correct 161 ms 37476 KB Output is correct
4 Correct 169 ms 38132 KB Output is correct
5 Correct 45 ms 37732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 37188 KB Output is correct
2 Correct 43 ms 37332 KB Output is correct
3 Correct 41 ms 37452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 35420 KB Output is correct
2 Correct 15 ms 35312 KB Output is correct
3 Correct 161 ms 37476 KB Output is correct
4 Correct 169 ms 38132 KB Output is correct
5 Correct 45 ms 37732 KB Output is correct
6 Correct 49 ms 37188 KB Output is correct
7 Correct 43 ms 37332 KB Output is correct
8 Correct 41 ms 37452 KB Output is correct
9 Correct 140 ms 37216 KB Output is correct
10 Correct 150 ms 37972 KB Output is correct
11 Correct 50 ms 37716 KB Output is correct