답안 #493651

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
493651 2021-12-12T14:01:30 Z inksamurai Džumbus (COCI19_dzumbus) C++17
20 / 110
104 ms 57568 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3dDwnkq ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;

const int _n=1001;

int main(){
_3dDwnkq;
	int n,m;
	cin>>n>>m;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	vec(vi) adj(n);
	rep(i,m){
		int u,v;
		cin>>u>>v;
		u--,v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	vi chs(n,0);
	vi usd(n,0);
	auto dfs=[&](auto self,int v)->void{
		chs[v]=1;
		usd[v]=1;
		for(auto u : adj[v]){
			if(!usd[u]){
				self(self,u);
				chs[v]+=chs[u];
			}
		}
	};
	rep(v,n){
		if(!usd[v]){
			dfs(dfs,v);
		}
	}
	usd=vi(n,0);
	vec(vec(vi)) dp(n+1,vec(vi)(n+1,vi(2,1e9)));
	auto magicaldfs=[&](auto self,int v)->void{
		usd[v]=1;
		dp[v][0][0]=0;
		int now=1;
		for(auto u : adj[v]){
			if(!usd[u]){
				self(self,u);
				int pnow=now;
				drep(j,pnow+1){
					drep(k,chs[u]+1){
						int nej=j+k;
						dp[v][nej][0]=min({
							dp[v][nej][0],
							dp[v][j][0]+min(dp[u][k][1],dp[u][k][0])
						});
						if(nej+1<=n) dp[v][nej+1][1]=min({
							dp[v][nej+1][1],
							dp[v][j][1]+dp[u][k][0]+a[u],
							dp[v][j][0]+dp[u][k][1]+a[v]
						});
						if(nej+2<=n) dp[v][nej+2][1]=min({
							dp[v][nej+2][1],
							dp[v][j][0]+dp[u][k][0]+a[v]+a[u]
						});
						dp[v][nej][1]=min({
							dp[v][nej][1],
							dp[v][j][1]+min(dp[u][k][1],dp[u][k][0])
						});
					}
				}
				now+=chs[u];
			}
		}
	};
	vi fdp(n+1,1e9);
	fdp[0]=0;
	int now=0;
	rep(v,n){
		if(!usd[v]){
			magicaldfs(magicaldfs,v);
			drep(j,now+1){
				drep(nej,chs[v]+1){
					fdp[nej+j]=min(fdp[nej+j],fdp[j]+min(dp[v][nej][0],dp[v][nej][1]));
				}
			}
		}
	}
	int q;
	cin>>q;
	rep(_,q){
		int x;
		cin>>x;
		int l=0,r=n+1,c=-1;
		while(l<=r){
			int m=(l+r)>>1;
			if(fdp[m]<=x){
				c=m;
				l=m+1;
			}else{
				r=m-1;
			}
		}
		cout<<c<<"\n";
	}
//	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 55364 KB Output is correct
2 Correct 68 ms 55432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 55364 KB Output is correct
2 Correct 68 ms 55432 KB Output is correct
3 Incorrect 104 ms 57568 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 34 ms 2928 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 55364 KB Output is correct
2 Correct 68 ms 55432 KB Output is correct
3 Incorrect 104 ms 57568 KB Output isn't correct
4 Halted 0 ms 0 KB -