#include <bits/stdc++.h>
using namespace std;
#define MAXN 60001
#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 |
2 ms |
3152 KB |
Output is correct |
2 |
Correct |
2 ms |
3152 KB |
Output is correct |
3 |
Correct |
2 ms |
3320 KB |
Output is correct |
4 |
Correct |
1 ms |
3152 KB |
Output is correct |
5 |
Correct |
2 ms |
3152 KB |
Output is correct |
6 |
Correct |
1 ms |
3152 KB |
Output is correct |
7 |
Correct |
2 ms |
3100 KB |
Output is correct |
8 |
Correct |
2 ms |
3152 KB |
Output is correct |
9 |
Correct |
2 ms |
3152 KB |
Output is correct |
10 |
Correct |
2 ms |
3152 KB |
Output is correct |
11 |
Correct |
1 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3152 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3152 KB |
Output is correct |
15 |
Correct |
1 ms |
3152 KB |
Output is correct |
16 |
Correct |
1 ms |
3152 KB |
Output is correct |
17 |
Correct |
2 ms |
3152 KB |
Output is correct |
18 |
Correct |
1 ms |
3152 KB |
Output is correct |
19 |
Correct |
2 ms |
3152 KB |
Output is correct |
20 |
Correct |
1 ms |
3152 KB |
Output is correct |
21 |
Correct |
1 ms |
3152 KB |
Output is correct |
22 |
Correct |
2 ms |
3204 KB |
Output is correct |
23 |
Correct |
1 ms |
3152 KB |
Output is correct |
24 |
Correct |
2 ms |
3152 KB |
Output is correct |
25 |
Correct |
2 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3152 KB |
Output is correct |
2 |
Correct |
2 ms |
3152 KB |
Output is correct |
3 |
Correct |
2 ms |
3320 KB |
Output is correct |
4 |
Correct |
1 ms |
3152 KB |
Output is correct |
5 |
Correct |
2 ms |
3152 KB |
Output is correct |
6 |
Correct |
1 ms |
3152 KB |
Output is correct |
7 |
Correct |
2 ms |
3100 KB |
Output is correct |
8 |
Correct |
2 ms |
3152 KB |
Output is correct |
9 |
Correct |
2 ms |
3152 KB |
Output is correct |
10 |
Correct |
2 ms |
3152 KB |
Output is correct |
11 |
Correct |
1 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3152 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3152 KB |
Output is correct |
15 |
Correct |
1 ms |
3152 KB |
Output is correct |
16 |
Correct |
1 ms |
3152 KB |
Output is correct |
17 |
Correct |
2 ms |
3152 KB |
Output is correct |
18 |
Correct |
1 ms |
3152 KB |
Output is correct |
19 |
Correct |
2 ms |
3152 KB |
Output is correct |
20 |
Correct |
1 ms |
3152 KB |
Output is correct |
21 |
Correct |
1 ms |
3152 KB |
Output is correct |
22 |
Correct |
2 ms |
3204 KB |
Output is correct |
23 |
Correct |
1 ms |
3152 KB |
Output is correct |
24 |
Correct |
2 ms |
3152 KB |
Output is correct |
25 |
Correct |
2 ms |
3152 KB |
Output is correct |
26 |
Correct |
6 ms |
3664 KB |
Output is correct |
27 |
Correct |
4 ms |
3664 KB |
Output is correct |
28 |
Correct |
6 ms |
3564 KB |
Output is correct |
29 |
Correct |
5 ms |
3408 KB |
Output is correct |
30 |
Correct |
7 ms |
3664 KB |
Output is correct |
31 |
Correct |
4 ms |
3664 KB |
Output is correct |
32 |
Correct |
6 ms |
3664 KB |
Output is correct |
33 |
Correct |
5 ms |
3408 KB |
Output is correct |
34 |
Correct |
4 ms |
3408 KB |
Output is correct |
35 |
Correct |
5 ms |
3664 KB |
Output is correct |
36 |
Correct |
9 ms |
3676 KB |
Output is correct |
37 |
Correct |
4 ms |
3408 KB |
Output is correct |
38 |
Correct |
7 ms |
3664 KB |
Output is correct |
39 |
Correct |
4 ms |
3664 KB |
Output is correct |
40 |
Correct |
7 ms |
3680 KB |
Output is correct |
41 |
Correct |
5 ms |
3836 KB |
Output is correct |
42 |
Correct |
6 ms |
3664 KB |
Output is correct |
43 |
Correct |
6 ms |
3408 KB |
Output is correct |
44 |
Correct |
6 ms |
3664 KB |
Output is correct |
45 |
Correct |
5 ms |
3408 KB |
Output is correct |
46 |
Correct |
6 ms |
3664 KB |
Output is correct |
47 |
Correct |
5 ms |
3408 KB |
Output is correct |
48 |
Correct |
6 ms |
3664 KB |
Output is correct |
49 |
Correct |
5 ms |
3676 KB |
Output is correct |
50 |
Correct |
7 ms |
3664 KB |
Output is correct |
51 |
Correct |
7 ms |
3664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4944 KB |
Output is correct |
2 |
Correct |
14 ms |
4944 KB |
Output is correct |
3 |
Correct |
14 ms |
5180 KB |
Output is correct |
4 |
Correct |
13 ms |
4944 KB |
Output is correct |
5 |
Correct |
13 ms |
5180 KB |
Output is correct |
6 |
Correct |
15 ms |
4944 KB |
Output is correct |
7 |
Correct |
14 ms |
4944 KB |
Output is correct |
8 |
Correct |
19 ms |
4944 KB |
Output is correct |
9 |
Correct |
15 ms |
4944 KB |
Output is correct |
10 |
Correct |
14 ms |
4944 KB |
Output is correct |
11 |
Correct |
14 ms |
4944 KB |
Output is correct |
12 |
Correct |
16 ms |
4944 KB |
Output is correct |
13 |
Correct |
13 ms |
5032 KB |
Output is correct |
14 |
Correct |
14 ms |
5116 KB |
Output is correct |
15 |
Correct |
14 ms |
5112 KB |
Output is correct |
16 |
Correct |
14 ms |
4944 KB |
Output is correct |
17 |
Correct |
13 ms |
4944 KB |
Output is correct |
18 |
Correct |
19 ms |
4944 KB |
Output is correct |
19 |
Correct |
13 ms |
4944 KB |
Output is correct |
20 |
Correct |
15 ms |
4944 KB |
Output is correct |
21 |
Correct |
14 ms |
4944 KB |
Output is correct |
22 |
Correct |
23 ms |
5040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3152 KB |
Output is correct |
2 |
Correct |
2 ms |
3152 KB |
Output is correct |
3 |
Correct |
2 ms |
3320 KB |
Output is correct |
4 |
Correct |
1 ms |
3152 KB |
Output is correct |
5 |
Correct |
2 ms |
3152 KB |
Output is correct |
6 |
Correct |
1 ms |
3152 KB |
Output is correct |
7 |
Correct |
2 ms |
3100 KB |
Output is correct |
8 |
Correct |
2 ms |
3152 KB |
Output is correct |
9 |
Correct |
2 ms |
3152 KB |
Output is correct |
10 |
Correct |
2 ms |
3152 KB |
Output is correct |
11 |
Correct |
1 ms |
3152 KB |
Output is correct |
12 |
Correct |
2 ms |
3152 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3152 KB |
Output is correct |
15 |
Correct |
1 ms |
3152 KB |
Output is correct |
16 |
Correct |
1 ms |
3152 KB |
Output is correct |
17 |
Correct |
2 ms |
3152 KB |
Output is correct |
18 |
Correct |
1 ms |
3152 KB |
Output is correct |
19 |
Correct |
2 ms |
3152 KB |
Output is correct |
20 |
Correct |
1 ms |
3152 KB |
Output is correct |
21 |
Correct |
1 ms |
3152 KB |
Output is correct |
22 |
Correct |
2 ms |
3204 KB |
Output is correct |
23 |
Correct |
1 ms |
3152 KB |
Output is correct |
24 |
Correct |
2 ms |
3152 KB |
Output is correct |
25 |
Correct |
2 ms |
3152 KB |
Output is correct |
26 |
Correct |
6 ms |
3664 KB |
Output is correct |
27 |
Correct |
4 ms |
3664 KB |
Output is correct |
28 |
Correct |
6 ms |
3564 KB |
Output is correct |
29 |
Correct |
5 ms |
3408 KB |
Output is correct |
30 |
Correct |
7 ms |
3664 KB |
Output is correct |
31 |
Correct |
4 ms |
3664 KB |
Output is correct |
32 |
Correct |
6 ms |
3664 KB |
Output is correct |
33 |
Correct |
5 ms |
3408 KB |
Output is correct |
34 |
Correct |
4 ms |
3408 KB |
Output is correct |
35 |
Correct |
5 ms |
3664 KB |
Output is correct |
36 |
Correct |
9 ms |
3676 KB |
Output is correct |
37 |
Correct |
4 ms |
3408 KB |
Output is correct |
38 |
Correct |
7 ms |
3664 KB |
Output is correct |
39 |
Correct |
4 ms |
3664 KB |
Output is correct |
40 |
Correct |
7 ms |
3680 KB |
Output is correct |
41 |
Correct |
5 ms |
3836 KB |
Output is correct |
42 |
Correct |
6 ms |
3664 KB |
Output is correct |
43 |
Correct |
6 ms |
3408 KB |
Output is correct |
44 |
Correct |
6 ms |
3664 KB |
Output is correct |
45 |
Correct |
5 ms |
3408 KB |
Output is correct |
46 |
Correct |
6 ms |
3664 KB |
Output is correct |
47 |
Correct |
5 ms |
3408 KB |
Output is correct |
48 |
Correct |
6 ms |
3664 KB |
Output is correct |
49 |
Correct |
5 ms |
3676 KB |
Output is correct |
50 |
Correct |
7 ms |
3664 KB |
Output is correct |
51 |
Correct |
7 ms |
3664 KB |
Output is correct |
52 |
Correct |
13 ms |
4944 KB |
Output is correct |
53 |
Correct |
14 ms |
4944 KB |
Output is correct |
54 |
Correct |
14 ms |
5180 KB |
Output is correct |
55 |
Correct |
13 ms |
4944 KB |
Output is correct |
56 |
Correct |
13 ms |
5180 KB |
Output is correct |
57 |
Correct |
15 ms |
4944 KB |
Output is correct |
58 |
Correct |
14 ms |
4944 KB |
Output is correct |
59 |
Correct |
19 ms |
4944 KB |
Output is correct |
60 |
Correct |
15 ms |
4944 KB |
Output is correct |
61 |
Correct |
14 ms |
4944 KB |
Output is correct |
62 |
Correct |
14 ms |
4944 KB |
Output is correct |
63 |
Correct |
16 ms |
4944 KB |
Output is correct |
64 |
Correct |
13 ms |
5032 KB |
Output is correct |
65 |
Correct |
14 ms |
5116 KB |
Output is correct |
66 |
Correct |
14 ms |
5112 KB |
Output is correct |
67 |
Correct |
14 ms |
4944 KB |
Output is correct |
68 |
Correct |
13 ms |
4944 KB |
Output is correct |
69 |
Correct |
19 ms |
4944 KB |
Output is correct |
70 |
Correct |
13 ms |
4944 KB |
Output is correct |
71 |
Correct |
15 ms |
4944 KB |
Output is correct |
72 |
Correct |
14 ms |
4944 KB |
Output is correct |
73 |
Correct |
23 ms |
5040 KB |
Output is correct |
74 |
Correct |
104 ms |
9092 KB |
Output is correct |
75 |
Correct |
57 ms |
8836 KB |
Output is correct |
76 |
Correct |
101 ms |
9044 KB |
Output is correct |
77 |
Correct |
54 ms |
8776 KB |
Output is correct |
78 |
Incorrect |
84 ms |
9076 KB |
Output isn't correct |
79 |
Halted |
0 ms |
0 KB |
- |