Submission #717011

# Submission time Handle Problem Language Result Execution time Memory
717011 2023-03-31T20:54:49 Z AmirAli_H1 Tug of War (BOI15_tug) C++17
100 / 100
1152 ms 13984 KB
// In the name of Allah

#include <bits/stdc++.h>
using namespace std;

typedef long long int	ll;
typedef long double	ld;
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;

#define all(x)		(x).begin(),(x).end()
#define len(x)		((ll) (x).size())
#define F		first
#define S		second
#define pb		push_back
#define sep             ' '
#define endl            '\n'
#define Mp		make_pair
#define debug(x)	cerr << #x << ": " <<  x << endl;
#define kill(x)		cout << x << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define file_io(x,y)	freopen(x, "r", stdin); freopen(y, "w", stdout);

int n, k;
const int maxn = 6e4 + 5;
const int maxs = 6e5 + 4;
int M = 3e5 + 2;
multiset<pii> adj[maxn];
vector<pair<pii, int>> E;
bool mark[maxn]; int col[maxn], c = 1;
int D[maxn], T[maxn];
vector<int> A[maxn];
queue<int> qu;
ll res = 0;
vector<ll> vc;
bitset<maxs> dp;

void dfs(int v) {
	mark[v] = 1;
	col[v] = c; D[c] += len(adj[v]); T[c]++;
	A[c].pb(v);
	for (auto f : adj[v]) {
		auto [u, w] = f;
		if (!mark[u]) dfs(u);
	}
}

