Submission #853666

# Submission time Handle Problem Language Result Execution time Memory
853666 2023-09-25T00:46:22 Z NK_ Tug of War (BOI15_tug) C++17
100 / 100
841 ms 10144 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 18;

const int nax = 3e4 + 10;
const int kax = 20;
const int SZ = nax * kax;
const int Z = SZ / 2;
using B = bitset<SZ>;
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
 
	int N, K; cin >> N >> K;
	int M = N; N += N;

	vi deg(N); vi W(N);
	V<set<pi>> adj(N); for(int i = 0; i < N; i++) {
		int u, v, w; cin >> u >> v >> w; --u, --v, v += M;
		W[i] = w;
		adj[u].insert(mp(v, i));
		adj[v].insert(mp(u, i));
	}

	queue<int> q; for(int i = 0; i < N; i++) {
		if (!sz(adj[i])) {
			cout << "NO" << nl;
			exit(0-0);
		}

		if (sz(adj[i]) == 1) q.push(i);
	}

	int BAL = 0;
	while(sz(q)) {
		int u = q.front(); q.pop();

		pi e = *begin(adj[u]);
		adj[u].clear();

		adj[e.f].erase(mp(u, e.s));

		if (u < M) BAL += W[e.s];
		else BAL -= W[e.s];

		if (sz(adj[e.f]) == 1) q.push(e.f);
	}

	for(int i = 0; i < N; i++) {
		if (sz(adj[i]) == 0) continue;
		if (sz(adj[i]) != 2) {
			cout << "NO" << nl;
			exit(0-0);
		}
	}
 
	int bal = 0;
	function<void(int, int)> dfs = [&](int u, int d) {
		if (sz(adj[u])) {
			pi e = *begin(adj[u]);
			adj[u].clear();
			adj[e.f].erase(mp(u, e.s));
			// cout << u << " " << e.s << endl;
			bal += d * W[e.s];
			dfs(e.f, -d);
		}
	};

	vi X;
	for(int u = 0; u < M; u++) if (sz(adj[u])) {
		bal = 0; dfs(u, 1); X.pb(abs(bal));
	}

	B dp; dp[Z + BAL] = 1;
	for(auto x : X) if (x) {
		dp = (dp << x) | (dp >> x);
	}

	for(int x = 0; x < SZ; x++) if (dp[x]) {
		// cout << x << endl;
		if (abs(x - Z) <= K) {
			cout << "YES" << nl;
			exit(0-0);
		}
	}

	cout << "NO" << nl;

	exit(0-0);
} 	 
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 820 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 820 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 53 ms 1372 KB Output is correct
27 Correct 9 ms 1372 KB Output is correct
28 Correct 54 ms 1424 KB Output is correct
29 Correct 10 ms 1372 KB Output is correct
30 Correct 54 ms 1372 KB Output is correct
31 Correct 9 ms 1372 KB Output is correct
32 Correct 53 ms 1372 KB Output is correct
33 Correct 10 ms 1372 KB Output is correct
34 Correct 6 ms 1372 KB Output is correct
35 Correct 13 ms 1820 KB Output is correct
36 Correct 52 ms 1372 KB Output is correct
37 Correct 9 ms 1368 KB Output is correct
38 Correct 54 ms 1432 KB Output is correct
39 Correct 10 ms 1372 KB Output is correct
40 Correct 52 ms 1372 KB Output is correct
41 Correct 10 ms 1368 KB Output is correct
42 Correct 52 ms 1372 KB Output is correct
43 Correct 11 ms 1528 KB Output is correct
44 Correct 51 ms 1368 KB Output is correct
45 Correct 10 ms 1372 KB Output is correct
46 Correct 52 ms 1372 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 28 ms 1440 KB Output is correct
49 Correct 28 ms 1416 KB Output is correct
50 Correct 55 ms 1420 KB Output is correct
51 Correct 56 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3672 KB Output is correct
2 Correct 6 ms 3928 KB Output is correct
3 Correct 7 ms 3672 KB Output is correct
4 Correct 6 ms 3676 KB Output is correct
5 Correct 7 ms 3676 KB Output is correct
6 Correct 7 ms 3672 KB Output is correct
7 Correct 7 ms 3676 KB Output is correct
8 Correct 6 ms 3732 KB Output is correct
9 Correct 8 ms 3676 KB Output is correct
10 Correct 6 ms 3676 KB Output is correct
11 Correct 7 ms 3676 KB Output is correct
12 Correct 6 ms 3552 KB Output is correct
13 Correct 8 ms 3840 KB Output is correct
14 Correct 7 ms 3676 KB Output is correct
15 Correct 6 ms 3676 KB Output is correct
16 Correct 7 ms 3676 KB Output is correct
17 Correct 6 ms 3656 KB Output is correct
18 Correct 7 ms 3676 KB Output is correct
19 Correct 6 ms 3676 KB Output is correct
20 Correct 8 ms 3764 KB Output is correct
21 Correct 6 ms 3676 KB Output is correct
22 Correct 9 ms 3772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 820 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 53 ms 1372 KB Output is correct
27 Correct 9 ms 1372 KB Output is correct
28 Correct 54 ms 1424 KB Output is correct
29 Correct 10 ms 1372 KB Output is correct
30 Correct 54 ms 1372 KB Output is correct
31 Correct 9 ms 1372 KB Output is correct
32 Correct 53 ms 1372 KB Output is correct
33 Correct 10 ms 1372 KB Output is correct
34 Correct 6 ms 1372 KB Output is correct
35 Correct 13 ms 1820 KB Output is correct
36 Correct 52 ms 1372 KB Output is correct
37 Correct 9 ms 1368 KB Output is correct
38 Correct 54 ms 1432 KB Output is correct
39 Correct 10 ms 1372 KB Output is correct
40 Correct 52 ms 1372 KB Output is correct
41 Correct 10 ms 1368 KB Output is correct
42 Correct 52 ms 1372 KB Output is correct
43 Correct 11 ms 1528 KB Output is correct
44 Correct 51 ms 1368 KB Output is correct
45 Correct 10 ms 1372 KB Output is correct
46 Correct 52 ms 1372 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 28 ms 1440 KB Output is correct
49 Correct 28 ms 1416 KB Output is correct
50 Correct 55 ms 1420 KB Output is correct
51 Correct 56 ms 1372 KB Output is correct
52 Correct 7 ms 3672 KB Output is correct
53 Correct 6 ms 3928 KB Output is correct
54 Correct 7 ms 3672 KB Output is correct
55 Correct 6 ms 3676 KB Output is correct
56 Correct 7 ms 3676 KB Output is correct
57 Correct 7 ms 3672 KB Output is correct
58 Correct 7 ms 3676 KB Output is correct
59 Correct 6 ms 3732 KB Output is correct
60 Correct 8 ms 3676 KB Output is correct
61 Correct 6 ms 3676 KB Output is correct
62 Correct 7 ms 3676 KB Output is correct
63 Correct 6 ms 3552 KB Output is correct
64 Correct 8 ms 3840 KB Output is correct
65 Correct 7 ms 3676 KB Output is correct
66 Correct 6 ms 3676 KB Output is correct
67 Correct 7 ms 3676 KB Output is correct
68 Correct 6 ms 3656 KB Output is correct
69 Correct 7 ms 3676 KB Output is correct
70 Correct 6 ms 3676 KB Output is correct
71 Correct 8 ms 3764 KB Output is correct
72 Correct 6 ms 3676 KB Output is correct
73 Correct 9 ms 3772 KB Output is correct
74 Correct 788 ms 9984 KB Output is correct
75 Correct 55 ms 9564 KB Output is correct
76 Correct 765 ms 9984 KB Output is correct
77 Correct 62 ms 9556 KB Output is correct
78 Correct 774 ms 9984 KB Output is correct
79 Correct 792 ms 9988 KB Output is correct
80 Correct 55 ms 9560 KB Output is correct
81 Correct 55 ms 9564 KB Output is correct
82 Correct 812 ms 9740 KB Output is correct
83 Correct 779 ms 9984 KB Output is correct
84 Correct 68 ms 9740 KB Output is correct
85 Correct 787 ms 9988 KB Output is correct
86 Correct 70 ms 9680 KB Output is correct
87 Correct 770 ms 9812 KB Output is correct
88 Correct 54 ms 9564 KB Output is correct
89 Correct 780 ms 9988 KB Output is correct
90 Correct 53 ms 9564 KB Output is correct
91 Correct 784 ms 9976 KB Output is correct
92 Correct 55 ms 9552 KB Output is correct
93 Correct 783 ms 10144 KB Output is correct
94 Correct 19 ms 9564 KB Output is correct
95 Correct 402 ms 9900 KB Output is correct
96 Correct 404 ms 9908 KB Output is correct
97 Correct 832 ms 9984 KB Output is correct
98 Correct 841 ms 9988 KB Output is correct