#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
const int MOD = 1e9+7;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
const int MXN = 30005;
int n, k;
int L[2*MXN], R[2*MXN], S[2*MXN];
bool visited[2*MXN];
vi at_l[MXN], at_r[MXN];
int deg_l[MXN], deg_r[MXN];
int diff0 = 0;
void impossible(string id = "") {
//cout << "NO" << id << "\n";
cout << "NO\n";
exit(0);
}
int main() {
FASTIO();
cin >> n >> k;
for(int i = 0; i < 2*n; i++) {
cin >> L[i] >> R[i] >> S[i]; L[i]--, R[i]--;
at_l[L[i]].PB(i);
at_r[R[i]].PB(i);
deg_l[L[i]]++, deg_r[R[i]]++;
}
queue<pii> Q;
for(int i = 0; i < n; i++) {
if(at_l[i].size() == 1) {
int j = at_l[i].back();
if(visited[j]) impossible("g");
Q.push({j, 0});
visited[j] = true;
deg_l[i]--;
diff0 += S[j];
}
if(at_r[i].size() == 1) {
int j = at_r[i].back();
if(visited[j]) impossible("h");
Q.push({j, 1});
visited[j] = true;
deg_r[i]--;
diff0 -= S[j];
}
}
while(!Q.empty()) {
int i = Q.front().F;
int lr = Q.front().S;
Q.pop();
if(lr == 0) { // l
deg_r[R[i]]--;
if(deg_r[R[i]] == 1) {
while(!at_r[R[i]].empty() && visited[at_r[R[i]].back()])
at_r[R[i]].pop_back();
assert(!at_r[R[i]].empty());
int j = at_r[R[i]].back();
Q.push({j, 1});
visited[j] = true;
deg_r[R[j]]--;
diff0 -= S[j];
}
}
else { // r
deg_l[L[i]]--;
if(deg_l[L[i]] == 1) {
while(!at_l[L[i]].empty() && visited[at_l[L[i]].back()])
at_l[L[i]].pop_back();
assert(!at_l[L[i]].empty());
int j = at_l[L[i]].back();
Q.push({j, 0});
visited[j] = true;
deg_l[L[j]]--;
diff0 += S[j];
}
}
}
// printf("diff0: %d\nvisited\n", diff0);
// for(int j = 0; j < 2*n; j++) {
// printf("%d ", visited[j]);
// }
// printf("\n");
vector<int> v;
for(int i = 0; i < n; i++) {
// printf("i = %d\nvisited:\n", i);
// for(int j = 0; j < 2*n; j++) {
// printf("%d ", visited[j]);
// }
// printf("\n");
if(deg_l[i] > 2) impossible("a");
if(deg_r[i] > 2) impossible("b");
assert(deg_l[i] != 1);
assert(deg_r[i] != 1);
if(deg_l[i] == 2) {
while(!at_l[i].empty() && visited[at_l[i].back()])
at_l[i].pop_back();
int j = at_l[i].back();
int sum = 0;
diff0 += S[j];
sum -= S[j];
deg_l[i]--;
visited[j] = true;
//printf("push %d (%d)\n", j, 0);
Q.push({j, 0});
while(!Q.empty()) {
int a = Q.front().F;
int lr = Q.front().S;
Q.pop();
// printf("a = %d\nvisited:\n", a);
// for(int x = 0; x < 2*n; x++) {
// printf("%d ", visited[x]);
// }
// printf("\n");
if(lr == 0) { // l
deg_r[R[a]]--;
if(deg_r[R[a]] != 1) impossible("c");
while(!at_r[R[a]].empty() && visited[at_r[R[a]].back()])
at_r[R[a]].pop_back();
assert(!at_r[R[a]].empty());
int b = at_r[R[a]].back();
//printf("push %d (%d)\n", b, 1);
Q.push({b, 1});
visited[b] = true;
deg_r[R[b]]--;
diff0 -= S[b];
sum += S[b];
}
else { // r
deg_l[L[a]]--;
if(deg_l[L[a]] == 0 && L[a] == i) continue;
if(deg_l[L[a]] != 1) impossible("d");
while(!at_l[L[a]].empty() && visited[at_l[L[a]].back()])
at_l[L[a]].pop_back();
assert(!at_l[L[a]].empty());
int b = at_l[L[a]].back();
//printf("push %d (%d)\n", b, 0);
Q.push({b, 0});
visited[b] = true;
deg_l[L[b]]--;
diff0 += S[b];
sum -= S[b];
}
}
v.PB(2*sum);
}
if(deg_r[i] == 2) {
while(!at_r[i].empty() && visited[at_r[i].back()])
at_r[i].pop_back();
int j = at_r[i].back();
int sum = 0;
diff0 -= S[j];
sum += S[j];
deg_r[i]--;
visited[j] = true;
Q.push({j, 1});
while(!Q.empty()) {
int a = Q.front().F;
int lr = Q.front().S;
Q.pop();
if(lr == 0) { // l
deg_r[R[a]]--;
if(deg_r[R[a]] == 0 && R[a] == i) continue;
if(deg_r[R[a]] != 1) impossible("e");
while(!at_r[R[a]].empty() && visited[at_r[R[a]].back()])
at_r[R[a]].pop_back();
assert(!at_r[R[a]].empty());
int b = at_r[R[a]].back();
Q.push({b, 1});
visited[b] = true;
deg_r[R[b]]--;
diff0 -= S[b];
sum += S[b];
}
else { // r
deg_l[L[a]]--;
if(deg_l[L[a]] != 1) impossible("f");
while(!at_l[L[a]].empty() && visited[at_l[L[a]].back()])
at_l[L[a]].pop_back();
assert(!at_l[L[a]].empty());
int b = at_l[L[a]].back();
Q.push({b, 0});
visited[b] = true;
deg_l[L[b]]--;
diff0 += S[b];
sum -= S[b];
}
}
v.PB(2*sum);
}
}
// cerr << diff0 << "\n";
// for(int x : v) {
// cerr << x << " ";
// }
// cerr << "\n";
sort(all(v));
multiset<int> MS(all(v));
for(int x = *MS.begin(); x <= *MS.rbegin();) {
if(MS.count(x) > 1) {
MS.erase(MS.find(x));
MS.erase(MS.find(x));
MS.insert(2*x);
}
else {
x++;
}
}
bitset<40*MXN> poss;
poss[20*MXN+diff0] = 1;
for(int x : MS) {
if(x >= 0) poss = poss | (poss << x);
else poss = poss | (poss >> (-x));
}
bool ok = false;
for(int i = 0; i < 40*MXN; i++) {
// if(poss[i]) {
// cerr << i-20*MXN << " ";
// }
if(abs(i-20*MXN) <= k && poss[i]) {
ok = true;
}
}
// cerr << "\n";
cout << (ok ? "YES\n" : "NO\n");
return 0;
}
/*
3 0
1 1 1
2 1 1
2 2 1
3 2 1
3 3 1
3 3 1
nah this problem suuuuucks
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2132 KB |
Output is correct |
2 |
Correct |
3 ms |
2132 KB |
Output is correct |
3 |
Correct |
3 ms |
2132 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
3 ms |
2132 KB |
Output is correct |
6 |
Correct |
3 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
3 ms |
2132 KB |
Output is correct |
9 |
Correct |
3 ms |
2180 KB |
Output is correct |
10 |
Correct |
3 ms |
2132 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
3 ms |
2132 KB |
Output is correct |
13 |
Correct |
3 ms |
2184 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
2180 KB |
Output is correct |
17 |
Correct |
3 ms |
2180 KB |
Output is correct |
18 |
Correct |
3 ms |
2132 KB |
Output is correct |
19 |
Correct |
3 ms |
2132 KB |
Output is correct |
20 |
Correct |
3 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
3 ms |
2132 KB |
Output is correct |
23 |
Correct |
4 ms |
2184 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2132 KB |
Output is correct |
2 |
Correct |
3 ms |
2132 KB |
Output is correct |
3 |
Correct |
3 ms |
2132 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
3 ms |
2132 KB |
Output is correct |
6 |
Correct |
3 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
3 ms |
2132 KB |
Output is correct |
9 |
Correct |
3 ms |
2180 KB |
Output is correct |
10 |
Correct |
3 ms |
2132 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
3 ms |
2132 KB |
Output is correct |
13 |
Correct |
3 ms |
2184 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
2180 KB |
Output is correct |
17 |
Correct |
3 ms |
2180 KB |
Output is correct |
18 |
Correct |
3 ms |
2132 KB |
Output is correct |
19 |
Correct |
3 ms |
2132 KB |
Output is correct |
20 |
Correct |
3 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
3 ms |
2132 KB |
Output is correct |
23 |
Correct |
4 ms |
2184 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2180 KB |
Output is correct |
26 |
Correct |
25 ms |
2516 KB |
Output is correct |
27 |
Correct |
15 ms |
2456 KB |
Output is correct |
28 |
Correct |
27 ms |
2448 KB |
Output is correct |
29 |
Correct |
17 ms |
2456 KB |
Output is correct |
30 |
Correct |
29 ms |
2516 KB |
Output is correct |
31 |
Correct |
21 ms |
2448 KB |
Output is correct |
32 |
Correct |
26 ms |
2452 KB |
Output is correct |
33 |
Correct |
15 ms |
2388 KB |
Output is correct |
34 |
Correct |
6 ms |
2388 KB |
Output is correct |
35 |
Correct |
15 ms |
2456 KB |
Output is correct |
36 |
Correct |
26 ms |
2516 KB |
Output is correct |
37 |
Correct |
15 ms |
2456 KB |
Output is correct |
38 |
Correct |
25 ms |
2516 KB |
Output is correct |
39 |
Correct |
15 ms |
2436 KB |
Output is correct |
40 |
Correct |
28 ms |
2540 KB |
Output is correct |
41 |
Correct |
15 ms |
2456 KB |
Output is correct |
42 |
Correct |
34 ms |
2516 KB |
Output is correct |
43 |
Correct |
16 ms |
2452 KB |
Output is correct |
44 |
Correct |
26 ms |
2544 KB |
Output is correct |
45 |
Correct |
15 ms |
2456 KB |
Output is correct |
46 |
Correct |
25 ms |
2516 KB |
Output is correct |
47 |
Correct |
3 ms |
2388 KB |
Output is correct |
48 |
Correct |
14 ms |
2388 KB |
Output is correct |
49 |
Correct |
16 ms |
2452 KB |
Output is correct |
50 |
Correct |
33 ms |
2516 KB |
Output is correct |
51 |
Correct |
27 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
353 ms |
3888 KB |
Output is correct |
2 |
Correct |
9 ms |
3412 KB |
Output is correct |
3 |
Correct |
334 ms |
3876 KB |
Output is correct |
4 |
Correct |
9 ms |
3284 KB |
Output is correct |
5 |
Correct |
310 ms |
3796 KB |
Output is correct |
6 |
Correct |
8 ms |
3344 KB |
Output is correct |
7 |
Correct |
356 ms |
3880 KB |
Output is correct |
8 |
Correct |
9 ms |
3344 KB |
Output is correct |
9 |
Correct |
327 ms |
3880 KB |
Output is correct |
10 |
Correct |
9 ms |
3300 KB |
Output is correct |
11 |
Correct |
322 ms |
3880 KB |
Output is correct |
12 |
Correct |
8 ms |
3284 KB |
Output is correct |
13 |
Correct |
318 ms |
3880 KB |
Output is correct |
14 |
Correct |
320 ms |
3892 KB |
Output is correct |
15 |
Correct |
8 ms |
3284 KB |
Output is correct |
16 |
Correct |
325 ms |
3880 KB |
Output is correct |
17 |
Correct |
8 ms |
3284 KB |
Output is correct |
18 |
Correct |
341 ms |
3884 KB |
Output is correct |
19 |
Correct |
9 ms |
3284 KB |
Output is correct |
20 |
Correct |
328 ms |
3884 KB |
Output is correct |
21 |
Correct |
9 ms |
3324 KB |
Output is correct |
22 |
Correct |
74 ms |
3648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2132 KB |
Output is correct |
2 |
Correct |
3 ms |
2132 KB |
Output is correct |
3 |
Correct |
3 ms |
2132 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
3 ms |
2132 KB |
Output is correct |
6 |
Correct |
3 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
3 ms |
2132 KB |
Output is correct |
9 |
Correct |
3 ms |
2180 KB |
Output is correct |
10 |
Correct |
3 ms |
2132 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
3 ms |
2132 KB |
Output is correct |
13 |
Correct |
3 ms |
2184 KB |
Output is correct |
14 |
Correct |
3 ms |
2132 KB |
Output is correct |
15 |
Correct |
3 ms |
2132 KB |
Output is correct |
16 |
Correct |
3 ms |
2180 KB |
Output is correct |
17 |
Correct |
3 ms |
2180 KB |
Output is correct |
18 |
Correct |
3 ms |
2132 KB |
Output is correct |
19 |
Correct |
3 ms |
2132 KB |
Output is correct |
20 |
Correct |
3 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
3 ms |
2132 KB |
Output is correct |
23 |
Correct |
4 ms |
2184 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2180 KB |
Output is correct |
26 |
Correct |
25 ms |
2516 KB |
Output is correct |
27 |
Correct |
15 ms |
2456 KB |
Output is correct |
28 |
Correct |
27 ms |
2448 KB |
Output is correct |
29 |
Correct |
17 ms |
2456 KB |
Output is correct |
30 |
Correct |
29 ms |
2516 KB |
Output is correct |
31 |
Correct |
21 ms |
2448 KB |
Output is correct |
32 |
Correct |
26 ms |
2452 KB |
Output is correct |
33 |
Correct |
15 ms |
2388 KB |
Output is correct |
34 |
Correct |
6 ms |
2388 KB |
Output is correct |
35 |
Correct |
15 ms |
2456 KB |
Output is correct |
36 |
Correct |
26 ms |
2516 KB |
Output is correct |
37 |
Correct |
15 ms |
2456 KB |
Output is correct |
38 |
Correct |
25 ms |
2516 KB |
Output is correct |
39 |
Correct |
15 ms |
2436 KB |
Output is correct |
40 |
Correct |
28 ms |
2540 KB |
Output is correct |
41 |
Correct |
15 ms |
2456 KB |
Output is correct |
42 |
Correct |
34 ms |
2516 KB |
Output is correct |
43 |
Correct |
16 ms |
2452 KB |
Output is correct |
44 |
Correct |
26 ms |
2544 KB |
Output is correct |
45 |
Correct |
15 ms |
2456 KB |
Output is correct |
46 |
Correct |
25 ms |
2516 KB |
Output is correct |
47 |
Correct |
3 ms |
2388 KB |
Output is correct |
48 |
Correct |
14 ms |
2388 KB |
Output is correct |
49 |
Correct |
16 ms |
2452 KB |
Output is correct |
50 |
Correct |
33 ms |
2516 KB |
Output is correct |
51 |
Correct |
27 ms |
2516 KB |
Output is correct |
52 |
Correct |
353 ms |
3888 KB |
Output is correct |
53 |
Correct |
9 ms |
3412 KB |
Output is correct |
54 |
Correct |
334 ms |
3876 KB |
Output is correct |
55 |
Correct |
9 ms |
3284 KB |
Output is correct |
56 |
Correct |
310 ms |
3796 KB |
Output is correct |
57 |
Correct |
8 ms |
3344 KB |
Output is correct |
58 |
Correct |
356 ms |
3880 KB |
Output is correct |
59 |
Correct |
9 ms |
3344 KB |
Output is correct |
60 |
Correct |
327 ms |
3880 KB |
Output is correct |
61 |
Correct |
9 ms |
3300 KB |
Output is correct |
62 |
Correct |
322 ms |
3880 KB |
Output is correct |
63 |
Correct |
8 ms |
3284 KB |
Output is correct |
64 |
Correct |
318 ms |
3880 KB |
Output is correct |
65 |
Correct |
320 ms |
3892 KB |
Output is correct |
66 |
Correct |
8 ms |
3284 KB |
Output is correct |
67 |
Correct |
325 ms |
3880 KB |
Output is correct |
68 |
Correct |
8 ms |
3284 KB |
Output is correct |
69 |
Correct |
341 ms |
3884 KB |
Output is correct |
70 |
Correct |
9 ms |
3284 KB |
Output is correct |
71 |
Correct |
328 ms |
3884 KB |
Output is correct |
72 |
Correct |
9 ms |
3324 KB |
Output is correct |
73 |
Correct |
74 ms |
3648 KB |
Output is correct |
74 |
Correct |
445 ms |
7364 KB |
Output is correct |
75 |
Correct |
82 ms |
5896 KB |
Output is correct |
76 |
Correct |
410 ms |
7364 KB |
Output is correct |
77 |
Correct |
78 ms |
5964 KB |
Output is correct |
78 |
Correct |
422 ms |
7372 KB |
Output is correct |
79 |
Correct |
479 ms |
7368 KB |
Output is correct |
80 |
Correct |
74 ms |
5896 KB |
Output is correct |
81 |
Correct |
56 ms |
5824 KB |
Output is correct |
82 |
Correct |
935 ms |
7376 KB |
Output is correct |
83 |
Correct |
390 ms |
7448 KB |
Output is correct |
84 |
Correct |
76 ms |
5892 KB |
Output is correct |
85 |
Correct |
459 ms |
7368 KB |
Output is correct |
86 |
Correct |
80 ms |
5844 KB |
Output is correct |
87 |
Correct |
413 ms |
7372 KB |
Output is correct |
88 |
Correct |
88 ms |
5892 KB |
Output is correct |
89 |
Correct |
413 ms |
7380 KB |
Output is correct |
90 |
Correct |
100 ms |
5892 KB |
Output is correct |
91 |
Correct |
454 ms |
7376 KB |
Output is correct |
92 |
Correct |
83 ms |
5896 KB |
Output is correct |
93 |
Correct |
451 ms |
7372 KB |
Output is correct |
94 |
Correct |
28 ms |
5580 KB |
Output is correct |
95 |
Correct |
219 ms |
6628 KB |
Output is correct |
96 |
Correct |
208 ms |
6632 KB |
Output is correct |
97 |
Correct |
715 ms |
7368 KB |
Output is correct |
98 |
Correct |
745 ms |
7368 KB |
Output is correct |