#include <bits/stdc++.h>
using std::pair;
using std::vector;
constexpr int MAX_S = 20;
constexpr int MAX_N = 30000;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, K;
std::cin >> N >> K;
const int V = 2 * N;
vector<int> L(V), R(V), S(V);
vector<vector<pair<int, int>>> graph(V);
for (int i = 0; i < V; ++i) {
std::cin >> L[i] >> R[i] >> S[i];
L[i] -= 1;
R[i] -= 1;
R[i] += N;
graph[L[i]].emplace_back(R[i], i);
graph[R[i]].emplace_back(L[i], i);
}
vector<int> deg(V);
std::queue<int> que;
for (int i = 0; i < V; ++i) {
deg[i] = graph[i].size();
if (deg[i] <= 1) {
que.push(i);
}
}
int fixed = 0;
vector<char> done(V);
while (!que.empty()) {
const int u = que.front();
que.pop();
bool found = false;
for (const auto& [v, e] : graph[u]) {
if (!done[v]) {
deg[v] -= 1;
if (deg[v] == 1) {
que.push(v);
}
fixed += u < N ? S[e] : -S[e];
found = true;
break;
}
}
if (!found) {
std::cout << "NO\n";
return 0;
}
done[u] = true;
}
const int W = MAX_S * N + 1;
vector<int> count(W);
for (int i = 0; i < V; ++i) {
if (done[i]) {
continue;
}
int u = i, p = -1, sum = 0;
vector<int> cycle;
while (true) {
cycle.push_back(u);
for (const auto& [v, e] : graph[u]) {
if (!done[v] and e != p) {
sum += u < N ? S[e] : -S[e];
p = e;
u = v;
break;
}
}
if (u == i) {
break;
}
}
for (const int u : cycle) {
done[u] = true;
}
if (sum < 0) {
sum = -sum;
}
fixed -= sum;
count[sum] += 1;
}
std::bitset<MAX_S * MAX_N + 1> dp;
dp.set(0);
for (int i = 0; i < W; ++i) {
if (count[i] == 0) {
continue;
}
if (count[i] >= 3) {
const int t = (count[i] - 1) / 2;
count[i] -= 2 * t;
count[2 * i] += t;
}
while (count[i]--) {
dp |= dp << i;
}
}
for (int i = 0; i < W; ++i) {
if (std::abs(fixed + 2 * i) <= K and dp.test(i)) {
std::cout << "YES\n";
return 0;
}
}
std::cout << "NO\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
536 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
536 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
536 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
532 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
540 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
532 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
536 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
536 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
536 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
532 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
540 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
532 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
928 KB |
Output is correct |
27 |
Correct |
8 ms |
920 KB |
Output is correct |
28 |
Correct |
6 ms |
980 KB |
Output is correct |
29 |
Correct |
7 ms |
980 KB |
Output is correct |
30 |
Correct |
5 ms |
980 KB |
Output is correct |
31 |
Correct |
6 ms |
1020 KB |
Output is correct |
32 |
Correct |
4 ms |
980 KB |
Output is correct |
33 |
Correct |
7 ms |
936 KB |
Output is correct |
34 |
Correct |
3 ms |
980 KB |
Output is correct |
35 |
Correct |
7 ms |
1016 KB |
Output is correct |
36 |
Correct |
5 ms |
980 KB |
Output is correct |
37 |
Correct |
7 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
9 ms |
980 KB |
Output is correct |
40 |
Correct |
5 ms |
980 KB |
Output is correct |
41 |
Correct |
9 ms |
980 KB |
Output is correct |
42 |
Correct |
4 ms |
1024 KB |
Output is correct |
43 |
Correct |
8 ms |
936 KB |
Output is correct |
44 |
Correct |
5 ms |
936 KB |
Output is correct |
45 |
Correct |
7 ms |
980 KB |
Output is correct |
46 |
Correct |
4 ms |
980 KB |
Output is correct |
47 |
Correct |
2 ms |
852 KB |
Output is correct |
48 |
Correct |
4 ms |
980 KB |
Output is correct |
49 |
Correct |
4 ms |
980 KB |
Output is correct |
50 |
Correct |
3 ms |
980 KB |
Output is correct |
51 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
2980 KB |
Output is correct |
2 |
Correct |
9 ms |
2260 KB |
Output is correct |
3 |
Correct |
63 ms |
2900 KB |
Output is correct |
4 |
Correct |
8 ms |
2292 KB |
Output is correct |
5 |
Correct |
53 ms |
2992 KB |
Output is correct |
6 |
Correct |
7 ms |
2296 KB |
Output is correct |
7 |
Correct |
59 ms |
2980 KB |
Output is correct |
8 |
Correct |
8 ms |
2260 KB |
Output is correct |
9 |
Correct |
55 ms |
2948 KB |
Output is correct |
10 |
Correct |
9 ms |
2304 KB |
Output is correct |
11 |
Correct |
60 ms |
2980 KB |
Output is correct |
12 |
Correct |
7 ms |
2260 KB |
Output is correct |
13 |
Correct |
54 ms |
2980 KB |
Output is correct |
14 |
Correct |
56 ms |
2976 KB |
Output is correct |
15 |
Correct |
8 ms |
2340 KB |
Output is correct |
16 |
Correct |
56 ms |
2900 KB |
Output is correct |
17 |
Correct |
9 ms |
2260 KB |
Output is correct |
18 |
Correct |
59 ms |
2900 KB |
Output is correct |
19 |
Correct |
8 ms |
2260 KB |
Output is correct |
20 |
Correct |
54 ms |
2952 KB |
Output is correct |
21 |
Correct |
8 ms |
2276 KB |
Output is correct |
22 |
Correct |
34 ms |
3140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
536 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
536 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
536 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
532 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
540 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
532 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
928 KB |
Output is correct |
27 |
Correct |
8 ms |
920 KB |
Output is correct |
28 |
Correct |
6 ms |
980 KB |
Output is correct |
29 |
Correct |
7 ms |
980 KB |
Output is correct |
30 |
Correct |
5 ms |
980 KB |
Output is correct |
31 |
Correct |
6 ms |
1020 KB |
Output is correct |
32 |
Correct |
4 ms |
980 KB |
Output is correct |
33 |
Correct |
7 ms |
936 KB |
Output is correct |
34 |
Correct |
3 ms |
980 KB |
Output is correct |
35 |
Correct |
7 ms |
1016 KB |
Output is correct |
36 |
Correct |
5 ms |
980 KB |
Output is correct |
37 |
Correct |
7 ms |
980 KB |
Output is correct |
38 |
Correct |
4 ms |
980 KB |
Output is correct |
39 |
Correct |
9 ms |
980 KB |
Output is correct |
40 |
Correct |
5 ms |
980 KB |
Output is correct |
41 |
Correct |
9 ms |
980 KB |
Output is correct |
42 |
Correct |
4 ms |
1024 KB |
Output is correct |
43 |
Correct |
8 ms |
936 KB |
Output is correct |
44 |
Correct |
5 ms |
936 KB |
Output is correct |
45 |
Correct |
7 ms |
980 KB |
Output is correct |
46 |
Correct |
4 ms |
980 KB |
Output is correct |
47 |
Correct |
2 ms |
852 KB |
Output is correct |
48 |
Correct |
4 ms |
980 KB |
Output is correct |
49 |
Correct |
4 ms |
980 KB |
Output is correct |
50 |
Correct |
3 ms |
980 KB |
Output is correct |
51 |
Correct |
3 ms |
980 KB |
Output is correct |
52 |
Correct |
59 ms |
2980 KB |
Output is correct |
53 |
Correct |
9 ms |
2260 KB |
Output is correct |
54 |
Correct |
63 ms |
2900 KB |
Output is correct |
55 |
Correct |
8 ms |
2292 KB |
Output is correct |
56 |
Correct |
53 ms |
2992 KB |
Output is correct |
57 |
Correct |
7 ms |
2296 KB |
Output is correct |
58 |
Correct |
59 ms |
2980 KB |
Output is correct |
59 |
Correct |
8 ms |
2260 KB |
Output is correct |
60 |
Correct |
55 ms |
2948 KB |
Output is correct |
61 |
Correct |
9 ms |
2304 KB |
Output is correct |
62 |
Correct |
60 ms |
2980 KB |
Output is correct |
63 |
Correct |
7 ms |
2260 KB |
Output is correct |
64 |
Correct |
54 ms |
2980 KB |
Output is correct |
65 |
Correct |
56 ms |
2976 KB |
Output is correct |
66 |
Correct |
8 ms |
2340 KB |
Output is correct |
67 |
Correct |
56 ms |
2900 KB |
Output is correct |
68 |
Correct |
9 ms |
2260 KB |
Output is correct |
69 |
Correct |
59 ms |
2900 KB |
Output is correct |
70 |
Correct |
8 ms |
2260 KB |
Output is correct |
71 |
Correct |
54 ms |
2952 KB |
Output is correct |
72 |
Correct |
8 ms |
2276 KB |
Output is correct |
73 |
Correct |
34 ms |
3140 KB |
Output is correct |
74 |
Correct |
36 ms |
7968 KB |
Output is correct |
75 |
Correct |
47 ms |
7968 KB |
Output is correct |
76 |
Correct |
48 ms |
7992 KB |
Output is correct |
77 |
Correct |
51 ms |
7940 KB |
Output is correct |
78 |
Correct |
37 ms |
7920 KB |
Output is correct |
79 |
Correct |
41 ms |
7976 KB |
Output is correct |
80 |
Correct |
45 ms |
7888 KB |
Output is correct |
81 |
Correct |
28 ms |
7952 KB |
Output is correct |
82 |
Correct |
26 ms |
7868 KB |
Output is correct |
83 |
Correct |
47 ms |
7972 KB |
Output is correct |
84 |
Correct |
52 ms |
7972 KB |
Output is correct |
85 |
Correct |
37 ms |
7864 KB |
Output is correct |
86 |
Correct |
47 ms |
7976 KB |
Output is correct |
87 |
Correct |
43 ms |
7888 KB |
Output is correct |
88 |
Correct |
58 ms |
7984 KB |
Output is correct |
89 |
Correct |
51 ms |
7880 KB |
Output is correct |
90 |
Correct |
50 ms |
7872 KB |
Output is correct |
91 |
Correct |
37 ms |
7880 KB |
Output is correct |
92 |
Correct |
44 ms |
7920 KB |
Output is correct |
93 |
Correct |
38 ms |
7992 KB |
Output is correct |
94 |
Correct |
27 ms |
5840 KB |
Output is correct |
95 |
Correct |
43 ms |
8268 KB |
Output is correct |
96 |
Correct |
43 ms |
8372 KB |
Output is correct |
97 |
Correct |
28 ms |
7892 KB |
Output is correct |
98 |
Correct |
30 ms |
7900 KB |
Output is correct |