Submission #115932

# Submission time Handle Problem Language Result Execution time Memory
115932 2019-06-10T01:32:33 Z luciocf Tug of War (BOI15_tug) C++14
23 / 100
2142 ms 5368 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

const int maxn = 6e4+10;

typedef pair<int, int> pii;

int n, k;

int sa, sb;

int pai[maxn], edge[maxn];

int firstCycle[maxn], lastCycle[maxn], pesoCycle[2][maxn], inCycle[maxn];
int comp[maxn], back[maxn];

int freq[20*maxn];
bool dp[20*maxn];

bool mark[maxn];

vector<int> V;

vector<pii> grafo[maxn];

int msb(int x)
{
	return 31-__builtin_clz(x);
}

void dfs(int u, int p, int cc)
{
	comp[u] = cc;

	for (auto pp: grafo[u])
	{
		int v = pp.ff, w = pp.ss;

		if (comp[v])
		{
			if (v != p)
			{
				firstCycle[cc] = u, lastCycle[cc] = v;
				back[cc] = w;
			}
			continue;
		}

		pai[v] = u, edge[v] = w;

		dfs(v, u, cc);
	}
}

void markCycle(int cc)
{
	int u = firstCycle[cc], v = lastCycle[cc];

	int s0 = 0, s1 = back[cc];
	bool turn = 0;

	while (true)
	{
		inCycle[v] = true;

		if (!turn) s0 += edge[v];
		else s1 += edge[v];

		if (u == v) break;

		v = pai[v];
		turn = !turn;
	}

	if (s0 > s1)
	{
		++freq[s1], ++freq[s0-s1];
		pesoCycle[0][cc] = s1, pesoCycle[1][cc] = s0-s1;
	}
	else
	{
		++freq[s0], ++freq[s1-s0];
		pesoCycle[0][cc] = s0, pesoCycle[1][cc] = s1-s0;
	}
}

void dfs2(int u)
{
	mark[u] = true;

	for (auto pp: grafo[u])
	{
		int v = pp.ff;
		if (!mark[v])
			dfs2(v);
	}

	if (!inCycle[u])
	{
		if (u <= n) sa += edge[u];
		else sb += edge[u];
	}
}

