Submission #120068

# Submission time Handle Problem Language Result Execution time Memory
120068 2019-06-23T07:30:02 Z E869120 Tug of War (BOI15_tug) C++14
100 / 100
1986 ms 14468 KB
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
#pragma warning (disable: 4996)

bitset<1200001>F;

bool solve(int N, int K, vector<int>G) {
	F.set(0);
	for (int i = 0; i < G.size(); i++) {
		F |= (F << (G[i] * 2));
	}
	int sm = 0; for (int i = 0; i < G.size(); i++) sm += G[i];
	for (int i = sm - K; i <= sm + K; i++) {
		if (i >= 0 && i <= sm * 2 && F[i] == 1) return true;
	}
	return false;
}

set<pair<int, int>> G[60009];
int N, K, V1, V2, A[60009], B[60009], S[60009]; bool used[60009], colored[60009];
vector<pair<int, int>> X[60009];

int main() {
	scanf("%d%d", &N, &K);
	for (int i = 1; i <= N * 2; i++) {
		scanf("%d%d%d", &A[i], &B[i], &S[i]);
		G[A[i]].insert(make_pair(B[i] + N, i));
		G[B[i] + N].insert(make_pair(A[i], i));
	}

	queue<int>Q;
	for (int i = 1; i <= 2 * N; i++) {
		if (G[i].size() == 1) Q.push(i);
	}
	while (!Q.empty()) {
		int pos = Q.front(); Q.pop();
		auto itr = G[pos].begin();
		pair<int, int> E = (*itr);

		if (pos <= N) V1 += S[E.second];
		else V2 += S[E.second];

		used[E.second] = false; colored[pos] = true;
		G[pos].erase(make_pair(E.first, E.second));
		G[E.first].erase(make_pair(pos, E.second)); if (G[E.first].size() == 1) Q.push(E.first);
	}
	for (int i = 1; i <= 2 * N; i++) {
		if (colored[i] == false && G[i].size() == 0) {
			cout << "NO" << endl;
			return 0;
		}
	}
	for (int i = 1; i <= 2 * N; i++) {
		auto itr = G[i].begin();
		while (itr != G[i].end()) {
			X[i].push_back(*itr);
			itr++;
		}
	}

	vector<int>Z; Z.push_back(abs(V1 - V2));
	for (int i = 1; i <= 2 * N; i++) {
		if (colored[i] == true) continue;

		vector<int>E;
		if (X[i][0].first == X[i][1].first) {
			colored[i] = true; colored[X[i][0].first] = true;
			E.push_back(S[X[i][0].second]);
			E.push_back(S[X[i][1].second]);
		}
		else {
			int cx = i;
			while (true) {
				int nex = -1, cost = -1;
				for (int j = 0; j < X[cx].size(); j++) {
					if (colored[X[cx][j].first] == false) { nex = X[cx][j].first; cost = S[X[cx][j].second]; }
				}
				if (nex == -1) break;
				cx = nex; colored[cx] = true; E.push_back(cost);
			}
		}

		int pa = 0, pb = 0;
		for (int j = 0; j < E.size(); j++) { if (j % 2 == 0) pa += E[j]; else pb += E[j]; }
		Z.push_back(abs(pa - pb));
	}

	bool FinalAns = solve(N, K, Z);
	if (FinalAns == true) cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}

Compilation message

tug.cpp:7:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning (disable: 4996)
 
tug.cpp: In function 'bool solve(int, int, std::vector<int>)':
tug.cpp:13:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < G.size(); i++) {
                  ~~^~~~~~~~~~
tug.cpp:16:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  int sm = 0; for (int i = 0; i < G.size(); i++) sm += G[i];
                              ~~^~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:79:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < X[cx].size(); j++) {
                     ~~^~~~~~~~~~~~~~
tug.cpp:88:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < E.size(); j++) { if (j % 2 == 0) pa += E[j]; else pb += E[j]; }
                   ~~^~~~~~~~~~
