답안 #205412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
205412 2020-02-28T21:57:36 Z luciocf Džumbus (COCI19_dzumbus) C++14
30 / 110
1000 ms 16504 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 1e3+10;
const ll inf = 1e18+10;

int n;
int a[maxn];

int sz[maxn];

int pai[maxn], peso[maxn];

ll dp[maxn][maxn][2];

vector<int> grafo[maxn];

void init(void)
{
	for (int i = 1; i <= n; i++)
		pai[i] = i, peso[i] = 1;
}

int Find(int x)
{
	if (pai[x] == x) return x;
	return pai[x] = Find(pai[x]);
}

void Join(int x, int y)
{
	x = Find(x), y = Find(y);
	if (x == y) return;

	if (peso[x] < peso[y]) swap(x, y);

	pai[y] = x, peso[x] += peso[y];
}

void dfs(int u, int p)
{
	dp[u][0][1] = inf;
	for (int i = 1; i <= n; i++)
		dp[u][i][0] = dp[u][i][1] = inf;

	sz[u] = 1;

	for (auto v: grafo[u])
	{
		if (v == p) continue;

		dfs(v, u);
		sz[u] += sz[v];

		for (int i = sz[u]; i >= 1; i--)
		{
			for (int j = min(i, sz[v]); j >= 1; j--)
			{
				ll caso1 = (i-j >= 1 ? dp[u][i-j-1][0] + 1ll*a[u] + dp[v][j][1] : inf);
				ll caso2 = (i-j >= 1 ? dp[u][i-j-1][0] + 1ll*a[u] + dp[v][j-1][0] + 1ll*a[v] : inf);
				ll caso3 = dp[u][i-j][1] + dp[v][j][0];
				ll caso4 = dp[u][i-j][1] + dp[v][j][1];
				ll caso5 = dp[u][i-j][1] + dp[v][j-1][0] + 1ll*a[v];

				dp[u][i][1] = min({dp[u][i][1], caso1, caso2, caso3, caso4, caso5});
			}
		}

		for (int i = n; i >= 1; i--)
			for (int j = i; j >= 1; j--)
				dp[u][i][0] = min(dp[u][i][0], dp[u][i-j][0] + min(dp[v][j][0], dp[v][j][1]));

	}
}

int main(void)
{
	int m;
	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);

	init();

	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back(v);
		grafo[v].push_back(u);

		Join(u, v);
	}

	a[0] = 1e9+10;
	for (int i = 1; i <= n; i++)
		if (Find(i) == i)
			grafo[0].push_back(i);

	dfs(0, 0);

	int q;
	scanf("%d", &q);

	while (q--)
	{
		int S;
		scanf("%d", &S);

		int ans = 0;
		for (int i = 1; i <= n; i++)
			if (dp[0][i][0] <= S || dp[0][i][1] <= S)
				ans = i;

		printf("%d\n", ans);
	}
}

Compilation message

dzumbus.cpp: In function 'int main()':
dzumbus.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
dzumbus.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
dzumbus.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
dzumbus.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &q);
  ~~~~~^~~~~~~~~~
dzumbus.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &S);
   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 16504 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 16504 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 1784 KB Output is correct
2 Correct 74 ms 1528 KB Output is correct
3 Correct 83 ms 1272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 16504 KB Time limit exceeded
2 Halted 0 ms 0 KB -