#include <bits/stdc++.h>
using namespace std;
multiset<pair<int, int>> graph[60001];
bool visited[60001];
bitset<600001> possible;
int tot = 0, sm = 0;
void dfs(int node) {
visited[node] = true;
if (!graph[node].size()) return;
int nxt, cost;
tie(nxt, cost) = *graph[node].begin();
tot += cost;
if (!visited[nxt]) {
graph[nxt].erase(graph[nxt].find({node, -cost}));
graph[node].clear();
dfs(nxt);
}
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= 2 * n; i++) {
int l, r, s;
scanf("%d %d %d", &l, &r, &s);
graph[l].insert({n + r, s});
graph[n + r].insert({l, -s});
}
queue<int> q;
for (int i = 1; i <= 2 * n; i++) {
if (graph[i].size() == 1) q.push(i);
if (graph[i].size() == 0) return printf("NO\n"), 0;
}
while (q.size()) {
int curr = q.front();
q.pop();
if (graph[curr].size() == 0) return printf("NO\n"), 0;
int nxt, cost;
tie(nxt, cost) = *graph[curr].begin();
tot += cost;
graph[curr].clear();
graph[nxt].erase(graph[nxt].find({curr, -cost}));
if (graph[nxt].size() == 1) q.push(nxt);
}
vector<int> items;
if (tot) items.push_back(abs(tot));
for (int i = 1; i <= 2 * n; i++)
if (!visited[i] && graph[i].size()) {
tot = 0;
graph[i].erase(graph[i].begin());
dfs(i);
if (tot) items.push_back(abs(tot));
}
sm = accumulate(items.begin(), items.end(), 0);
possible[0] = 1;
for (int i : items) possible |= possible << i;
for (int i = 0; i <= sm; i++)
if (possible[i] && abs(2 * i - sm) <= k) return printf("YES\n"), 0;
printf("NO\n");
return 0;
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
tug.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d %d %d", &l, &r, &s);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
3 ms |
3416 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
3 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3412 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
2 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3420 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
3 ms |
3416 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
3 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3412 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
2 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3420 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
41 ms |
3676 KB |
Output is correct |
27 |
Correct |
7 ms |
3676 KB |
Output is correct |
28 |
Correct |
28 ms |
3912 KB |
Output is correct |
29 |
Correct |
6 ms |
3676 KB |
Output is correct |
30 |
Correct |
32 ms |
3676 KB |
Output is correct |
31 |
Correct |
6 ms |
3672 KB |
Output is correct |
32 |
Correct |
28 ms |
3676 KB |
Output is correct |
33 |
Correct |
7 ms |
3676 KB |
Output is correct |
34 |
Correct |
5 ms |
3812 KB |
Output is correct |
35 |
Correct |
7 ms |
3676 KB |
Output is correct |
36 |
Correct |
27 ms |
3912 KB |
Output is correct |
37 |
Correct |
6 ms |
3676 KB |
Output is correct |
38 |
Correct |
28 ms |
3844 KB |
Output is correct |
39 |
Correct |
7 ms |
3676 KB |
Output is correct |
40 |
Correct |
29 ms |
3676 KB |
Output is correct |
41 |
Correct |
7 ms |
3676 KB |
Output is correct |
42 |
Correct |
28 ms |
3896 KB |
Output is correct |
43 |
Correct |
6 ms |
3676 KB |
Output is correct |
44 |
Correct |
28 ms |
3676 KB |
Output is correct |
45 |
Correct |
7 ms |
3676 KB |
Output is correct |
46 |
Correct |
28 ms |
3912 KB |
Output is correct |
47 |
Correct |
3 ms |
3676 KB |
Output is correct |
48 |
Correct |
16 ms |
3836 KB |
Output is correct |
49 |
Correct |
16 ms |
3676 KB |
Output is correct |
50 |
Correct |
29 ms |
3916 KB |
Output is correct |
51 |
Correct |
30 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5468 KB |
Output is correct |
2 |
Correct |
11 ms |
5464 KB |
Output is correct |
3 |
Correct |
8 ms |
5468 KB |
Output is correct |
4 |
Correct |
9 ms |
5468 KB |
Output is correct |
5 |
Correct |
9 ms |
5440 KB |
Output is correct |
6 |
Correct |
8 ms |
5444 KB |
Output is correct |
7 |
Correct |
8 ms |
5536 KB |
Output is correct |
8 |
Correct |
8 ms |
5464 KB |
Output is correct |
9 |
Correct |
9 ms |
5468 KB |
Output is correct |
10 |
Correct |
9 ms |
5512 KB |
Output is correct |
11 |
Correct |
8 ms |
5464 KB |
Output is correct |
12 |
Correct |
8 ms |
5468 KB |
Output is correct |
13 |
Correct |
13 ms |
5464 KB |
Output is correct |
14 |
Correct |
8 ms |
5468 KB |
Output is correct |
15 |
Correct |
8 ms |
5464 KB |
Output is correct |
16 |
Correct |
9 ms |
5468 KB |
Output is correct |
17 |
Correct |
10 ms |
5468 KB |
Output is correct |
18 |
Correct |
8 ms |
5464 KB |
Output is correct |
19 |
Correct |
8 ms |
5464 KB |
Output is correct |
20 |
Correct |
9 ms |
5464 KB |
Output is correct |
21 |
Correct |
8 ms |
5468 KB |
Output is correct |
22 |
Correct |
9 ms |
5464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
3 ms |
3416 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
3 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3408 KB |
Output is correct |
8 |
Correct |
2 ms |
3420 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
2 ms |
3420 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3412 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
2 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3420 KB |
Output is correct |
24 |
Correct |
1 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
41 ms |
3676 KB |
Output is correct |
27 |
Correct |
7 ms |
3676 KB |
Output is correct |
28 |
Correct |
28 ms |
3912 KB |
Output is correct |
29 |
Correct |
6 ms |
3676 KB |
Output is correct |
30 |
Correct |
32 ms |
3676 KB |
Output is correct |
31 |
Correct |
6 ms |
3672 KB |
Output is correct |
32 |
Correct |
28 ms |
3676 KB |
Output is correct |
33 |
Correct |
7 ms |
3676 KB |
Output is correct |
34 |
Correct |
5 ms |
3812 KB |
Output is correct |
35 |
Correct |
7 ms |
3676 KB |
Output is correct |
36 |
Correct |
27 ms |
3912 KB |
Output is correct |
37 |
Correct |
6 ms |
3676 KB |
Output is correct |
38 |
Correct |
28 ms |
3844 KB |
Output is correct |
39 |
Correct |
7 ms |
3676 KB |
Output is correct |
40 |
Correct |
29 ms |
3676 KB |
Output is correct |
41 |
Correct |
7 ms |
3676 KB |
Output is correct |
42 |
Correct |
28 ms |
3896 KB |
Output is correct |
43 |
Correct |
6 ms |
3676 KB |
Output is correct |
44 |
Correct |
28 ms |
3676 KB |
Output is correct |
45 |
Correct |
7 ms |
3676 KB |
Output is correct |
46 |
Correct |
28 ms |
3912 KB |
Output is correct |
47 |
Correct |
3 ms |
3676 KB |
Output is correct |
48 |
Correct |
16 ms |
3836 KB |
Output is correct |
49 |
Correct |
16 ms |
3676 KB |
Output is correct |
50 |
Correct |
29 ms |
3916 KB |
Output is correct |
51 |
Correct |
30 ms |
3676 KB |
Output is correct |
52 |
Correct |
10 ms |
5468 KB |
Output is correct |
53 |
Correct |
11 ms |
5464 KB |
Output is correct |
54 |
Correct |
8 ms |
5468 KB |
Output is correct |
55 |
Correct |
9 ms |
5468 KB |
Output is correct |
56 |
Correct |
9 ms |
5440 KB |
Output is correct |
57 |
Correct |
8 ms |
5444 KB |
Output is correct |
58 |
Correct |
8 ms |
5536 KB |
Output is correct |
59 |
Correct |
8 ms |
5464 KB |
Output is correct |
60 |
Correct |
9 ms |
5468 KB |
Output is correct |
61 |
Correct |
9 ms |
5512 KB |
Output is correct |
62 |
Correct |
8 ms |
5464 KB |
Output is correct |
63 |
Correct |
8 ms |
5468 KB |
Output is correct |
64 |
Correct |
13 ms |
5464 KB |
Output is correct |
65 |
Correct |
8 ms |
5468 KB |
Output is correct |
66 |
Correct |
8 ms |
5464 KB |
Output is correct |
67 |
Correct |
9 ms |
5468 KB |
Output is correct |
68 |
Correct |
10 ms |
5468 KB |
Output is correct |
69 |
Correct |
8 ms |
5464 KB |
Output is correct |
70 |
Correct |
8 ms |
5464 KB |
Output is correct |
71 |
Correct |
9 ms |
5464 KB |
Output is correct |
72 |
Correct |
8 ms |
5468 KB |
Output is correct |
73 |
Correct |
9 ms |
5464 KB |
Output is correct |
74 |
Correct |
417 ms |
10076 KB |
Output is correct |
75 |
Correct |
43 ms |
9736 KB |
Output is correct |
76 |
Correct |
405 ms |
10068 KB |
Output is correct |
77 |
Correct |
44 ms |
9820 KB |
Output is correct |
78 |
Correct |
444 ms |
10196 KB |
Output is correct |
79 |
Correct |
400 ms |
10204 KB |
Output is correct |
80 |
Correct |
44 ms |
9816 KB |
Output is correct |
81 |
Correct |
45 ms |
9880 KB |
Output is correct |
82 |
Correct |
425 ms |
10204 KB |
Output is correct |
83 |
Correct |
427 ms |
10068 KB |
Output is correct |
84 |
Correct |
42 ms |
9816 KB |
Output is correct |
85 |
Correct |
427 ms |
10204 KB |
Output is correct |
86 |
Correct |
49 ms |
10068 KB |
Output is correct |
87 |
Correct |
389 ms |
10064 KB |
Output is correct |
88 |
Correct |
41 ms |
9816 KB |
Output is correct |
89 |
Correct |
407 ms |
10196 KB |
Output is correct |
90 |
Correct |
52 ms |
9936 KB |
Output is correct |
91 |
Correct |
412 ms |
10200 KB |
Output is correct |
92 |
Correct |
42 ms |
9816 KB |
Output is correct |
93 |
Correct |
427 ms |
10324 KB |
Output is correct |
94 |
Correct |
24 ms |
9820 KB |
Output is correct |
95 |
Correct |
225 ms |
9996 KB |
Output is correct |
96 |
Correct |
216 ms |
10064 KB |
Output is correct |
97 |
Correct |
428 ms |
10180 KB |
Output is correct |
98 |
Correct |
449 ms |
10204 KB |
Output is correct |