tug.cpp:28: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:30:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &A[i], &B[i], &S[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 4988 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 4992 KB Output is correct
17 Correct 16 ms 4992 KB Output is correct
18 Correct 18 ms 4992 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 6 ms 4608 KB Output is correct
22 Correct 6 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 7 ms 4992 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 4988 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 4992 KB Output is correct
17 Correct 16 ms 4992 KB Output is correct
18 Correct 18 ms 4992 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 6 ms 4608 KB Output is correct
22 Correct 6 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 7 ms 4992 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 134 ms 5684 KB Output is correct
27 Correct 26 ms 5760 KB Output is correct
28 Correct 131 ms 5632 KB Output is correct
29 Correct 26 ms 5632 KB Output is correct
30 Correct 128 ms 5652 KB Output is correct
31 Correct 27 ms 5636 KB Output is correct
32 Correct 129 ms 5756 KB Output is correct
33 Correct 25 ms 5632 KB Output is correct
34 Correct 18 ms 5632 KB Output is correct
35 Correct 27 ms 5632 KB Output is correct
36 Correct 131 ms 5752 KB Output is correct
37 Correct 26 ms 5632 KB Output is correct
38 Correct 137 ms 5632 KB Output is correct
39 Correct 27 ms 5632 KB Output is correct
40 Correct 132 ms 5632 KB Output is correct
41 Correct 27 ms 5632 KB Output is correct
42 Correct 134 ms 5632 KB Output is correct
43 Correct 26 ms 5632 KB Output is correct
44 Correct 128 ms 5632 KB Output is correct
45 Correct 27 ms 5632 KB Output is correct
46 Correct 125 ms 5632 KB Output is correct
47 Correct 9 ms 4992 KB Output is correct
48 Correct 72 ms 5504 KB Output is correct
49 Correct 67 ms 5476 KB Output is correct
50 Correct 128 ms 5632 KB Output is correct
51 Correct 132 ms 5632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 398 ms 7800 KB Output is correct
2 Correct 24 ms 6732 KB Output is correct
3 Correct 383 ms 7900 KB Output is correct
4 Correct 24 ms 6784 KB Output is correct
5 Correct 417 ms 7936 KB Output is correct
6 Correct 23 ms 6784 KB Output is correct
7 Correct 419 ms 7936 KB Output is correct
8 Correct 27 ms 6776 KB Output is correct
9 Correct 411 ms 7936 KB Output is correct
10 Correct 20 ms 6784 KB Output is correct
11 Correct 411 ms 7916 KB Output is correct
12 Correct 22 ms 6776 KB Output is correct
13 Correct 405 ms 8028 KB Output is correct
14 Correct 404 ms 7940 KB Output is correct
15 Correct 20 ms 6784 KB Output is correct
16 Correct 403 ms 7912 KB Output is correct
17 Correct 23 ms 6776 KB Output is correct
18 Correct 406 ms 8056 KB Output is correct
19 Correct 21 ms 6784 KB Output is correct
20 Correct 392 ms 7936 KB Output is correct
21 Correct 18 ms 6784 KB Output is correct
22 Correct 199 ms 7168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 7 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 4988 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 6 ms 4992 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 4992 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 4992 KB Output is correct
17 Correct 16 ms 4992 KB Output is correct
18 Correct 18 ms 4992 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 6 ms 4608 KB Output is correct
22 Correct 6 ms 4992 KB Output is correct
23 Correct 7 ms 4992 KB Output is correct
24 Correct 7 ms 4992 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 134 ms 5684 KB Output is correct
27 Correct 26 ms 5760 KB Output is correct
28 Correct 131 ms 5632 KB Output is correct
29 Correct 26 ms 5632 KB Output is correct
30 Correct 128 ms 5652 KB Output is correct
31 Correct 27 ms 5636 KB Output is correct
32 Correct 129 ms 5756 KB Output is correct
33 Correct 25 ms 5632 KB Output is correct
34 Correct 18 ms 5632 KB Output is correct
35 Correct 27 ms 5632 KB Output is correct
36 Correct 131 ms 5752 KB Output is correct
37 Correct 26 ms 5632 KB Output is correct
38 Correct 137 ms 5632 KB Output is correct
39 Correct 27 ms 5632 KB Output is correct
40 Correct 132 ms 5632 KB Output is correct
41 Correct 27 ms 5632 KB Output is correct
42 Correct 134 ms 5632 KB Output is correct
43 Correct 26 ms 5632 KB Output is correct
44 Correct 128 ms 5632 KB Output is correct
45 Correct 27 ms 5632 KB Output is correct
46 Correct 125 ms 5632 KB Output is correct
47 Correct 9 ms 4992 KB Output is correct
48 Correct 72 ms 5504 KB Output is correct
49 Correct 67 ms 5476 KB Output is correct
50 Correct 128 ms 5632 KB Output is correct
51 Correct 132 ms 5632 KB Output is correct
52 Correct 398 ms 7800 KB Output is correct
53 Correct 24 ms 6732 KB Output is correct
54 Correct 383 ms 7900 KB Output is correct
55 Correct 24 ms 6784 KB Output is correct
56 Correct 417 ms 7936 KB Output is correct
57 Correct 23 ms 6784 KB Output is correct
58 Correct 419 ms 7936 KB Output is correct
59 Correct 27 ms 6776 KB Output is correct
60 Correct 411 ms 7936 KB Output is correct
61 Correct 20 ms 6784 KB Output is correct
62 Correct 411 ms 7916 KB Output is correct
63 Correct 22 ms 6776 KB Output is correct
64 Correct 405 ms 8028 KB Output is correct
65 Correct 404 ms 7940 KB Output is correct
66 Correct 20 ms 6784 KB Output is correct
67 Correct 403 ms 7912 KB Output is correct
68 Correct 23 ms 6776 KB Output is correct
69 Correct 406 ms 8056 KB Output is correct
70 Correct 21 ms 6784 KB Output is correct
71 Correct 392 ms 7936 KB Output is correct
72 Correct 18 ms 6784 KB Output is correct
73 Correct 199 ms 7168 KB Output is correct
74 Correct 1908 ms 13964 KB Output is correct
75 Correct 190 ms 14200 KB Output is correct
76 Correct 1952 ms 14392 KB Output is correct
77 Correct 166 ms 14072 KB Output is correct
78 Correct 1883 ms 14384 KB Output is correct
79 Correct 1908 ms 14456 KB Output is correct
80 Correct 167 ms 14072 KB Output is correct
81 Correct 173 ms 14080 KB Output is correct
82 Correct 1834 ms 14380 KB Output is correct
83 Correct 1861 ms 14388 KB Output is correct
84 Correct 165 ms 14072 KB Output is correct
85 Correct 1880 ms 14380 KB Output is correct
86 Correct 167 ms 14156 KB Output is correct
87 Correct 1893 ms 14468 KB Output is correct
88 Correct 162 ms 14072 KB Output is correct
89 Correct 1845 ms 14376 KB Output is correct
90 Correct 157 ms 14072 KB Output is correct
91 Correct 1857 ms 14388 KB Output is correct
92 Correct 165 ms 14072 KB Output is correct
93 Correct 1842 ms 14424 KB Output is correct
94 Correct 69 ms 11896 KB Output is correct
95 Correct 969 ms 12644 KB Output is correct
96 Correct 953 ms 12536 KB Output is correct
97 Correct 1986 ms 14376 KB Output is correct
98 Correct 1934 ms 14400 KB Output is correct