답안 #223363

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223363 2020-04-15T08:02:06 Z Minnakhmetov Džumbus (COCI19_dzumbus) C++14
110 / 110
185 ms 19192 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const ll INF = 1e18;
const int N = 1005;
vector<int> g[N];
bool used[N];
ll dp[N][N][2], cost[N], dp2[N][2];
int sz[N];

void dfs(int node) {
	used[node] = true;
	for (int to : g[node]) {
		if (!used[to]) {
			dfs(to);
		}
	}
}

void dive(int node, int anc) {
	sz[node] = 1;
	for (int to : g[node]) {
		if (to != anc) {
			dive(to, node);
			sz[node] += sz[to];
		}
	}

	int s = 0;
	dp[node][0][0] = 0;
	for (int to : g[node]) {
		if (to != anc) {
			for (int j = s; j >= 0; j--) {
				for (int k = 0; k <= sz[to]; k++) {
					dp[node][j + k][0] = min(dp[node][j + k][0], 
						min(dp[to][k][0], dp[to][k][1]) + dp[node][j][0]);
				}
			}
			s += sz[to];
		}
	}

	for (int i = 0; i < N; i++) {
		dp2[i][0] = dp2[i][1] = INF;
	}

	dp2[1][0] = cost[node];

	s = 1;

	for (int to : g[node]) {
		if (to != anc) {
			for (int j = s; j >= 0; j--) {
				for (int k = 0; k <= sz[to]; k++) {
					for (int h = 0; h < 2; h++) {
						dp2[j + k][h] = min(dp2[j + k][h], dp[to][k][0] + dp2[j][h]);
						dp2[j + k][1] = min(dp2[j + k][1], dp[to][k][1] + dp2[j][h]);
						dp2[j + k + 1][1] = min(dp2[j + k + 1][1], dp[to][k][0] + cost[to] + dp2[j][h]);
					}
				}
			}
			s += sz[to];
		}
	}

	for (int i = 1; i < N; i++) {
		dp[node][i][1] = dp2[i][1];
	}
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
    	cin >> cost[i];
    }

    for (int i = 0; i < m; i++) {
    	int x, y;
    	cin >> x >> y;
    	x--, y--;
    	g[x].push_back(y);
    	g[y].push_back(x);
    }

    int rt = n++;

    for (int i = 0; i < N; i++) {
    	for (int j = 0; j < N; j++) {
    		dp[i][j][0] = dp[i][j][1] = INF;
    	}
    }

    cost[rt] = INF;

    for (int i = 0; i < n - 1; i++) {
    	if (!used[i]) {
    		g[rt].push_back(i);
    		dfs(i);
    	}
    }

    dive(rt, -1);

    int q;
    cin >> q;

    while (q--) {
    	ll x;
    	cin >> x;

    	int ans = 0;

    	for (int i = n - 1; i >= 0; i--) {
    		if (dp[rt][i][0] <= x) {
    			ans = i;
    			break;
    		}
    	}

    	cout << ans << "\n";
    }
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 16256 KB Output is correct
2 Correct 22 ms 16384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 16256 KB Output is correct
2 Correct 22 ms 16384 KB Output is correct
3 Correct 93 ms 18624 KB Output is correct
4 Correct 87 ms 19192 KB Output is correct
5 Correct 178 ms 18680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 16948 KB Output is correct
2 Correct 63 ms 16888 KB Output is correct
3 Correct 75 ms 18552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 16256 KB Output is correct
2 Correct 22 ms 16384 KB Output is correct
3 Correct 93 ms 18624 KB Output is correct
4 Correct 87 ms 19192 KB Output is correct
5 Correct 178 ms 18680 KB Output is correct
6 Correct 61 ms 16948 KB Output is correct
7 Correct 63 ms 16888 KB Output is correct
8 Correct 75 ms 18552 KB Output is correct
9 Correct 89 ms 18296 KB Output is correct
10 Correct 100 ms 18936 KB Output is correct
11 Correct 185 ms 18812 KB Output is correct