Submission #24493

# Submission time Handle Problem Language Result Execution time Memory
24493 2017-06-09T11:14:24 Z Bruteforceman Tug of War (BOI15_tug) C++11
100 / 100
113 ms 6736 KB
#include "bits/stdc++.h"
using namespace std;
bool del[2 * 30005];
int deg[2][30005];
vector <int> g[2][30005];
 
struct data
{
	int l, r, s;
	data () {}
	data (int l, int r, int s) : l(l), r(r), s(s) {}
} a[2 * 30005];
 
struct node
{
	int i, side;
	node () {}
	node (int i, int side) : i(i), side(side) {}
};
 
bool play[2 * 30010];
bool vis[2][30010];
int sum;
 
void cycle(node n) {
	// cout << n.i << " " << n.side << endl;
	if(n.side == -1) {
		play[n.i] = true;
		if(vis[0][a[n.i].l] == false) {
			cycle(node(a[n.i].l, 0));
			sum += a[n.i].s;
		} else {
			cycle(node(a[n.i].r, 1));
			sum -= a[n.i].s;
		}
	} else {
		vis[n.side][n.i] = true;
		for(auto i : g[n.side][n.i]) {
			if(del[i] == false && play[i] == false) {
				cycle(node(i, -1));
				break;
			}
		}
	}
}
 
const int tot = 20 * 30005;
bitset <tot> dp;
 
void knapsack(vector <int> v) {
	map <int, int> f;
	vector <int> u;
	for(auto i : v) {
		f[i] += 1;
	}
	for(auto i : f) {
		int p = i.first;
		int q = i.second;
		for(int j = 0; (1 << j) <= q; j++) {
			u.push_back((1 << j) * p);
			q -= 1 << j;
		}
		if(q > 0) {
			u.push_back(q * p);
		} 
	}
	dp[0] = 1;
	for(auto i : u) {
		dp |= dp << i;
	}
}
 
int main(int argc, char const *argv[])
{
	int n, k;
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n+n; i++) {
		int l, r, s;
		scanf("%d %d %d", &l, &r, &s);
		a[i] = data(l, r, s);
		g[0][l].push_back(i);
		g[1][r].push_back(i);
		++deg[0][l];
		++deg[1][r];
	}
	queue <node> Q;
	for(int i = 1; i <= n; i++) {
		if(deg[0][i] == 1) Q.push(node(i, 0));
		if(deg[1][i] == 1) Q.push(node(i, 1));
		
		if(deg[0][i] == 0 || deg[1][i] == 0) {
			printf("NO\n");
			exit(0);
		}
	}
	int add[2];
	add[0] = add[1] = 0;
	while(!Q.empty()) {
		node p = Q.front();
		Q.pop();
		for(auto i : g[p.side][p.i]) {
			if(del[i] == false) {
				add[p.side] += a[i].s;
				del[i] = true;
				if(p.side == 0) {
					--deg[1][a[i].r];
					if(deg[1][a[i].r] == 1) {
						Q.push(node(a[i].r, 1));
					}
					if(deg[1][a[i].r] == 0) {
						printf("NO\n");
						exit(0);
					}
				} else {
					--deg[0][a[i].l];
					if(deg[0][a[i].l] == 1) {
						Q.push(node(a[i].l, 0));
					}
					if(deg[0][a[i].l] == 0) {
						printf("NO\n");
						exit(0);
					}
				}
			}
		}
	}
	vector <int> v;
	int neg = 0;
	for(int i = 1; i <= n+n; i++) {
		if(play[i] == false && del[i] == false) {
			int k1, k2;
			sum = 0;
			cycle(node(i, -1));
			k1 = sum;
			v.push_back(abs(k1));
			neg -= abs(k1);
		}
	} 
	knapsack(v);
	add[0] -= add[1];
	for(int i = 0; i < tot; i++) {
		if(dp[i] == 1) {
			int hehe = i + i + neg + add[0];
			if(abs(hehe) <= k) {
				printf("YES\n");
				exit(0);
			}
		}
	}
 	printf("NO\n");
	return 0; 
}

Compilation message

tug.cpp: In function 'int main(int, const char**)':
tug.cpp:131:12: warning: unused variable 'k2' [-Wunused-variable]
    int k1, k2;
            ^
tug.cpp:76:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
                        ^
tug.cpp:79:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &l, &r, &s);
                                ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4644 KB Output is correct
