#include <bits/stdc++.h>
using namespace std;
#define MAXN 600005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
multiset<pair<int, int>> e[MAXN];
bool v[MAXN];
int n, k, r = 0, r2 = 0;
bitset<MAXN> bs;
void dfs(int node){
v[node] = true;
if(!e[node].size()) return;
int nxt, cost;
tie(nxt, cost) = *e[node].begin();
r += cost;
if(!v[nxt]){
e[nxt].erase(e[nxt].find({node, -cost}));
e[node].clear();
dfs(nxt);
}
}
int main(){
cin >> n >> k;
FOR(i, 1, 2 * n){
int l, r, s;
cin >> l >> r >> s;
e[l].insert({n + r, s}), e[n + r].insert({l, -s});
}
queue<int> q;
FOR(i, 1, 2 * n) {
if(e[i].size() == 1) q.push(i);
if(e[i].size() == 0) return cout << "NO" << endl, 0;
}
while(q.size()){
int qi = q.front(); q.pop();
if(e[qi].size() == 0) return cout << "NO" << endl, 0;
int nxt, cost;
tie(nxt, cost) = *e[qi].begin();
r += cost;
e[qi].clear();
e[nxt].erase(e[nxt].find({qi, -cost}));
if(e[nxt].size() == 1) q.push(nxt);
}
vector<int> items;
if(r) items.push_back(abs(r));
FOR(i, 1, 2 * n){
if(!v[i] && e[i].size()){
r = 0;
e[i].erase(e[i].begin());
dfs(i);
if(r) items.push_back(abs(r));
}
}
r2 = accumulate(items.begin(), items.end(), 0);
bs[0] = 1;
for(int i : items) bs |= bs << i;
FOR(i, 0, r2){
if(bs[i] && abs(2 * i - r2) <= k) return cout << "YES" << endl, 0;
}
return cout << "NO" << endl, 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
29264 KB |
Output is correct |
2 |
Correct |
8 ms |
29264 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29264 KB |
Output is correct |
5 |
Correct |
6 ms |
29136 KB |
Output is correct |
6 |
Correct |
6 ms |
29264 KB |
Output is correct |
7 |
Correct |
7 ms |
29264 KB |
Output is correct |
8 |
Correct |
7 ms |
29264 KB |
Output is correct |
9 |
Correct |
7 ms |
29264 KB |
Output is correct |
10 |
Correct |
7 ms |
29304 KB |
Output is correct |
11 |
Correct |
7 ms |
29264 KB |
Output is correct |
12 |
Correct |
6 ms |
29140 KB |
Output is correct |
13 |
Correct |
6 ms |
29428 KB |
Output is correct |
14 |
Correct |
7 ms |
29264 KB |
Output is correct |
15 |
Correct |
9 ms |
29264 KB |
Output is correct |
16 |
Correct |
6 ms |
29264 KB |
Output is correct |
17 |
Correct |
7 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29264 KB |
Output is correct |
19 |
Correct |
6 ms |
29264 KB |
Output is correct |
20 |
Correct |
6 ms |
29140 KB |
Output is correct |
21 |
Correct |
6 ms |
29008 KB |
Output is correct |
22 |
Correct |
6 ms |
29432 KB |
Output is correct |
23 |
Correct |
6 ms |
29264 KB |
Output is correct |
24 |
Correct |
6 ms |
29264 KB |
Output is correct |
25 |
Correct |
6 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
29264 KB |
Output is correct |
2 |
Correct |
8 ms |
29264 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29264 KB |
Output is correct |
5 |
Correct |
6 ms |
29136 KB |
Output is correct |
6 |
Correct |
6 ms |
29264 KB |
Output is correct |
7 |
Correct |
7 ms |
29264 KB |
Output is correct |
8 |
Correct |
7 ms |
29264 KB |
Output is correct |
9 |
Correct |
7 ms |
29264 KB |
Output is correct |
10 |
Correct |
7 ms |
29304 KB |
Output is correct |
11 |
Correct |
7 ms |
29264 KB |
Output is correct |
12 |
Correct |
6 ms |
29140 KB |
Output is correct |
13 |
Correct |
6 ms |
29428 KB |
Output is correct |
14 |
Correct |
7 ms |
29264 KB |
Output is correct |
15 |
Correct |
9 ms |
29264 KB |
Output is correct |
16 |
Correct |
6 ms |
29264 KB |
Output is correct |
17 |
Correct |
7 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29264 KB |
Output is correct |
19 |
Correct |
6 ms |
29264 KB |
Output is correct |
20 |
Correct |
6 ms |
29140 KB |
Output is correct |
21 |
Correct |
6 ms |
29008 KB |
Output is correct |
22 |
Correct |
6 ms |
29432 KB |
Output is correct |
23 |
Correct |
6 ms |
29264 KB |
Output is correct |
24 |
Correct |
6 ms |
29264 KB |
Output is correct |
25 |
Correct |
6 ms |
29264 KB |
Output is correct |
26 |
Correct |
38 ms |
29692 KB |
Output is correct |
27 |
Correct |
19 ms |
29520 KB |
Output is correct |
28 |
Correct |
39 ms |
29520 KB |
Output is correct |
29 |
Correct |
13 ms |
29520 KB |
Output is correct |
30 |
Correct |
48 ms |
29520 KB |
Output is correct |
31 |
Correct |
13 ms |
29708 KB |
Output is correct |
32 |
Correct |
38 ms |
29520 KB |
Output is correct |
33 |
Correct |
12 ms |
29520 KB |
Output is correct |
34 |
Correct |
10 ms |
29520 KB |
Output is correct |
35 |
Correct |
12 ms |
29520 KB |
Output is correct |
36 |
Correct |
38 ms |
29688 KB |
Output is correct |
37 |
Correct |
12 ms |
29520 KB |
Output is correct |
38 |
Correct |
38 ms |
29520 KB |
Output is correct |
39 |
Correct |
13 ms |
29520 KB |
Output is correct |
40 |
Correct |
38 ms |
29520 KB |
Output is correct |
41 |
Correct |
13 ms |
29520 KB |
Output is correct |
42 |
Correct |
39 ms |
29520 KB |
Output is correct |
43 |
Correct |
12 ms |
29520 KB |
Output is correct |
44 |
Correct |
38 ms |
29716 KB |
Output is correct |
45 |
Correct |
12 ms |
29520 KB |
Output is correct |
46 |
Correct |
45 ms |
29520 KB |
Output is correct |
47 |
Correct |
9 ms |
29520 KB |
Output is correct |
48 |
Correct |
35 ms |
29520 KB |
Output is correct |
49 |
Correct |
26 ms |
29692 KB |
Output is correct |
50 |
Correct |
41 ms |
29688 KB |
Output is correct |
51 |
Correct |
39 ms |
29520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31056 KB |
Output is correct |
2 |
Correct |
21 ms |
31056 KB |
Output is correct |
3 |
Correct |
22 ms |
31056 KB |
Output is correct |
4 |
Correct |
20 ms |
31056 KB |
Output is correct |
5 |
Correct |
23 ms |
30936 KB |
Output is correct |
6 |
Correct |
23 ms |
31056 KB |
Output is correct |
7 |
Correct |
26 ms |
31056 KB |
Output is correct |
8 |
Correct |
21 ms |
31056 KB |
Output is correct |
9 |
Correct |
20 ms |
31060 KB |
Output is correct |
10 |
Correct |
19 ms |
31068 KB |
Output is correct |
11 |
Correct |
19 ms |
31056 KB |
Output is correct |
12 |
Correct |
21 ms |
31056 KB |
Output is correct |
13 |
Correct |
22 ms |
30904 KB |
Output is correct |
14 |
Correct |
21 ms |
31056 KB |
Output is correct |
15 |
Correct |
19 ms |
31056 KB |
Output is correct |
16 |
Correct |
19 ms |
31068 KB |
Output is correct |
17 |
Correct |
21 ms |
31056 KB |
Output is correct |
18 |
Correct |
24 ms |
31016 KB |
Output is correct |
19 |
Correct |
20 ms |
31056 KB |
Output is correct |
20 |
Correct |
19 ms |
31056 KB |
Output is correct |
21 |
Correct |
19 ms |
30988 KB |
Output is correct |
22 |
Correct |
25 ms |
31056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
29264 KB |
Output is correct |
2 |
Correct |
8 ms |
29264 KB |
Output is correct |
3 |
Correct |
9 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29264 KB |
Output is correct |
5 |
Correct |
6 ms |
29136 KB |
Output is correct |
6 |
Correct |
6 ms |
29264 KB |
Output is correct |
7 |
Correct |
7 ms |
29264 KB |
Output is correct |
8 |
Correct |
7 ms |
29264 KB |
Output is correct |
9 |
Correct |
7 ms |
29264 KB |
Output is correct |
10 |
Correct |
7 ms |
29304 KB |
Output is correct |
11 |
Correct |
7 ms |
29264 KB |
Output is correct |
12 |
Correct |
6 ms |
29140 KB |
Output is correct |
13 |
Correct |
6 ms |
29428 KB |
Output is correct |
14 |
Correct |
7 ms |
29264 KB |
Output is correct |
15 |
Correct |
9 ms |
29264 KB |
Output is correct |
16 |
Correct |
6 ms |
29264 KB |
Output is correct |
17 |
Correct |
7 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29264 KB |
Output is correct |
19 |
Correct |
6 ms |
29264 KB |
Output is correct |
20 |
Correct |
6 ms |
29140 KB |
Output is correct |
21 |
Correct |
6 ms |
29008 KB |
Output is correct |
22 |
Correct |
6 ms |
29432 KB |
Output is correct |
23 |
Correct |
6 ms |
29264 KB |
Output is correct |
24 |
Correct |
6 ms |
29264 KB |
Output is correct |
25 |
Correct |
6 ms |
29264 KB |
Output is correct |
26 |
Correct |
38 ms |
29692 KB |
Output is correct |
27 |
Correct |
19 ms |
29520 KB |
Output is correct |
28 |
Correct |
39 ms |
29520 KB |
Output is correct |
29 |
Correct |
13 ms |
29520 KB |
Output is correct |
30 |
Correct |
48 ms |
29520 KB |
Output is correct |
31 |
Correct |
13 ms |
29708 KB |
Output is correct |
32 |
Correct |
38 ms |
29520 KB |
Output is correct |
33 |
Correct |
12 ms |
29520 KB |
Output is correct |
34 |
Correct |
10 ms |
29520 KB |
Output is correct |
35 |
Correct |
12 ms |
29520 KB |
Output is correct |
36 |
Correct |
38 ms |
29688 KB |
Output is correct |
37 |
Correct |
12 ms |
29520 KB |
Output is correct |
38 |
Correct |
38 ms |
29520 KB |
Output is correct |
39 |
Correct |
13 ms |
29520 KB |
Output is correct |
40 |
Correct |
38 ms |
29520 KB |
Output is correct |
41 |
Correct |
13 ms |
29520 KB |
Output is correct |
42 |
Correct |
39 ms |
29520 KB |
Output is correct |
43 |
Correct |
12 ms |
29520 KB |
Output is correct |
44 |
Correct |
38 ms |
29716 KB |
Output is correct |
45 |
Correct |
12 ms |
29520 KB |
Output is correct |
46 |
Correct |
45 ms |
29520 KB |
Output is correct |
47 |
Correct |
9 ms |
29520 KB |
Output is correct |
48 |
Correct |
35 ms |
29520 KB |
Output is correct |
49 |
Correct |
26 ms |
29692 KB |
Output is correct |
50 |
Correct |
41 ms |
29688 KB |
Output is correct |
51 |
Correct |
39 ms |
29520 KB |
Output is correct |
52 |
Correct |
20 ms |
31056 KB |
Output is correct |
53 |
Correct |
21 ms |
31056 KB |
Output is correct |
54 |
Correct |
22 ms |
31056 KB |
Output is correct |
55 |
Correct |
20 ms |
31056 KB |
Output is correct |
56 |
Correct |
23 ms |
30936 KB |
Output is correct |
57 |
Correct |
23 ms |
31056 KB |
Output is correct |
58 |
Correct |
26 ms |
31056 KB |
Output is correct |
59 |
Correct |
21 ms |
31056 KB |
Output is correct |
60 |
Correct |
20 ms |
31060 KB |
Output is correct |
61 |
Correct |
19 ms |
31068 KB |
Output is correct |
62 |
Correct |
19 ms |
31056 KB |
Output is correct |
63 |
Correct |
21 ms |
31056 KB |
Output is correct |
64 |
Correct |
22 ms |
30904 KB |
Output is correct |
65 |
Correct |
21 ms |
31056 KB |
Output is correct |
66 |
Correct |
19 ms |
31056 KB |
Output is correct |
67 |
Correct |
19 ms |
31068 KB |
Output is correct |
68 |
Correct |
21 ms |
31056 KB |
Output is correct |
69 |
Correct |
24 ms |
31016 KB |
Output is correct |
70 |
Correct |
20 ms |
31056 KB |
Output is correct |
71 |
Correct |
19 ms |
31056 KB |
Output is correct |
72 |
Correct |
19 ms |
30988 KB |
Output is correct |
73 |
Correct |
25 ms |
31056 KB |
Output is correct |
74 |
Correct |
523 ms |
35144 KB |
Output is correct |
75 |
Correct |
84 ms |
34888 KB |
Output is correct |
76 |
Correct |
524 ms |
35244 KB |
Output is correct |
77 |
Correct |
74 ms |
34892 KB |
Output is correct |
78 |
Correct |
535 ms |
35320 KB |
Output is correct |
79 |
Correct |
501 ms |
35152 KB |
Output is correct |
80 |
Correct |
72 ms |
34896 KB |
Output is correct |
81 |
Correct |
67 ms |
34944 KB |
Output is correct |
82 |
Correct |
512 ms |
35144 KB |
Output is correct |
83 |
Correct |
517 ms |
35208 KB |
Output is correct |
84 |
Correct |
92 ms |
34888 KB |
Output is correct |
85 |
Correct |
509 ms |
35244 KB |
Output is correct |
86 |
Correct |
64 ms |
34888 KB |
Output is correct |
87 |
Correct |
507 ms |
35148 KB |
Output is correct |
88 |
Correct |
73 ms |
34944 KB |
Output is correct |
89 |
Correct |
516 ms |
35144 KB |
Output is correct |
90 |
Correct |
68 ms |
34888 KB |
Output is correct |
91 |
Correct |
529 ms |
35244 KB |
Output is correct |
92 |
Correct |
70 ms |
34888 KB |
Output is correct |
93 |
Correct |
584 ms |
35244 KB |
Output is correct |
94 |
Correct |
53 ms |
34632 KB |
Output is correct |
95 |
Correct |
333 ms |
35008 KB |
Output is correct |
96 |
Correct |
296 ms |
35144 KB |
Output is correct |
97 |
Correct |
528 ms |
35148 KB |
Output is correct |
98 |
Correct |
541 ms |
35244 KB |
Output is correct |