void get_res() {
	for (int i = 0; i < 2 * n; i++) {
		if (len(adj[i]) == 1) {
			qu.push(i);
		}
		else if (len(adj[i]) == 0) kill("NO");
	}
	
	while (!qu.empty()) {
		int i = qu.front(); qu.pop();
		if (mark[i]) continue;
		
		mark[i] = 1;
		auto [j, w] = *adj[i].begin();
		adj[i].clear();
		
		res += w;
		adj[j].erase(adj[j].find(Mp(i, -w)));
		if (!mark[j]) {
			if (len(adj[j]) == 1) {
				qu.push(j);
			}
			else if (len(adj[j]) == 0) kill("NO");
		}
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> k;
	for (int i = 0; i < 2 * n; i++) {
		int u, v, w;
		cin >> u >> v >> w; u--; v--;
		v += n;
		adj[u].insert(Mp(v, w)); adj[v].insert(Mp(u, -w));
		E.pb(Mp(Mp(u, v), w));
	}
	
	get_res();
	
	for (int i = 0; i < 2 * n; i++) {
		if (!mark[i]) {
			dfs(i); c++;
		}
	}
	
	for (int i = 1; i < c; i++) {
		if (D[i] != 2 * T[i]) kill("NO");
		ll m = 0;
		for (int j = 0; j < len(A[i]); j++) {
			int u = A[i][j], v = A[i][(j + 1) % len(A[i])];
			for (auto f : adj[u]) {
				auto [vx, wx] = f;
				if (v == vx) {
					adj[u].erase(adj[u].find(Mp(v, wx)));
					adj[v].erase(adj[v].find(Mp(u, -wx)));
					m += wx;
					break;
				}
			}
		}
		
		vc.pb(m);
	}
	
	M += res;
	dp[M] = 1;
	for (ll val : vc) {
		ll x = abs(val);
		dp = (dp >> x) | (dp << x);
	}
	
	bool flag = 0;
	for (int i = -k - res; i <= k - res; i++) {
		if (dp[i + M] && abs(res + i) <= k) {
			flag = 1;
			break;
		}
	}
	
	if (flag) cout << "YES" << endl;
	else cout << "NO" << endl;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4820 KB Output is correct
2 Correct 2 ms 4820 KB Output is correct
3 Correct 3 ms 4780 KB Output is correct
4 Correct 3 ms 4784 KB Output is correct
5 Correct 3 ms 4820 KB Output is correct
6 Correct 3 ms 4776 KB Output is correct
7 Correct 3 ms 4820 KB Output is correct
8 Correct 3 ms 4780 KB Output is correct
9 Correct 3 ms 4780 KB Output is correct
10 Correct 3 ms 4820 KB Output is correct
11 Correct 4 ms 4820 KB Output is correct
12 Correct 3 ms 4820 KB Output is correct
13 Correct 3 ms 4772 KB Output is correct
14 Correct 3 ms 4780 KB Output is correct
15 Correct 2 ms 4820 KB Output is correct
16 Correct 3 ms 4820 KB Output is correct
17 Correct 3 ms 4784 KB Output is correct
18 Correct 3 ms 4736 KB Output is correct
19 Correct 2 ms 4820 KB Output is correct
20 Correct 3 ms 4820 KB Output is correct
21 Correct 3 ms 4780 KB Output is correct
22 Correct 4 ms 4764 KB Output is correct
23 Correct 2 ms 4820 KB Output is correct
24 Correct 3 ms 4756 KB Output is correct
25 Correct 4 ms 4780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4820 KB Output is correct
2 Correct 2 ms 4820 KB Output is correct
3 Correct 3 ms 4780 KB Output is correct
4 Correct 3 ms 4784 KB Output is correct
5 Correct 3 ms 4820 KB Output is correct
6 Correct 3 ms 4776 KB Output is correct
7 Correct 3 ms 4820 KB Output is correct
8 Correct 3 ms 4780 KB Output is correct
9 Correct 3 ms 4780 KB Output is correct
10 Correct 3 ms 4820 KB Output is correct
11 Correct 4 ms 4820 KB Output is correct
12 Correct 3 ms 4820 KB Output is correct
13 Correct 3 ms 4772 KB Output is correct
14 Correct 3 ms 4780 KB Output is correct
15 Correct 2 ms 4820 KB Output is correct
16 Correct 3 ms 4820 KB Output is correct
17 Correct 3 ms 4784 KB Output is correct
18 Correct 3 ms 4736 KB Output is correct
19 Correct 2 ms 4820 KB Output is correct
20 Correct 3 ms 4820 KB Output is correct
21 Correct 3 ms 4780 KB Output is correct
22 Correct 4 ms 4764 KB Output is correct
23 Correct 2 ms 4820 KB Output is correct
24 Correct 3 ms 4756 KB Output is correct
25 Correct 4 ms 4780 KB Output is correct
26 Correct 94 ms 5416 KB Output is correct
27 Correct 13 ms 5304 KB Output is correct
28 Correct 69 ms 5460 KB Output is correct
29 Correct 14 ms 5296 KB Output is correct
30 Correct 74 ms 5460 KB Output is correct
31 Correct 21 ms 5300 KB Output is correct
32 Correct 70 ms 5444 KB Output is correct
33 Correct 14 ms 5392 KB Output is correct
34 Correct 10 ms 5372 KB Output is correct
35 Correct 15 ms 5384 KB Output is correct
36 Correct 68 ms 5440 KB Output is correct
37 Correct 14 ms 5332 KB Output is correct
38 Correct 73 ms 5444 KB Output is correct
39 Correct 14 ms 5388 KB Output is correct
40 Correct 71 ms 5460 KB Output is correct
41 Correct 14 ms 5300 KB Output is correct
42 Correct 68 ms 5428 KB Output is correct
43 Correct 14 ms 5392 KB Output is correct
44 Correct 95 ms 5436 KB Output is correct
45 Correct 14 ms 5332 KB Output is correct
46 Correct 93 ms 5436 KB Output is correct
47 Correct 4 ms 5172 KB Output is correct
48 Correct 37 ms 5332 KB Output is correct
49 Correct 37 ms 5332 KB Output is correct
50 Correct 71 ms 5460 KB Output is correct
51 Correct 75 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 199 ms 7860 KB Output is correct
2 Correct 12 ms 7056 KB Output is correct
3 Correct 201 ms 7788 KB Output is correct
4 Correct 15 ms 7056 KB Output is correct
5 Correct 202 ms 7796 KB Output is correct
6 Correct 11 ms 7056 KB Output is correct
7 Correct 236 ms 7824 KB Output is correct
8 Correct 11 ms 7056 KB Output is correct
9 Correct 201 ms 7824 KB Output is correct
10 Correct 12 ms 7056 KB Output is correct
11 Correct 202 ms 7824 KB Output is correct
12 Correct 12 ms 7028 KB Output is correct
13 Correct 244 ms 7856 KB Output is correct
14 Correct 214 ms 7824 KB Output is correct
15 Correct 12 ms 7076 KB Output is correct
16 Correct 206 ms 7904 KB Output is correct
17 Correct 13 ms 7056 KB Output is correct
18 Correct 216 ms 7792 KB Output is correct
19 Correct 11 ms 7056 KB Output is correct
20 Correct 196 ms 7824 KB Output is correct
21 Correct 13 ms 7056 KB Output is correct
22 Correct 119 ms 7440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4820 KB Output is correct
2 Correct 2 ms 4820 KB Output is correct
3 Correct 3 ms 4780 KB Output is correct
4 Correct 3 ms 4784 KB Output is correct
5 Correct 3 ms 4820 KB Output is correct
6 Correct 3 ms 4776 KB Output is correct
7 Correct 3 ms 4820 KB Output is correct
8 Correct 3 ms 4780 KB Output is correct
9 Correct 3 ms 4780 KB Output is correct
10 Correct 3 ms 4820 KB Output is correct
11 Correct 4 ms 4820 KB Output is correct
12 Correct 3 ms 4820 KB Output is correct
13 Correct 3 ms 4772 KB Output is correct
14 Correct 3 ms 4780 KB Output is correct
15 Correct 2 ms 4820 KB Output is correct
16 Correct 3 ms 4820 KB Output is correct
17 Correct 3 ms 4784 KB Output is correct
18 Correct 3 ms 4736 KB Output is correct
19 Correct 2 ms 4820 KB Output is correct
20 Correct 3 ms 4820 KB Output is correct
21 Correct 3 ms 4780 KB Output is correct
22 Correct 4 ms 4764 KB Output is correct
23 Correct 2 ms 4820 KB Output is correct
24 Correct 3 ms 4756 KB Output is correct
25 Correct 4 ms 4780 KB Output is correct
26 Correct 94 ms 5416 KB Output is correct
27 Correct 13 ms 5304 KB Output is correct
28 Correct 69 ms 5460 KB Output is correct
29 Correct 14 ms 5296 KB Output is correct
30 Correct 74 ms 5460 KB Output is correct
31 Correct 21 ms 5300 KB Output is correct
32 Correct 70 ms 5444 KB Output is correct
33 Correct 14 ms 5392 KB Output is correct
34 Correct 10 ms 5372 KB Output is correct
35 Correct 15 ms 5384 KB Output is correct
36 Correct 68 ms 5440 KB Output is correct
37 Correct 14 ms 5332 KB Output is correct
38 Correct 73 ms 5444 KB Output is correct
39 Correct 14 ms 5388 KB Output is correct
40 Correct 71 ms 5460 KB Output is correct
41 Correct 14 ms 5300 KB Output is correct
42 Correct 68 ms 5428 KB Output is correct
43 Correct 14 ms 5392 KB Output is correct
44 Correct 95 ms 5436 KB Output is correct
45 Correct 14 ms 5332 KB Output is correct
46 Correct 93 ms 5436 KB Output is correct
47 Correct 4 ms 5172 KB Output is correct
48 Correct 37 ms 5332 KB Output is correct
49 Correct 37 ms 5332 KB Output is correct
50 Correct 71 ms 5460 KB Output is correct
51 Correct 75 ms 5460 KB Output is correct
52 Correct 199 ms 7860 KB Output is correct
53 Correct 12 ms 7056 KB Output is correct
54 Correct 201 ms 7788 KB Output is correct
55 Correct 15 ms 7056 KB Output is correct
56 Correct 202 ms 7796 KB Output is correct
57 Correct 11 ms 7056 KB Output is correct
58 Correct 236 ms 7824 KB Output is correct
59 Correct 11 ms 7056 KB Output is correct
60 Correct 201 ms 7824 KB Output is correct
61 Correct 12 ms 7056 KB Output is correct
62 Correct 202 ms 7824 KB Output is correct
63 Correct 12 ms 7028 KB Output is correct
64 Correct 244 ms 7856 KB Output is correct
65 Correct 214 ms 7824 KB Output is correct
66 Correct 12 ms 7076 KB Output is correct
67 Correct 206 ms 7904 KB Output is correct
68 Correct 13 ms 7056 KB Output is correct
69 Correct 216 ms 7792 KB Output is correct
70 Correct 11 ms 7056 KB Output is correct
71 Correct 196 ms 7824 KB Output is correct
72 Correct 13 ms 7056 KB Output is correct
73 Correct 119 ms 7440 KB Output is correct
74 Correct 1140 ms 13928 KB Output is correct
75 Correct 94 ms 12640 KB Output is correct
76 Correct 1014 ms 13916 KB Output is correct
77 Correct 108 ms 12568 KB Output is correct
78 Correct 1125 ms 13984 KB Output is correct
79 Correct 1026 ms 13892 KB Output is correct
80 Correct 91 ms 12644 KB Output is correct
81 Correct 87 ms 12496 KB Output is correct
82 Correct 1031 ms 13404 KB Output is correct
83 Correct 1080 ms 13956 KB Output is correct
84 Correct 90 ms 12548 KB Output is correct
85 Correct 996 ms 13956 KB Output is correct
86 Correct 105 ms 12572 KB Output is correct
87 Correct 1046 ms 13876 KB Output is correct
88 Correct 119 ms 12616 KB Output is correct
89 Correct 1038 ms 13956 KB Output is correct
90 Correct 101 ms 12548 KB Output is correct
91 Correct 1028 ms 13952 KB Output is correct
92 Correct 102 ms 12544 KB Output is correct
93 Correct 1085 ms 13916 KB Output is correct
94 Correct 44 ms 11908 KB Output is correct
95 Correct 535 ms 12660 KB Output is correct
96 Correct 599 ms 12608 KB Output is correct
97 Correct 1063 ms 13828 KB Output is correct
98 Correct 1152 ms 13888 KB Output is correct