Submission #1110901

# Submission time Handle Problem Language Result Execution time Memory
1110901 2024-11-11T00:27:38 Z Rainmaker2627 Tug of War (BOI15_tug) C++17
100 / 100
437 ms 5712 KB
#include<bits/stdc++.h>
using namespace std;

int n, k, c=0;
vector<int> L, R, ind, vis;
vector<vector<pair<int, int>>> adj;

void dfs(int u, int p) {
	if (vis[u]==0) vis[u]=2;
	for (auto [v, w] : adj[u]) {
		if (vis[v]==1 || vis[v]==2) continue;
		if (v==p) { p=-1; continue; }
		if (u>n) R[c]+=w;
		else L[c]+=w;
		dfs(v, u);
	}
}

int main() {
	cin.tie(0)->sync_with_stdio(false);

	int t=0;
	cin >> n >> k;
	ind.assign(2*n+1, 0);
	adj.assign(2*n+1, vector<pair<int, int>>());
	for (int i = 0; i < 2*n; ++i) {
		int l, r, s;
		cin >> l >> r >> s;
		adj[l].push_back({n+r, s});
		adj[n+r].push_back({l, s});
		ind[l]++; ind[n+r]++;
		t+=s;
	}

	queue<int> q;
	vis.assign(2*n+1, 0);
	for (int i = 1; i <= 2*n; ++i) {
		if (ind[i]==1) q.push(i);
		if (ind[i]==0) {
			cout << "NO\n";
			return 0;
		}
	}

	int l=0, r=0;
	while (!q.empty()) {
		int u=q.front();
		q.pop();

		vis[u]=1;
		for (auto [v, w] : adj[u]) {
			if (vis[v]) continue;
			if (--ind[v]==1) q.push(v);
			if (ind[v]==0) {
				cout << "NO\n";
				return 0;
			}
			if (u>n) r+=w;
			else l+=w;
		}
	}

	for (int i = 1; i <= n; ++i) {
		if (vis[i]) continue;
		L.push_back(0);
		R.push_back(0);
		vis[i]=3;
		dfs(i, i);
		c++;
	}

	int m=abs(l-r);
	bitset<600001> dp;
	dp[0]=1; dp|=dp<<abs(l-r);
	for (int i = 0; i < c; ++i) {
		dp|=(dp<<abs(L[i]-R[i]));
		m+=abs(L[i]-R[i]);
	}

	for (int i = 0; i <= m; ++i) {
		if (dp[i] && abs(2*i-m)<=k) {
			cout << "YES\n";
			return 0;
		}
	} cout << "NO\n";

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 760 KB Output is correct
6 Correct 1 ms 808 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 760 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 848 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 760 KB Output is correct
6 Correct 1 ms 808 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 760 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 848 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 29 ms 848 KB Output is correct
27 Correct 6 ms 848 KB Output is correct
28 Correct 28 ms 1016 KB Output is correct
29 Correct 6 ms 848 KB Output is correct
30 Correct 28 ms 996 KB Output is correct
31 Correct 7 ms 848 KB Output is correct
32 Correct 28 ms 848 KB Output is correct
33 Correct 6 ms 848 KB Output is correct
34 Correct 4 ms 848 KB Output is correct
35 Correct 6 ms 848 KB Output is correct
36 Correct 29 ms 996 KB Output is correct
37 Correct 7 ms 848 KB Output is correct
38 Correct 29 ms 848 KB Output is correct
39 Correct 6 ms 848 KB Output is correct
40 Correct 27 ms 848 KB Output is correct
41 Correct 6 ms 848 KB Output is correct
42 Correct 28 ms 996 KB Output is correct
43 Correct 6 ms 848 KB Output is correct
44 Correct 27 ms 1020 KB Output is correct
45 Correct 8 ms 848 KB Output is correct
46 Correct 28 ms 848 KB Output is correct
47 Correct 2 ms 848 KB Output is correct
48 Correct 18 ms 848 KB Output is correct
49 Correct 16 ms 848 KB Output is correct
50 Correct 36 ms 848 KB Output is correct
51 Correct 28 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 2128 KB Output is correct
2 Correct 7 ms 2160 KB Output is correct
3 Correct 79 ms 2168 KB Output is correct
4 Correct 8 ms 2128 KB Output is correct
5 Correct 84 ms 2364 KB Output is correct
6 Correct 6 ms 2152 KB Output is correct
7 Correct 74 ms 2128 KB Output is correct
8 Correct 7 ms 2128 KB Output is correct
9 Correct 74 ms 2128 KB Output is correct
10 Correct 8 ms 2128 KB Output is correct
11 Correct 91 ms 2284 KB Output is correct
12 Correct 7 ms 2140 KB Output is correct
13 Correct 95 ms 2120 KB Output is correct
14 Correct 84 ms 2236 KB Output is correct
15 Correct 7 ms 2128 KB Output is correct
16 Correct 79 ms 2128 KB Output is correct
17 Correct 9 ms 2128 KB Output is correct
18 Correct 79 ms 2128 KB Output is correct
19 Correct 9 ms 2140 KB Output is correct
20 Correct 76 ms 2128 KB Output is correct
21 Correct 7 ms 2128 KB Output is correct
22 Correct 42 ms 2128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 760 KB Output is correct
6 Correct 1 ms 808 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 760 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 1 ms 848 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 29 ms 848 KB Output is correct
27 Correct 6 ms 848 KB Output is correct
28 Correct 28 ms 1016 KB Output is correct
29 Correct 6 ms 848 KB Output is correct
30 Correct 28 ms 996 KB Output is correct
31 Correct 7 ms 848 KB Output is correct
32 Correct 28 ms 848 KB Output is correct
33 Correct 6 ms 848 KB Output is correct
34 Correct 4 ms 848 KB Output is correct
35 Correct 6 ms 848 KB Output is correct
36 Correct 29 ms 996 KB Output is correct
37 Correct 7 ms 848 KB Output is correct
38 Correct 29 ms 848 KB Output is correct
39 Correct 6 ms 848 KB Output is correct
40 Correct 27 ms 848 KB Output is correct
41 Correct 6 ms 848 KB Output is correct
42 Correct 28 ms 996 KB Output is correct
43 Correct 6 ms 848 KB Output is correct
44 Correct 27 ms 1020 KB Output is correct
45 Correct 8 ms 848 KB Output is correct
46 Correct 28 ms 848 KB Output is correct
47 Correct 2 ms 848 KB Output is correct
48 Correct 18 ms 848 KB Output is correct
49 Correct 16 ms 848 KB Output is correct
50 Correct 36 ms 848 KB Output is correct
51 Correct 28 ms 1016 KB Output is correct
52 Correct 76 ms 2128 KB Output is correct
53 Correct 7 ms 2160 KB Output is correct
54 Correct 79 ms 2168 KB Output is correct
55 Correct 8 ms 2128 KB Output is correct
56 Correct 84 ms 2364 KB Output is correct
57 Correct 6 ms 2152 KB Output is correct
58 Correct 74 ms 2128 KB Output is correct
59 Correct 7 ms 2128 KB Output is correct
60 Correct 74 ms 2128 KB Output is correct
61 Correct 8 ms 2128 KB Output is correct
62 Correct 91 ms 2284 KB Output is correct
63 Correct 7 ms 2140 KB Output is correct
64 Correct 95 ms 2120 KB Output is correct
65 Correct 84 ms 2236 KB Output is correct
66 Correct 7 ms 2128 KB Output is correct
67 Correct 79 ms 2128 KB Output is correct
68 Correct 9 ms 2128 KB Output is correct
69 Correct 79 ms 2128 KB Output is correct
70 Correct 9 ms 2140 KB Output is correct
71 Correct 76 ms 2128 KB Output is correct
72 Correct 7 ms 2128 KB Output is correct
73 Correct 42 ms 2128 KB Output is correct
74 Correct 437 ms 5456 KB Output is correct
75 Correct 46 ms 5200 KB Output is correct
76 Correct 410 ms 5456 KB Output is correct
77 Correct 36 ms 5200 KB Output is correct
78 Correct 409 ms 5588 KB Output is correct
79 Correct 412 ms 5456 KB Output is correct
80 Correct 41 ms 5124 KB Output is correct
81 Correct 38 ms 5160 KB Output is correct
82 Correct 423 ms 5456 KB Output is correct
83 Correct 408 ms 5456 KB Output is correct
84 Correct 41 ms 5224 KB Output is correct
85 Correct 406 ms 5456 KB Output is correct
86 Correct 36 ms 5252 KB Output is correct
87 Correct 408 ms 5704 KB Output is correct
88 Correct 47 ms 5200 KB Output is correct
89 Correct 408 ms 5456 KB Output is correct
90 Correct 39 ms 5176 KB Output is correct
91 Correct 423 ms 5576 KB Output is correct
92 Correct 39 ms 5024 KB Output is correct
93 Correct 419 ms 5576 KB Output is correct
94 Correct 20 ms 5200 KB Output is correct
95 Correct 214 ms 5712 KB Output is correct
96 Correct 213 ms 5704 KB Output is correct
97 Correct 429 ms 5452 KB Output is correct
98 Correct 415 ms 5456 KB Output is correct