Submission #117353

# Submission time Handle Problem Language Result Execution time Memory
117353 2019-06-15T14:53:08 Z luciocf Tug of War (BOI15_tug) C++14
100 / 100
1670 ms 6904 KB
#include <bits/stdc++.h>
 
#pragma GCC optimize("Ofast")
 
#define ff first
#define ss second
 
using namespace std;
 
const int maxn = 6e4+10;
const int add = 10*maxn;
 
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;
 
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 soma = 0;
	for (int i = 1; i <= cc; i++)
	{
		markCycle(i);
		dfs2(firstCycle[i]);

		soma += (max(pesoCycle[0][i], pesoCycle[1][i])-min(pesoCycle[0][i], pesoCycle[1][i]));
	}
 
	dp[sa-sb-soma+add] = 1;
	for (int i = 1; i <= cc; i++)
	{ 
		int ss = 2*(max(pesoCycle[0][i], pesoCycle[1][i])-min(pesoCycle[0][i], pesoCycle[1][i]));
		dp |= (dp << ss);
	}
 
	for (int i = add-k; i <= add+k; i++)
	{
		if (dp[i])
		{
			printf("YES\n");
			return 0;
		}
	}
 
	printf("NO\n");
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:101: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:108: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 2176 KB Output is correct
2 Correct 4 ms 2176 KB Output is correct
3 Correct 4 ms 2176 KB Output is correct
4 Correct 4 ms 2176 KB Output is correct
5 Correct 4 ms 2176 KB Output is correct
6 Correct 4 ms 2176 KB Output is correct
7 Correct 4 ms 2176 KB Output is correct
8 Correct 4 ms 2176 KB Output is correct
9 Correct 4 ms 2176 KB Output is correct
10 Correct 4 ms 2176 KB Output is correct
11 Correct 4 ms 2176 KB Output is correct
12 Correct 3 ms 2176 KB Output is correct
13 Correct 4 ms 2176 KB Output is correct
14 Correct 4 ms 2176 KB Output is correct
15 Correct 4 ms 2176 KB Output is correct
16 Correct 4 ms 2176 KB Output is correct
17 Correct 4 ms 2176 KB Output is correct
18 Correct 4 ms 2176 KB Output is correct
19 Correct 4 ms 2176 KB Output is correct
20 Correct 4 ms 2176 KB Output is correct
21 Correct 3 ms 1792 KB Output is correct
22 Correct 4 ms 2176 KB Output is correct
23 Correct 4 ms 2176 KB Output is correct
24 Correct 4 ms 2176 KB Output is correct
25 Correct 4 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2176 KB Output is correct
2 Correct 4 ms 2176 KB Output is correct
3 Correct 4 ms 2176 KB Output is correct
4 Correct 4 ms 2176 KB Output is correct
5 Correct 4 ms 2176 KB Output is correct
6 Correct 4 ms 2176 KB Output is correct
7 Correct 4 ms 2176 KB Output is correct
8 Correct 4 ms 2176 KB Output is correct
9 Correct 4 ms 2176 KB Output is correct
10 Correct 4 ms 2176 KB Output is correct
11 Correct 4 ms 2176 KB Output is correct
12 Correct 3 ms 2176 KB Output is correct
13 Correct 4 ms 2176 KB Output is correct
14 Correct 4 ms 2176 KB Output is correct
15 Correct 4 ms 2176 KB Output is correct
16 Correct 4 ms 2176 KB Output is correct
17 Correct 4 ms 2176 KB Output is correct
18 Correct 4 ms 2176 KB Output is correct
19 Correct 4 ms 2176 KB Output is correct
20 Correct 4 ms 2176 KB Output is correct
21 Correct 3 ms 1792 KB Output is correct
22 Correct 4 ms 2176 KB Output is correct
23 Correct 4 ms 2176 KB Output is correct
24 Correct 4 ms 2176 KB Output is correct
25 Correct 4 ms 2176 KB Output is correct
26 Correct 108 ms 2496 KB Output is correct
27 Correct 20 ms 2432 KB Output is correct
28 Correct 110 ms 2552 KB Output is correct
29 Correct 19 ms 2432 KB Output is correct
30 Correct 112 ms 2404 KB Output is correct
31 Correct 20 ms 2432 KB Output is correct
32 Correct 112 ms 2552 KB Output is correct
33 Correct 20 ms 2432 KB Output is correct
34 Correct 14 ms 2432 KB Output is correct
35 Correct 22 ms 2476 KB Output is correct
36 Correct 108 ms 2432 KB Output is correct
37 Correct 20 ms 2548 KB Output is correct
38 Correct 106 ms 2432 KB Output is correct
39 Correct 18 ms 2468 KB Output is correct
40 Correct 110 ms 2524 KB Output is correct
41 Correct 25 ms 2432 KB Output is correct
42 Correct 108 ms 2432 KB Output is correct
43 Correct 20 ms 2432 KB Output is correct
44 Correct 106 ms 2460 KB Output is correct
45 Correct 19 ms 2432 KB Output is correct
46 Correct 109 ms 2524 KB Output is correct
47 Correct 5 ms 2092 KB Output is correct
48 Correct 58 ms 2432 KB Output is correct
49 Correct 64 ms 2520 KB Output is correct
50 Correct 108 ms 2432 KB Output is correct
51 Correct 113 ms 2432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 407 ms 3448 KB Output is correct
2 Correct 15 ms 2944 KB Output is correct
3 Correct 365 ms 3320 KB Output is correct
4 Correct 13 ms 2560 KB Output is correct
5 Correct 386 ms 3320 KB Output is correct
6 Correct 14 ms 2688 KB Output is correct
7 Correct 380 ms 3320 KB Output is correct
8 Correct 13 ms 2816 KB Output is correct
9 Correct 366 ms 3320 KB Output is correct
10 Correct 12 ms 2688 KB Output is correct
11 Correct 366 ms 3328 KB Output is correct
12 Correct 12 ms 2816 KB Output is correct
13 Correct 362 ms 3356 KB Output is correct
14 Correct 355 ms 3412 KB Output is correct
15 Correct 12 ms 2688 KB Output is correct
16 Correct 356 ms 3320 KB Output is correct
17 Correct 12 ms 2816 KB Output is correct
18 Correct 374 ms 3328 KB Output is correct
19 Correct 13 ms 2760 KB Output is correct
20 Correct 370 ms 3320 KB Output is correct
21 Correct 13 ms 2944 KB Output is correct
22 Correct 194 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2176 KB Output is correct
2 Correct 4 ms 2176 KB Output is correct
3 Correct 4 ms 2176 KB Output is correct
4 Correct 4 ms 2176 KB Output is correct
5 Correct 4 ms 2176 KB Output is correct
6 Correct 4 ms 2176 KB Output is correct
7 Correct 4 ms 2176 KB Output is correct
8 Correct 4 ms 2176 KB Output is correct
9 Correct 4 ms 2176 KB Output is correct
10 Correct 4 ms 2176 KB Output is correct
11 Correct 4 ms 2176 KB Output is correct
12 Correct 3 ms 2176 KB Output is correct
13 Correct 4 ms 2176 KB Output is correct
14 Correct 4 ms 2176 KB Output is correct
15 Correct 4 ms 2176 KB Output is correct
16 Correct 4 ms 2176 KB Output is correct
17 Correct 4 ms 2176 KB Output is correct
18 Correct 4 ms 2176 KB Output is correct
19 Correct 4 ms 2176 KB Output is correct
20 Correct 4 ms 2176 KB Output is correct
21 Correct 3 ms 1792 KB Output is correct
22 Correct 4 ms 2176 KB Output is correct
23 Correct 4 ms 2176 KB Output is correct
24 Correct 4 ms 2176 KB Output is correct
25 Correct 4 ms 2176 KB Output is correct
26 Correct 108 ms 2496 KB Output is correct
27 Correct 20 ms 2432 KB Output is correct
28 Correct 110 ms 2552 KB Output is correct
29 Correct 19 ms 2432 KB Output is correct
30 Correct 112 ms 2404 KB Output is correct
31 Correct 20 ms 2432 KB Output is correct
32 Correct 112 ms 2552 KB Output is correct
33 Correct 20 ms 2432 KB Output is correct
34 Correct 14 ms 2432 KB Output is correct
35 Correct 22 ms 2476 KB Output is correct
36 Correct 108 ms 2432 KB Output is correct
37 Correct 20 ms 2548 KB Output is correct
38 Correct 106 ms 2432 KB Output is correct
39 Correct 18 ms 2468 KB Output is correct
40 Correct 110 ms 2524 KB Output is correct
41 Correct 25 ms 2432 KB Output is correct
42 Correct 108 ms 2432 KB Output is correct
43 Correct 20 ms 2432 KB Output is correct
44 Correct 106 ms 2460 KB Output is correct
45 Correct 19 ms 2432 KB Output is correct
46 Correct 109 ms 2524 KB Output is correct
47 Correct 5 ms 2092 KB Output is correct
48 Correct 58 ms 2432 KB Output is correct
49 Correct 64 ms 2520 KB Output is correct
50 Correct 108 ms 2432 KB Output is correct
51 Correct 113 ms 2432 KB Output is correct
52 Correct 407 ms 3448 KB Output is correct
53 Correct 15 ms 2944 KB Output is correct
54 Correct 365 ms 3320 KB Output is correct
55 Correct 13 ms 2560 KB Output is correct
56 Correct 386 ms 3320 KB Output is correct
57 Correct 14 ms 2688 KB Output is correct
58 Correct 380 ms 3320 KB Output is correct
59 Correct 13 ms 2816 KB Output is correct
60 Correct 366 ms 3320 KB Output is correct
61 Correct 12 ms 2688 KB Output is correct
62 Correct 366 ms 3328 KB Output is correct
63 Correct 12 ms 2816 KB Output is correct
64 Correct 362 ms 3356 KB Output is correct
65 Correct 355 ms 3412 KB Output is correct
66 Correct 12 ms 2688 KB Output is correct
67 Correct 356 ms 3320 KB Output is correct
68 Correct 12 ms 2816 KB Output is correct
69 Correct 374 ms 3328 KB Output is correct
70 Correct 13 ms 2760 KB Output is correct
71 Correct 370 ms 3320 KB Output is correct
72 Correct 13 ms 2944 KB Output is correct
73 Correct 194 ms 3456 KB Output is correct
74 Correct 1607 ms 5460 KB Output is correct
75 Correct 101 ms 6008 KB Output is correct
76 Correct 1587 ms 6288 KB Output is correct
77 Correct 109 ms 6008 KB Output is correct
78 Correct 1626 ms 6284 KB Output is correct
79 Correct 1647 ms 6392 KB Output is correct
80 Correct 127 ms 5940 KB Output is correct
81 Correct 120 ms 5880 KB Output is correct
82 Correct 1670 ms 6284 KB Output is correct
83 Correct 1597 ms 6264 KB Output is correct
84 Correct 116 ms 5880 KB Output is correct
85 Correct 1618 ms 6276 KB Output is correct
86 Correct 113 ms 5880 KB Output is correct
87 Correct 1575 ms 6312 KB Output is correct
88 Correct 102 ms 5880 KB Output is correct
89 Correct 1659 ms 6392 KB Output is correct
90 Correct 112 ms 5880 KB Output is correct
91 Correct 1627 ms 6292 KB Output is correct
92 Correct 102 ms 5880 KB Output is correct
93 Correct 1572 ms 6296 KB Output is correct
94 Correct 37 ms 4996 KB Output is correct
95 Correct 845 ms 6876 KB Output is correct
96 Correct 837 ms 6904 KB Output is correct
97 Correct 1659 ms 6320 KB Output is correct
98 Correct 1621 ms 6264 KB Output is correct