2 Correct 0 ms 4644 KB Output is correct
3 Correct 0 ms 4644 KB Output is correct
4 Correct 0 ms 4640 KB Output is correct
5 Correct 0 ms 4644 KB Output is correct
6 Correct 3 ms 4644 KB Output is correct
7 Correct 0 ms 4640 KB Output is correct
8 Correct 0 ms 4640 KB Output is correct
9 Correct 0 ms 4644 KB Output is correct
10 Correct 3 ms 4644 KB Output is correct
11 Correct 0 ms 4640 KB Output is correct
12 Correct 0 ms 4640 KB Output is correct
13 Correct 0 ms 4644 KB Output is correct
14 Correct 0 ms 4640 KB Output is correct
15 Correct 0 ms 4640 KB Output is correct
16 Correct 3 ms 4640 KB Output is correct
17 Correct 0 ms 4644 KB Output is correct
18 Correct 0 ms 4644 KB Output is correct
19 Correct 3 ms 4640 KB Output is correct
20 Correct 0 ms 4640 KB Output is correct
21 Correct 0 ms 4620 KB Output is correct
22 Correct 0 ms 4644 KB Output is correct
23 Correct 0 ms 4640 KB Output is correct
24 Correct 0 ms 4644 KB Output is correct
25 Correct 3 ms 4640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4644 KB Output is correct
2 Correct 0 ms 4644 KB Output is correct
3 Correct 0 ms 4644 KB Output is correct
4 Correct 0 ms 4640 KB Output is correct
5 Correct 0 ms 4644 KB Output is correct
6 Correct 3 ms 4644 KB Output is correct
7 Correct 0 ms 4640 KB Output is correct
8 Correct 0 ms 4640 KB Output is correct
9 Correct 0 ms 4644 KB Output is correct
10 Correct 3 ms 4644 KB Output is correct
11 Correct 0 ms 4640 KB Output is correct
12 Correct 0 ms 4640 KB Output is correct
13 Correct 0 ms 4644 KB Output is correct
14 Correct 0 ms 4640 KB Output is correct
15 Correct 0 ms 4640 KB Output is correct
16 Correct 3 ms 4640 KB Output is correct
17 Correct 0 ms 4644 KB Output is correct
18 Correct 0 ms 4644 KB Output is correct
19 Correct 3 ms 4640 KB Output is correct
20 Correct 0 ms 4640 KB Output is correct
21 Correct 0 ms 4620 KB Output is correct
22 Correct 0 ms 4644 KB Output is correct
23 Correct 0 ms 4640 KB Output is correct
24 Correct 0 ms 4644 KB Output is correct
25 Correct 3 ms 4640 KB Output is correct
26 Correct 6 ms 4776 KB Output is correct
27 Correct 13 ms 4772 KB Output is correct
28 Correct 9 ms 4772 KB Output is correct
29 Correct 9 ms 4776 KB Output is correct
30 Correct 9 ms 4772 KB Output is correct
31 Correct 16 ms 4776 KB Output is correct
32 Correct 6 ms 4776 KB Output is correct
33 Correct 13 ms 4776 KB Output is correct
34 Correct 3 ms 4776 KB Output is correct
35 Correct 9 ms 4776 KB Output is correct
36 Correct 9 ms 4772 KB Output is correct
37 Correct 19 ms 4772 KB Output is correct
38 Correct 6 ms 4776 KB Output is correct
39 Correct 16 ms 4776 KB Output is correct
40 Correct 6 ms 4776 KB Output is correct
41 Correct 13 ms 4772 KB Output is correct
42 Correct 9 ms 4776 KB Output is correct
43 Correct 9 ms 4776 KB Output is correct
44 Correct 6 ms 4772 KB Output is correct
45 Correct 16 ms 4772 KB Output is correct
46 Correct 6 ms 4776 KB Output is correct
47 Correct 0 ms 4752 KB Output is correct
48 Correct 9 ms 4776 KB Output is correct
49 Correct 9 ms 4772 KB Output is correct
50 Correct 6 ms 4772 KB Output is correct
51 Correct 6 ms 4772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 5468 KB Output is correct
2 Correct 23 ms 5280 KB Output is correct
3 Correct 9 ms 5472 KB Output is correct
4 Correct 16 ms 5280 KB Output is correct
5 Correct 13 ms 5468 KB Output is correct
6 Correct 13 ms 5280 KB Output is correct
7 Correct 13 ms 5472 KB Output is correct
8 Correct 9 ms 5280 KB Output is correct
9 Correct 19 ms 5468 KB Output is correct
10 Correct 13 ms 5280 KB Output is correct
11 Correct 6 ms 5468 KB Output is correct
12 Correct 9 ms 5280 KB Output is correct
13 Correct 13 ms 5468 KB Output is correct
14 Correct 9 ms 5468 KB Output is correct
15 Correct 16 ms 5280 KB Output is correct
16 Correct 13 ms 5468 KB Output is correct
17 Correct 9 ms 5280 KB Output is correct
18 Correct 9 ms 5472 KB Output is correct
19 Correct 16 ms 5280 KB Output is correct
20 Correct 16 ms 5468 KB Output is correct
21 Correct 13 ms 5148 KB Output is correct
22 Correct 16 ms 5436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4644 KB Output is correct
2 Correct 0 ms 4644 KB Output is correct
3 Correct 0 ms 4644 KB Output is correct
4 Correct 0 ms 4640 KB Output is correct
5 Correct 0 ms 4644 KB Output is correct
6 Correct 3 ms 4644 KB Output is correct
7 Correct 0 ms 4640 KB Output is correct
8 Correct 0 ms 4640 KB Output is correct
9 Correct 0 ms 4644 KB Output is correct
10 Correct 3 ms 4644 KB Output is correct
11 Correct 0 ms 4640 KB Output is correct
12 Correct 0 ms 4640 KB Output is correct
13 Correct 0 ms 4644 KB Output is correct
14 Correct 0 ms 4640 KB Output is correct
15 Correct 0 ms 4640 KB Output is correct
16 Correct 3 ms 4640 KB Output is correct
17 Correct 0 ms 4644 KB Output is correct
18 Correct 0 ms 4644 KB Output is correct
19 Correct 3 ms 4640 KB Output is correct
20 Correct 0 ms 4640 KB Output is correct
21 Correct 0 ms 4620 KB Output is correct
22 Correct 0 ms 4644 KB Output is correct
23 Correct 0 ms 4640 KB Output is correct
24 Correct 0 ms 4644 KB Output is correct
25 Correct 3 ms 4640 KB Output is correct
26 Correct 6 ms 4776 KB Output is correct
27 Correct 13 ms 4772 KB Output is correct
28 Correct 9 ms 4772 KB Output is correct
29 Correct 9 ms 4776 KB Output is correct
30 Correct 9 ms 4772 KB Output is correct
31 Correct 16 ms 4776 KB Output is correct
32 Correct 6 ms 4776 KB Output is correct
33 Correct 13 ms 4776 KB Output is correct
34 Correct 3 ms 4776 KB Output is correct
35 Correct 9 ms 4776 KB Output is correct
36 Correct 9 ms 4772 KB Output is correct
37 Correct 19 ms 4772 KB Output is correct
38 Correct 6 ms 4776 KB Output is correct
39 Correct 16 ms 4776 KB Output is correct
40 Correct 6 ms 4776 KB Output is correct
41 Correct 13 ms 4772 KB Output is correct
42 Correct 9 ms 4776 KB Output is correct
43 Correct 9 ms 4776 KB Output is correct
44 Correct 6 ms 4772 KB Output is correct
45 Correct 16 ms 4772 KB Output is correct
46 Correct 6 ms 4776 KB Output is correct
47 Correct 0 ms 4752 KB Output is correct
48 Correct 9 ms 4776 KB Output is correct
49 Correct 9 ms 4772 KB Output is correct
50 Correct 6 ms 4772 KB Output is correct
51 Correct 6 ms 4772 KB Output is correct
52 Correct 23 ms 5468 KB Output is correct
53 Correct 23 ms 5280 KB Output is correct
54 Correct 9 ms 5472 KB Output is correct
55 Correct 16 ms 5280 KB Output is correct
56 Correct 13 ms 5468 KB Output is correct
57 Correct 13 ms 5280 KB Output is correct
58 Correct 13 ms 5472 KB Output is correct
59 Correct 9 ms 5280 KB Output is correct
60 Correct 19 ms 5468 KB Output is correct
61 Correct 13 ms 5280 KB Output is correct
62 Correct 6 ms 5468 KB Output is correct
63 Correct 9 ms 5280 KB Output is correct
64 Correct 13 ms 5468 KB Output is correct
65 Correct 9 ms 5468 KB Output is correct
66 Correct 16 ms 5280 KB Output is correct
67 Correct 13 ms 5468 KB Output is correct
68 Correct 9 ms 5280 KB Output is correct
69 Correct 9 ms 5472 KB Output is correct
70 Correct 16 ms 5280 KB Output is correct
71 Correct 16 ms 5468 KB Output is correct
72 Correct 13 ms 5148 KB Output is correct
73 Correct 16 ms 5436 KB Output is correct
74 Correct 69 ms 6736 KB Output is correct
75 Correct 109 ms 6624 KB Output is correct
76 Correct 76 ms 6736 KB Output is correct
77 Correct 109 ms 6624 KB Output is correct
78 Correct 79 ms 6736 KB Output is correct
79 Correct 73 ms 6736 KB Output is correct
80 Correct 106 ms 6620 KB Output is correct
81 Correct 63 ms 6492 KB Output is correct
82 Correct 26 ms 6736 KB Output is correct
83 Correct 76 ms 6736 KB Output is correct
84 Correct 106 ms 6620 KB Output is correct
85 Correct 69 ms 6736 KB Output is correct
86 Correct 109 ms 6624 KB Output is correct
87 Correct 59 ms 6736 KB Output is correct
88 Correct 103 ms 6620 KB Output is correct
89 Correct 66 ms 6736 KB Output is correct
90 Correct 103 ms 6620 KB Output is correct
91 Correct 66 ms 6736 KB Output is correct
92 Correct 113 ms 6620 KB Output is correct
93 Correct 76 ms 6736 KB Output is correct
94 Correct 53 ms 6336 KB Output is correct
95 Correct 89 ms 6732 KB Output is correct
96 Correct 69 ms 6732 KB Output is correct
97 Correct 53 ms 6736 KB Output is correct
98 Correct 66 ms 6736 KB Output is correct