#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define endl '\n'
#define all(x) begin(x), end(x)
int n,k,ori=600069;
int l[30069],r[30069],s[30069];
bool done[30069];
set<int> grph[30069];
bitset<600069*2> bst;
void check(int x) {
if (done[x]) {
return;
}
if (grph[x].size()==0) {
cout<<"NO"<<endl;
exit(0);
}
if (grph[x].size()!=1) {
return;
}
done[x]=true;
int i=*grph[x].begin();
ori+=(x<n?-s[i]:s[i]);
grph[l[i]].erase(i);
grph[r[i]].erase(i);
check(l[i]);
check(r[i]);
}
int dfs(int x, int lst=-1) {
if (done[x]) {
return 0;
}
done[x]=true;
for (auto i : grph[x]) {
if (l[i]!=lst&&l[i]!=x) {
return s[i]-dfs(l[i],x);
}
if (lst==l[i]) {
lst=-1;
}
if (r[i]!=lst&&r[i]!=x) {
return s[i]-dfs(r[i],x);
}
if (lst==r[i]) {
lst=-1;
}
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>k;
for (int i = 0; i < 2*n; i++) {
cin>>l[i]>>r[i]>>s[i];
l[i]--;r[i]--;
r[i]+=n;
grph[l[i]].insert(i);
grph[r[i]].insert(i);
}
for (int i = 0; i < 2*n; i++) {
check(i);
}
bst[ori]=true;
for (int i = 0; i < 2*n; i++) {
if (done[i]) {
continue;
}
int tmp=dfs(i);
tmp=abs(tmp);
bst=bst<<tmp;
bst|=bst>>(2*tmp);
}
for (int i = 600069-k; i <= 600069+k; i++) {
if (bst[i]) {
cout<<"YES"<<endl;
return 0;
}
}
cout<<"NO"<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2196 KB |
Output is correct |
2 |
Correct |
2 ms |
2132 KB |
Output is correct |
3 |
Correct |
2 ms |
2176 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
2 ms |
2132 KB |
Output is correct |
6 |
Correct |
2 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
2 ms |
2132 KB |
Output is correct |
9 |
Correct |
3 ms |
2148 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2152 KB |
Output is correct |
12 |
Correct |
2 ms |
2168 KB |
Output is correct |
13 |
Correct |
2 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2164 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
3 ms |
2168 KB |
Output is correct |
24 |
Correct |
2 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2196 KB |
Output is correct |
2 |
Correct |
2 ms |
2132 KB |
Output is correct |
3 |
Correct |
2 ms |
2176 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
2 ms |
2132 KB |
Output is correct |
6 |
Correct |
2 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
2 ms |
2132 KB |
Output is correct |
9 |
Correct |
3 ms |
2148 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2152 KB |
Output is correct |
12 |
Correct |
2 ms |
2168 KB |
Output is correct |
13 |
Correct |
2 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2164 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
3 ms |
2168 KB |
Output is correct |
24 |
Correct |
2 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2168 KB |
Output is correct |
26 |
Correct |
130 ms |
2644 KB |
Output is correct |
27 |
Correct |
20 ms |
2568 KB |
Output is correct |
28 |
Correct |
135 ms |
2652 KB |
Output is correct |
29 |
Correct |
25 ms |
2644 KB |
Output is correct |
30 |
Correct |
133 ms |
2648 KB |
Output is correct |
31 |
Correct |
22 ms |
2564 KB |
Output is correct |
32 |
Correct |
139 ms |
2644 KB |
Output is correct |
33 |
Correct |
20 ms |
2644 KB |
Output is correct |
34 |
Correct |
13 ms |
2644 KB |
Output is correct |
35 |
Correct |
21 ms |
2660 KB |
Output is correct |
36 |
Correct |
140 ms |
2648 KB |
Output is correct |
37 |
Correct |
22 ms |
2644 KB |
Output is correct |
38 |
Correct |
133 ms |
2644 KB |
Output is correct |
39 |
Correct |
20 ms |
2560 KB |
Output is correct |
40 |
Correct |
128 ms |
2644 KB |
Output is correct |
41 |
Correct |
20 ms |
2644 KB |
Output is correct |
42 |
Correct |
165 ms |
2648 KB |
Output is correct |
43 |
Correct |
28 ms |
2644 KB |
Output is correct |
44 |
Correct |
138 ms |
2644 KB |
Output is correct |
45 |
Correct |
20 ms |
2644 KB |
Output is correct |
46 |
Correct |
135 ms |
2644 KB |
Output is correct |
47 |
Correct |
3 ms |
2516 KB |
Output is correct |
48 |
Correct |
66 ms |
2636 KB |
Output is correct |
49 |
Correct |
65 ms |
2772 KB |
Output is correct |
50 |
Correct |
129 ms |
2644 KB |
Output is correct |
51 |
Correct |
157 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
351 ms |
4564 KB |
Output is correct |
2 |
Correct |
9 ms |
4356 KB |
Output is correct |
3 |
Correct |
403 ms |
4536 KB |
Output is correct |
4 |
Correct |
10 ms |
4308 KB |
Output is correct |
5 |
Correct |
328 ms |
4536 KB |
Output is correct |
6 |
Correct |
11 ms |
4284 KB |
Output is correct |
7 |
Correct |
388 ms |
4536 KB |
Output is correct |
8 |
Correct |
9 ms |
4360 KB |
Output is correct |
9 |
Correct |
361 ms |
4560 KB |
Output is correct |
10 |
Correct |
10 ms |
4288 KB |
Output is correct |
11 |
Correct |
387 ms |
4536 KB |
Output is correct |
12 |
Correct |
12 ms |
4400 KB |
Output is correct |
13 |
Correct |
355 ms |
4556 KB |
Output is correct |
14 |
Correct |
321 ms |
4532 KB |
Output is correct |
15 |
Correct |
9 ms |
4388 KB |
Output is correct |
16 |
Correct |
328 ms |
4552 KB |
Output is correct |
17 |
Correct |
9 ms |
4308 KB |
Output is correct |
18 |
Correct |
406 ms |
4484 KB |
Output is correct |
19 |
Correct |
17 ms |
4336 KB |
Output is correct |
20 |
Correct |
330 ms |
4592 KB |
Output is correct |
21 |
Correct |
13 ms |
4400 KB |
Output is correct |
22 |
Correct |
224 ms |
4536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2196 KB |
Output is correct |
2 |
Correct |
2 ms |
2132 KB |
Output is correct |
3 |
Correct |
2 ms |
2176 KB |
Output is correct |
4 |
Correct |
3 ms |
2132 KB |
Output is correct |
5 |
Correct |
2 ms |
2132 KB |
Output is correct |
6 |
Correct |
2 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
2 ms |
2132 KB |
Output is correct |
9 |
Correct |
3 ms |
2148 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
2152 KB |
Output is correct |
12 |
Correct |
2 ms |
2168 KB |
Output is correct |
13 |
Correct |
2 ms |
2132 KB |
Output is correct |
14 |
Correct |
2 ms |
2164 KB |
Output is correct |
15 |
Correct |
2 ms |
2132 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
2 ms |
2132 KB |
Output is correct |
18 |
Correct |
2 ms |
2132 KB |
Output is correct |
19 |
Correct |
2 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
2004 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
3 ms |
2168 KB |
Output is correct |
24 |
Correct |
2 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
2168 KB |
Output is correct |
26 |
Correct |
130 ms |
2644 KB |
Output is correct |
27 |
Correct |
20 ms |
2568 KB |
Output is correct |
28 |
Correct |
135 ms |
2652 KB |
Output is correct |
29 |
Correct |
25 ms |
2644 KB |
Output is correct |
30 |
Correct |
133 ms |
2648 KB |
Output is correct |
31 |
Correct |
22 ms |
2564 KB |
Output is correct |
32 |
Correct |
139 ms |
2644 KB |
Output is correct |
33 |
Correct |
20 ms |
2644 KB |
Output is correct |
34 |
Correct |
13 ms |
2644 KB |
Output is correct |
35 |
Correct |
21 ms |
2660 KB |
Output is correct |
36 |
Correct |
140 ms |
2648 KB |
Output is correct |
37 |
Correct |
22 ms |
2644 KB |
Output is correct |
38 |
Correct |
133 ms |
2644 KB |
Output is correct |
39 |
Correct |
20 ms |
2560 KB |
Output is correct |
40 |
Correct |
128 ms |
2644 KB |
Output is correct |
41 |
Correct |
20 ms |
2644 KB |
Output is correct |
42 |
Correct |
165 ms |
2648 KB |
Output is correct |
43 |
Correct |
28 ms |
2644 KB |
Output is correct |
44 |
Correct |
138 ms |
2644 KB |
Output is correct |
45 |
Correct |
20 ms |
2644 KB |
Output is correct |
46 |
Correct |
135 ms |
2644 KB |
Output is correct |
47 |
Correct |
3 ms |
2516 KB |
Output is correct |
48 |
Correct |
66 ms |
2636 KB |
Output is correct |
49 |
Correct |
65 ms |
2772 KB |
Output is correct |
50 |
Correct |
129 ms |
2644 KB |
Output is correct |
51 |
Correct |
157 ms |
2648 KB |
Output is correct |
52 |
Correct |
351 ms |
4564 KB |
Output is correct |
53 |
Correct |
9 ms |
4356 KB |
Output is correct |
54 |
Correct |
403 ms |
4536 KB |
Output is correct |
55 |
Correct |
10 ms |
4308 KB |
Output is correct |
56 |
Correct |
328 ms |
4536 KB |
Output is correct |
57 |
Correct |
11 ms |
4284 KB |
Output is correct |
58 |
Correct |
388 ms |
4536 KB |
Output is correct |
59 |
Correct |
9 ms |
4360 KB |
Output is correct |
60 |
Correct |
361 ms |
4560 KB |
Output is correct |
61 |
Correct |
10 ms |
4288 KB |
Output is correct |
62 |
Correct |
387 ms |
4536 KB |
Output is correct |
63 |
Correct |
12 ms |
4400 KB |
Output is correct |
64 |
Correct |
355 ms |
4556 KB |
Output is correct |
65 |
Correct |
321 ms |
4532 KB |
Output is correct |
66 |
Correct |
9 ms |
4388 KB |
Output is correct |
67 |
Correct |
328 ms |
4552 KB |
Output is correct |
68 |
Correct |
9 ms |
4308 KB |
Output is correct |
69 |
Correct |
406 ms |
4484 KB |
Output is correct |
70 |
Correct |
17 ms |
4336 KB |
Output is correct |
71 |
Correct |
330 ms |
4592 KB |
Output is correct |
72 |
Correct |
13 ms |
4400 KB |
Output is correct |
73 |
Correct |
224 ms |
4536 KB |
Output is correct |
74 |
Runtime error |
4 ms |
3924 KB |
Execution killed with signal 11 |
75 |
Halted |
0 ms |
0 KB |
- |