#include <bits/stdc++.h>
using namespace std;
void ng() {
cerr << "NG" << endl;
cout << "NO" << endl;
exit(0);
}
int remove(int v, vector<set<tuple<int, int, int>>>& g) {
if (g[v].size() == 1) {
tuple<int, int, int> elt = *g[v].begin();
int l = v;
int r = get<0>(elt);
int s = get<1>(elt);
int i = get<2>(elt);
g[l].erase({r, s, i});
g[r].erase({l, s, i});
return s + -remove(r, g);
}
return 0;
}
int dfs_(int v, int start, int sign, vector<set<tuple<int, int, int>>>& g, vector<bool>& seen) {
seen[v] = true;
int res = 0;
sign *= -1;
bool did = false;
for (auto [nxt, s, e] : g[v]) {
if (seen[nxt]) continue;
else did = true;
cerr << "dfs " << v << " " << nxt << endl;
res += sign * s;
res += dfs_(nxt, start, sign, g, seen);
}
if (!did) {
bool ok = false;
for (auto [nxt, s, e] : g[v]) {
if (nxt == start) {
ok = true;
res += sign * s;
}
}
if (!ok) ng();
}
// cerr << v << " " << used_e << " " << sign << " " << res << endl;
return res;
}
int dfs(int v, int used_e, int end, int sign, vector<set<tuple<int, int, int>>>& g, vector<bool>& seen) {
seen[v] = true;
int res = 0;
sign *= -1;
bool did = false;
for (auto [nxt, s, e] : g[v]) {
if (e == used_e) continue;
if (seen[nxt]) continue;
did = true;
res += sign * s;
res += dfs(nxt, e, end, sign, g, seen);
}
if ((!did) && v != end) {
ng();
}
// cerr << v << " " << used_e << " " << sign << " " << res << endl;
return res;
}
int main() {
int n, k;
cin >> n >> k;
vector<set<tuple<int, int, int>>> g(2 * n);
for (int i = 0; i < 2 * n; i++) {
int l, r, s;
cin >> l >> r >> s;
l--;
r--;
r += n;
g[l].emplace(r, s, i);
g[r].emplace(l, s, i);
}
int cur_sum = 0;
for (int i = 0; i < 2 * n; i++) {
int sign = i < n ? 1 : -1;
cur_sum += remove(i, g) * sign;
}
cerr << "cur_sum: " << cur_sum << endl;
vector<int> nums;
vector<bool> seen(2 * n, false);
for (int i = 0; i < 2 * n; i++) {
if (seen[i]) continue;
if (g[i].size() == 0) continue;
int l = i;
int r = get<0>(*g[i].begin());
int s = get<1>(*g[i].begin());
int used_e = get<2>(*g[i].begin());
int num = s + dfs(l, used_e, r, 1, g, seen);
nums.push_back(num);
}
for (auto a : nums) {
cerr << a << " ";
}
cerr << endl;
int m = nums.size();
int mid = n * 20 / 2;
bitset<600001> cur(0);
cur.set(cur_sum + mid);
for (int i = 0; i < m; i++) {
int num = abs(nums[i]);
cur <<= num;
cur |= cur >> (2 * num);
/*
cerr << i << endl;
for (int i = 0; i < 600001; i++) {
if (cur.test(i)) {
cerr << i - mid << " ";
}
}
cerr << endl;
*/
}
bool ok = false;
int minimum = INT32_MAX;
for (int i = -k; i <= k; i++) {
int num = mid + i;
if (cur.test(num)) {
ok = true;
minimum = min(i, minimum);
}
}
if (ok) {
cout << "YES" << endl;
cerr << minimum << endl;
} else {
cout << "NO" << endl;
}
}
Compilation message
tug.cpp: In function 'int dfs_(int, int, int, std::vector<std::set<std::tuple<int, int, int> > >&, std::vector<bool>&)':
tug.cpp:29:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for (auto [nxt, s, e] : g[v]) {
| ^
tug.cpp:38:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | for (auto [nxt, s, e] : g[v]) {
| ^
tug.cpp: In function 'int dfs(int, int, int, int, std::vector<std::set<std::tuple<int, int, int> > >&, std::vector<bool>&)':
tug.cpp:55:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for (auto [nxt, s, e] : g[v]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
856 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 |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
760 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 |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
856 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 |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
760 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 |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
47 ms |
1372 KB |
Output is correct |
27 |
Correct |
10 ms |
1116 KB |
Output is correct |
28 |
Correct |
50 ms |
1348 KB |
Output is correct |
29 |
Correct |
9 ms |
1112 KB |
Output is correct |
30 |
Correct |
47 ms |
1368 KB |
Output is correct |
31 |
Correct |
9 ms |
1116 KB |
Output is correct |
32 |
Correct |
47 ms |
1348 KB |
Output is correct |
33 |
Correct |
9 ms |
1116 KB |
Output is correct |
34 |
Correct |
6 ms |
1116 KB |
Output is correct |
35 |
Correct |
9 ms |
1356 KB |
Output is correct |
36 |
Correct |
47 ms |
1116 KB |
Output is correct |
37 |
Correct |
9 ms |
1112 KB |
Output is correct |
38 |
Correct |
47 ms |
1276 KB |
Output is correct |
39 |
Correct |
9 ms |
1116 KB |
Output is correct |
40 |
Correct |
49 ms |
1372 KB |
Output is correct |
41 |
Correct |
9 ms |
1112 KB |
Output is correct |
42 |
Correct |
48 ms |
1372 KB |
Output is correct |
43 |
Correct |
9 ms |
1112 KB |
Output is correct |
44 |
Correct |
47 ms |
1368 KB |
Output is correct |
45 |
Correct |
9 ms |
1368 KB |
Output is correct |
46 |
Correct |
47 ms |
1116 KB |
Output is correct |
47 |
Correct |
3 ms |
1112 KB |
Output is correct |
48 |
Correct |
26 ms |
1356 KB |
Output is correct |
49 |
Correct |
25 ms |
1116 KB |
Output is correct |
50 |
Correct |
48 ms |
1344 KB |
Output is correct |
51 |
Correct |
48 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
4228 KB |
Output is correct |
2 |
Correct |
14 ms |
4024 KB |
Output is correct |
3 |
Correct |
119 ms |
4436 KB |
Output is correct |
4 |
Correct |
14 ms |
3932 KB |
Output is correct |
5 |
Correct |
113 ms |
4208 KB |
Output is correct |
6 |
Correct |
13 ms |
3932 KB |
Output is correct |
7 |
Correct |
114 ms |
4224 KB |
Output is correct |
8 |
Correct |
13 ms |
3928 KB |
Output is correct |
9 |
Correct |
119 ms |
4416 KB |
Output is correct |
10 |
Correct |
13 ms |
3932 KB |
Output is correct |
11 |
Correct |
115 ms |
4212 KB |
Output is correct |
12 |
Correct |
14 ms |
3932 KB |
Output is correct |
13 |
Correct |
121 ms |
4212 KB |
Output is correct |
14 |
Correct |
116 ms |
4188 KB |
Output is correct |
15 |
Correct |
13 ms |
3928 KB |
Output is correct |
16 |
Correct |
114 ms |
4212 KB |
Output is correct |
17 |
Correct |
13 ms |
3932 KB |
Output is correct |
18 |
Correct |
114 ms |
4228 KB |
Output is correct |
19 |
Correct |
14 ms |
3932 KB |
Output is correct |
20 |
Correct |
113 ms |
4208 KB |
Output is correct |
21 |
Correct |
14 ms |
3932 KB |
Output is correct |
22 |
Correct |
65 ms |
4164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
856 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 |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
760 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 |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
47 ms |
1372 KB |
Output is correct |
27 |
Correct |
10 ms |
1116 KB |
Output is correct |
28 |
Correct |
50 ms |
1348 KB |
Output is correct |
29 |
Correct |
9 ms |
1112 KB |
Output is correct |
30 |
Correct |
47 ms |
1368 KB |
Output is correct |
31 |
Correct |
9 ms |
1116 KB |
Output is correct |
32 |
Correct |
47 ms |
1348 KB |
Output is correct |
33 |
Correct |
9 ms |
1116 KB |
Output is correct |
34 |
Correct |
6 ms |
1116 KB |
Output is correct |
35 |
Correct |
9 ms |
1356 KB |
Output is correct |
36 |
Correct |
47 ms |
1116 KB |
Output is correct |
37 |
Correct |
9 ms |
1112 KB |
Output is correct |
38 |
Correct |
47 ms |
1276 KB |
Output is correct |
39 |
Correct |
9 ms |
1116 KB |
Output is correct |
40 |
Correct |
49 ms |
1372 KB |
Output is correct |
41 |
Correct |
9 ms |
1112 KB |
Output is correct |
42 |
Correct |
48 ms |
1372 KB |
Output is correct |
43 |
Correct |
9 ms |
1112 KB |
Output is correct |
44 |
Correct |
47 ms |
1368 KB |
Output is correct |
45 |
Correct |
9 ms |
1368 KB |
Output is correct |
46 |
Correct |
47 ms |
1116 KB |
Output is correct |
47 |
Correct |
3 ms |
1112 KB |
Output is correct |
48 |
Correct |
26 ms |
1356 KB |
Output is correct |
49 |
Correct |
25 ms |
1116 KB |
Output is correct |
50 |
Correct |
48 ms |
1344 KB |
Output is correct |
51 |
Correct |
48 ms |
1368 KB |
Output is correct |
52 |
Correct |
114 ms |
4228 KB |
Output is correct |
53 |
Correct |
14 ms |
4024 KB |
Output is correct |
54 |
Correct |
119 ms |
4436 KB |
Output is correct |
55 |
Correct |
14 ms |
3932 KB |
Output is correct |
56 |
Correct |
113 ms |
4208 KB |
Output is correct |
57 |
Correct |
13 ms |
3932 KB |
Output is correct |
58 |
Correct |
114 ms |
4224 KB |
Output is correct |
59 |
Correct |
13 ms |
3928 KB |
Output is correct |
60 |
Correct |
119 ms |
4416 KB |
Output is correct |
61 |
Correct |
13 ms |
3932 KB |
Output is correct |
62 |
Correct |
115 ms |
4212 KB |
Output is correct |
63 |
Correct |
14 ms |
3932 KB |
Output is correct |
64 |
Correct |
121 ms |
4212 KB |
Output is correct |
65 |
Correct |
116 ms |
4188 KB |
Output is correct |
66 |
Correct |
13 ms |
3928 KB |
Output is correct |
67 |
Correct |
114 ms |
4212 KB |
Output is correct |
68 |
Correct |
13 ms |
3932 KB |
Output is correct |
69 |
Correct |
114 ms |
4228 KB |
Output is correct |
70 |
Correct |
14 ms |
3932 KB |
Output is correct |
71 |
Correct |
113 ms |
4208 KB |
Output is correct |
72 |
Correct |
14 ms |
3932 KB |
Output is correct |
73 |
Correct |
65 ms |
4164 KB |
Output is correct |
74 |
Correct |
712 ms |
11240 KB |
Output is correct |
75 |
Correct |
96 ms |
10852 KB |
Output is correct |
76 |
Correct |
706 ms |
11092 KB |
Output is correct |
77 |
Correct |
82 ms |
11116 KB |
Output is correct |
78 |
Correct |
705 ms |
11328 KB |
Output is correct |
79 |
Correct |
708 ms |
11088 KB |
Output is correct |
80 |
Correct |
74 ms |
10836 KB |
Output is correct |
81 |
Correct |
76 ms |
10844 KB |
Output is correct |
82 |
Correct |
716 ms |
11236 KB |
Output is correct |
83 |
Correct |
709 ms |
11324 KB |
Output is correct |
84 |
Correct |
89 ms |
10956 KB |
Output is correct |
85 |
Correct |
711 ms |
11088 KB |
Output is correct |
86 |
Correct |
69 ms |
10832 KB |
Output is correct |
87 |
Correct |
708 ms |
11216 KB |
Output is correct |
88 |
Correct |
70 ms |
10824 KB |
Output is correct |
89 |
Correct |
726 ms |
11100 KB |
Output is correct |
90 |
Correct |
83 ms |
10832 KB |
Output is correct |
91 |
Correct |
718 ms |
11092 KB |
Output is correct |
92 |
Correct |
86 ms |
11004 KB |
Output is correct |
93 |
Correct |
772 ms |
11324 KB |
Output is correct |
94 |
Correct |
63 ms |
10760 KB |
Output is correct |
95 |
Correct |
405 ms |
11048 KB |
Output is correct |
96 |
Correct |
409 ms |
11152 KB |
Output is correct |
97 |
Correct |
769 ms |
11348 KB |
Output is correct |
98 |
Correct |
750 ms |
11348 KB |
Output is correct |