int main(void)
{
	memset(firstCycle, -1, sizeof firstCycle);

	scanf("%d %d", &n, &k);

	int S = 0;

	for (int i = 1; i <= 2*n; i++)
	{
		int a, b, w;
		scanf("%d %d %d", &a, &b, &w);

		b += n;
		S += w;

		grafo[a].push_back({b, w});
		grafo[b].push_back({a, w});
	}

	int cc = 0;
	for (int i = 1; i <= 2*n; i++)
	{
		if (comp[i]) continue;

		dfs(i, 0, ++cc);

		if (firstCycle[cc] == -1)
		{
			printf("NO\n");
			return 0;
		}
	}

	for (int i = 1; i <= cc; i++)
	{
		markCycle(i);
		dfs2(firstCycle[i]);
	}

	for (int i = 0; i <= cc; i++)
	{
		int a = pesoCycle[0][i];

		int x = msb(freq[a]);

		for (int j = 0; j < x; j++)
			V.push_back((1<<j)*a);

		V.push_back(a*(freq[i]-(1<<x)+1));


		a = pesoCycle[1][i];

		x = msb(freq[a]);

		for (int j = 0; j < x; j++)
			V.push_back((1<<j)*a);

		V.push_back(a*(freq[i]-(1<<x)+1));
	}

	// solve a
	dp[0] = 1;
	for (auto w: V)
		for (int i = (S-2*sa)/2; i >= w; i--)
			dp[i] |= dp[i-w];

	int mx = 0;
	for (int i = 0; i <= (S-2*sa)/2; i++)
		if (dp[i])
			mx = max(mx, i);

	if (S-sb-2*sa-2*mx <= k)
	{
		printf("YES\n");
		return 0;
	}

	memset(dp, 0, sizeof dp);

	// solve b
	dp[0] = 1;
	for (auto w: V)
		for (int i = (S-2*sb)/2; i >= w; i--)
			dp[i] |= dp[i-w];

	mx = 0;
	for (int i = 0; i <= (S-2*sb)/2; i++)
		if (dp[i])
			mx = max(mx, i);

	if (S-sa-2*sb-2*mx <= k)
	{
		printf("YES\n");
		return 0;
	}

	printf("NO\n");
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
tug.cpp:120:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a, &b, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3200 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 4 ms 3200 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Correct 4 ms 3200 KB Output is correct
6 Correct 4 ms 3200 KB Output is correct
7 Correct 3 ms 2048 KB Output is correct
8 Correct 3 ms 2048 KB Output is correct
9 Correct 3 ms 2020 KB Output is correct
10 Correct 4 ms 3176 KB Output is correct
11 Correct 3 ms 2048 KB Output is correct
12 Correct 3 ms 2048 KB Output is correct
13 Correct 3 ms 2048 KB Output is correct
14 Correct 3 ms 2048 KB Output is correct
15 Correct 3 ms 2048 KB Output is correct
16 Correct 4 ms 3200 KB Output is correct
17 Correct 3 ms 2048 KB Output is correct
18 Correct 3 ms 2048 KB Output is correct
19 Correct 3 ms 2048 KB Output is correct
20 Correct 3 ms 2048 KB Output is correct
21 Correct 3 ms 1920 KB Output is correct
22 Correct 3 ms 2048 KB Output is correct
23 Incorrect 4 ms 3200 KB Output isn't correct
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3200 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 4 ms 3200 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Correct 4 ms 3200 KB Output is correct
6 Correct 4 ms 3200 KB Output is correct
7 Correct 3 ms 2048 KB Output is correct
8 Correct 3 ms 2048 KB Output is correct
9 Correct 3 ms 2020 KB Output is correct
10 Correct 4 ms 3176 KB Output is correct
11 Correct 3 ms 2048 KB Output is correct
12 Correct 3 ms 2048 KB Output is correct
13 Correct 3 ms 2048 KB Output is correct
14 Correct 3 ms 2048 KB Output is correct
15 Correct 3 ms 2048 KB Output is correct
16 Correct 4 ms 3200 KB Output is correct
17 Correct 3 ms 2048 KB Output is correct
18 Correct 3 ms 2048 KB Output is correct
19 Correct 3 ms 2048 KB Output is correct
20 Correct 3 ms 2048 KB Output is correct
21 Correct 3 ms 1920 KB Output is correct
22 Correct 3 ms 2048 KB Output is correct
23 Incorrect 4 ms 3200 KB Output isn't correct
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1840 ms 5368 KB Output is correct
2 Correct 12 ms 3072 KB Output is correct
3 Correct 1862 ms 5368 KB Output is correct
4 Correct 12 ms 2816 KB Output is correct
5 Correct 1831 ms 5368 KB Output is correct
6 Correct 13 ms 2944 KB Output is correct
7 Correct 1827 ms 5368 KB Output is correct
8 Correct 12 ms 3072 KB Output is correct
9 Correct 1876 ms 5368 KB Output is correct
10 Correct 12 ms 2944 KB Output is correct
11 Correct 1832 ms 5368 KB Output is correct
12 Correct 12 ms 3072 KB Output is correct
13 Correct 1834 ms 5368 KB Output is correct
14 Correct 1893 ms 5368 KB Output is correct
15 Correct 14 ms 2944 KB Output is correct
16 Correct 1856 ms 5360 KB Output is correct
17 Correct 16 ms 3072 KB Output is correct
18 Correct 1939 ms 5360 KB Output is correct
19 Correct 13 ms 3072 KB Output is correct
20 Correct 2142 ms 5368 KB Output is correct
21 Correct 13 ms 3200 KB Output is correct
22 Correct 565 ms 3968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3200 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 4 ms 3200 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Correct 4 ms 3200 KB Output is correct
6 Correct 4 ms 3200 KB Output is correct
7 Correct 3 ms 2048 KB Output is correct
8 Correct 3 ms 2048 KB Output is correct
9 Correct 3 ms 2020 KB Output is correct
10 Correct 4 ms 3176 KB Output is correct
11 Correct 3 ms 2048 KB Output is correct
12 Correct 3 ms 2048 KB Output is correct
13 Correct 3 ms 2048 KB Output is correct
14 Correct 3 ms 2048 KB Output is correct
15 Correct 3 ms 2048 KB Output is correct
16 Correct 4 ms 3200 KB Output is correct
17 Correct 3 ms 2048 KB Output is correct
18 Correct 3 ms 2048 KB Output is correct
19 Correct 3 ms 2048 KB Output is correct
20 Correct 3 ms 2048 KB Output is correct
21 Correct 3 ms 1920 KB Output is correct
22 Correct 3 ms 2048 KB Output is correct
23 Incorrect 4 ms 3200 KB Output isn't correct
24 Halted 0 ms 0 KB -