답안 #117329

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
117329 2019-06-15T13:43:19 Z luciocf Tug of War (BOI15_tug) C++14
71 / 100
3000 ms 5248 KB
#include <bits/stdc++.h>

#pragma GCC optimize ("O2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,avx,mmx,popcnt,tune=native")
 
#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];
 
bitset<1200002> dp[2];
 
bool mark[maxn];
 
vector<int> V;
 
vector<pair<pii, int>> grafo[maxn];

void dfs(int u, int ant, int cc)
{
	comp[u] = cc;
 
	for (auto pp: grafo[u])
	{
		int v = pp.ff.ff, w = pp.ff.ss, e = pp.ss;
 
		if (comp[v] == cc)
		{
			if (ant == e) continue;

			firstCycle[cc] = u, lastCycle[cc] = v;
			back[cc] = w;

			continue;
		}
 
		pai[v] = u, edge[v] = w;
 
		dfs(v, e, cc);
	}
}
 
void markCycle(int cc)
{
	int v = lastCycle[cc];
 
	int s0 = 0, s1 = back[cc];
	bool turn = 0;
 
	while (true)
	{
		inCycle[v] = true;
 
		if (v == firstCycle[cc]) break;

		if (!turn) s0 += edge[v];
		else s1 += edge[v];
 
		v = pai[v];
		turn = !turn;
	}
 
	pesoCycle[0][cc] = s0, pesoCycle[1][cc] = s1;
}

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

		if (mark[v]) continue;

		dfs2(v);

		if (!inCycle[v])
		{
			if (v <= n) sa += w;
			else sb += w; 
		}
	}
}
 
int main(void)
{
	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}, i});
		grafo[b].push_back({{a, w}, i});
	}
 
	int cc = 0;
	for (int i = 1; i <= 2*n; i++)
	{
		if (comp[i]) continue;
 
		dfs(i, 0, ++cc);
 
		if (firstCycle[cc] == 0)
		{
			printf("NO\n");
			return 0;
		}
	}

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

		tot += max(pesoCycle[0][i], pesoCycle[1][i]);
	}
 
	dp[0][0] = 1;
	for (int i = 1; i <= cc; i++)
	{
		int a = (i-1)&1, b = i&1;

		dp[b].reset();

		for (int j = tot; j >= min(pesoCycle[0][i], pesoCycle[1][i]); j--)
		{
			if (pesoCycle[0][i] <= j)
				dp[b][j] = dp[b][j]|dp[a][j-pesoCycle[0][i]];

			if (pesoCycle[1][i] <= j)
				dp[b][j] = dp[b][j]|dp[a][j-pesoCycle[1][i]];
		}
	}

	for (int i = 1; i <= tot; i++)
	{
		if (dp[cc&1][i])
		{
			int a = i+sa, b = S-i-sa;

			if (abs(a-b) <= k)
			{
				printf("YES\n");
				return 0;
			}

			a = S-i-sb, b = i+sb;
			
			if (abs(a-b) <= k)
			{
				printf("YES\n");
				return 0;
			}
		}
	}
 
	printf("NO\n");
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:103: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:110: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);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2048 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 3 ms 2048 KB Output is correct
4 Correct 4 ms 2048 KB Output is correct
5 Correct 3 ms 2048 KB Output is correct
6 Correct 4 ms 2048 KB Output is correct
7 Correct 4 ms 2048 KB Output is correct
8 Correct 4 ms 2048 KB Output is correct
9 Correct 3 ms 2048 KB Output is correct
10 Correct 4 ms 2048 KB Output is correct
11 Correct 4 ms 2048 KB Output is correct
12 Correct 4 ms 2048 KB Output is correct
13 Correct 4 ms 2048 KB Output is correct
14 Correct 4 ms 2048 KB Output is correct
15 Correct 3 ms 2048 KB Output is correct
16 Correct 3 ms 2048 KB Output is correct
17 Correct 4 ms 2048 KB Output is correct
18 Correct 6 ms 2048 KB Output is correct
19 Correct 4 ms 2048 KB Output is correct
20 Correct 3 ms 2048 KB Output is correct
21 Correct 3 ms 1792 KB Output is correct
22 Correct 4 ms 2048 KB Output is correct
23 Correct 3 ms 2048 KB Output is correct
24 Correct 4 ms 2048 KB Output is correct
25 Correct 3 ms 2048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2048 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 3 ms 2048 KB Output is correct
4 Correct 4 ms 2048 KB Output is correct
5 Correct 3 ms 2048 KB Output is correct
6 Correct 4 ms 2048 KB Output is correct
7 Correct 4 ms 2048 KB Output is correct
8 Correct 4 ms 2048 KB Output is correct
9 Correct 3 ms 2048 KB Output is correct
10 Correct 4 ms 2048 KB Output is correct
11 Correct 4 ms 2048 KB Output is correct
12 Correct 4 ms 2048 KB Output is correct
13 Correct 4 ms 2048 KB Output is correct
14 Correct 4 ms 2048 KB Output is correct
15 Correct 3 ms 2048 KB Output is correct
16 Correct 3 ms 2048 KB Output is correct
17 Correct 4 ms 2048 KB Output is correct
18 Correct 6 ms 2048 KB Output is correct
19 Correct 4 ms 2048 KB Output is correct
20 Correct 3 ms 2048 KB Output is correct
21 Correct 3 ms 1792 KB Output is correct
22 Correct 4 ms 2048 KB Output is correct
23 Correct 3 ms 2048 KB Output is correct
24 Correct 4 ms 2048 KB Output is correct
25 Correct 3 ms 2048 KB Output is correct
26 Correct 297 ms 2304 KB Output is correct
27 Correct 60 ms 2304 KB Output is correct
28 Correct 296 ms 2424 KB Output is correct
29 Correct 59 ms 2304 KB Output is correct
30 Correct 297 ms 2424 KB Output is correct
31 Correct 60 ms 2304 KB Output is correct
32 Correct 301 ms 2424 KB Output is correct
33 Correct 60 ms 2304 KB Output is correct
34 Correct 32 ms 2304 KB Output is correct
35 Correct 62 ms 2296 KB Output is correct
36 Correct 301 ms 2304 KB Output is correct
37 Correct 60 ms 2296 KB Output is correct
38 Correct 302 ms 2396 KB Output is correct
39 Correct 60 ms 2304 KB Output is correct
40 Correct 312 ms 2304 KB Output is correct
41 Correct 63 ms 2304 KB Output is correct
42 Correct 298 ms 2352 KB Output is correct
43 Correct 59 ms 2304 KB Output is correct
44 Correct 303 ms 2304 KB Output is correct
45 Correct 60 ms 2304 KB Output is correct
46 Correct 304 ms 2304 KB Output is correct
47 Correct 5 ms 2048 KB Output is correct
48 Correct 84 ms 2304 KB Output is correct
49 Correct 83 ms 2304 KB Output is correct
50 Correct 413 ms 2348 KB Output is correct
51 Correct 421 ms 2424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 602 ms 3192 KB Output is correct
2 Correct 13 ms 2816 KB Output is correct
3 Correct 608 ms 3292 KB Output is correct
4 Correct 13 ms 2560 KB Output is correct
5 Correct 610 ms 3216 KB Output is correct
6 Correct 13 ms 2688 KB Output is correct
7 Correct 638 ms 3200 KB Output is correct
8 Correct 13 ms 2816 KB Output is correct
9 Correct 609 ms 3320 KB Output is correct
10 Correct 13 ms 2688 KB Output is correct
11 Correct 601 ms 3320 KB Output is correct
12 Correct 13 ms 2816 KB Output is correct
13 Correct 601 ms 3184 KB Output is correct
14 Correct 598 ms 3324 KB Output is correct
15 Correct 17 ms 2816 KB Output is correct
16 Correct 597 ms 3200 KB Output is correct
17 Correct 12 ms 2816 KB Output is correct
18 Correct 583 ms 3200 KB Output is correct
19 Correct 12 ms 2816 KB Output is correct
20 Correct 593 ms 3192 KB Output is correct
21 Correct 13 ms 2944 KB Output is correct
22 Correct 177 ms 3368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2048 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 3 ms 2048 KB Output is correct
4 Correct 4 ms 2048 KB Output is correct
5 Correct 3 ms 2048 KB Output is correct
6 Correct 4 ms 2048 KB Output is correct
7 Correct 4 ms 2048 KB Output is correct
8 Correct 4 ms 2048 KB Output is correct
9 Correct 3 ms 2048 KB Output is correct
10 Correct 4 ms 2048 KB Output is correct
11 Correct 4 ms 2048 KB Output is correct
12 Correct 4 ms 2048 KB Output is correct
13 Correct 4 ms 2048 KB Output is correct
14 Correct 4 ms 2048 KB Output is correct
15 Correct 3 ms 2048 KB Output is correct
16 Correct 3 ms 2048 KB Output is correct
17 Correct 4 ms 2048 KB Output is correct
18 Correct 6 ms 2048 KB Output is correct
19 Correct 4 ms 2048 KB Output is correct
20 Correct 3 ms 2048 KB Output is correct
21 Correct 3 ms 1792 KB Output is correct
22 Correct 4 ms 2048 KB Output is correct
23 Correct 3 ms 2048 KB Output is correct
24 Correct 4 ms 2048 KB Output is correct
25 Correct 3 ms 2048 KB Output is correct
26 Correct 297 ms 2304 KB Output is correct
27 Correct 60 ms 2304 KB Output is correct
28 Correct 296 ms 2424 KB Output is correct
29 Correct 59 ms 2304 KB Output is correct
30 Correct 297 ms 2424 KB Output is correct
31 Correct 60 ms 2304 KB Output is correct
32 Correct 301 ms 2424 KB Output is correct
33 Correct 60 ms 2304 KB Output is correct
34 Correct 32 ms 2304 KB Output is correct
35 Correct 62 ms 2296 KB Output is correct
36 Correct 301 ms 2304 KB Output is correct
37 Correct 60 ms 2296 KB Output is correct
38 Correct 302 ms 2396 KB Output is correct
39 Correct 60 ms 2304 KB Output is correct
40 Correct 312 ms 2304 KB Output is correct
41 Correct 63 ms 2304 KB Output is correct
42 Correct 298 ms 2352 KB Output is correct
43 Correct 59 ms 2304 KB Output is correct
44 Correct 303 ms 2304 KB Output is correct
45 Correct 60 ms 2304 KB Output is correct
46 Correct 304 ms 2304 KB Output is correct
47 Correct 5 ms 2048 KB Output is correct
48 Correct 84 ms 2304 KB Output is correct
49 Correct 83 ms 2304 KB Output is correct
50 Correct 413 ms 2348 KB Output is correct
51 Correct 421 ms 2424 KB Output is correct
52 Correct 602 ms 3192 KB Output is correct
53 Correct 13 ms 2816 KB Output is correct
54 Correct 608 ms 3292 KB Output is correct
55 Correct 13 ms 2560 KB Output is correct
56 Correct 610 ms 3216 KB Output is correct
57 Correct 13 ms 2688 KB Output is correct
58 Correct 638 ms 3200 KB Output is correct
59 Correct 13 ms 2816 KB Output is correct
60 Correct 609 ms 3320 KB Output is correct
61 Correct 13 ms 2688 KB Output is correct
62 Correct 601 ms 3320 KB Output is correct
63 Correct 13 ms 2816 KB Output is correct
64 Correct 601 ms 3184 KB Output is correct
65 Correct 598 ms 3324 KB Output is correct
66 Correct 17 ms 2816 KB Output is correct
67 Correct 597 ms 3200 KB Output is correct
68 Correct 12 ms 2816 KB Output is correct
69 Correct 583 ms 3200 KB Output is correct
70 Correct 12 ms 2816 KB Output is correct
71 Correct 593 ms 3192 KB Output is correct
72 Correct 13 ms 2944 KB Output is correct
73 Correct 177 ms 3368 KB Output is correct
74 Execution timed out 3040 ms 5248 KB Time limit exceeded
75 Halted 0 ms 0 KB -