#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[60069],r[60069],s[60069];
bool done[60069];
set<int> grph[60069];
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 |
2 ms |
3540 KB |
Output is correct |
2 |
Correct |
2 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
2 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
4 ms |
3540 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
2 ms |
3540 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
3 ms |
3540 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
3 ms |
3540 KB |
Output is correct |
23 |
Correct |
2 ms |
3540 KB |
Output is correct |
24 |
Correct |
2 ms |
3540 KB |
Output is correct |
25 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3540 KB |
Output is correct |
2 |
Correct |
2 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
2 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
4 ms |
3540 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
2 ms |
3540 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
3 ms |
3540 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
3 ms |
3540 KB |
Output is correct |
23 |
Correct |
2 ms |
3540 KB |
Output is correct |
24 |
Correct |
2 ms |
3540 KB |
Output is correct |
25 |
Correct |
3 ms |
3540 KB |
Output is correct |
26 |
Correct |
137 ms |
4000 KB |
Output is correct |
27 |
Correct |
30 ms |
3924 KB |
Output is correct |
28 |
Correct |
144 ms |
4000 KB |
Output is correct |
29 |
Correct |
20 ms |
3924 KB |
Output is correct |
30 |
Correct |
153 ms |
4012 KB |
Output is correct |
31 |
Correct |
21 ms |
3924 KB |
Output is correct |
32 |
Correct |
132 ms |
3924 KB |
Output is correct |
33 |
Correct |
21 ms |
4024 KB |
Output is correct |
34 |
Correct |
19 ms |
4012 KB |
Output is correct |
35 |
Correct |
26 ms |
4016 KB |
Output is correct |
36 |
Correct |
139 ms |
3924 KB |
Output is correct |
37 |
Correct |
24 ms |
3924 KB |
Output is correct |
38 |
Correct |
127 ms |
4000 KB |
Output is correct |
39 |
Correct |
20 ms |
4024 KB |
Output is correct |
40 |
Correct |
142 ms |
3996 KB |
Output is correct |
41 |
Correct |
27 ms |
3936 KB |
Output is correct |
42 |
Correct |
141 ms |
3996 KB |
Output is correct |
43 |
Correct |
20 ms |
4020 KB |
Output is correct |
44 |
Correct |
129 ms |
3996 KB |
Output is correct |
45 |
Correct |
20 ms |
4016 KB |
Output is correct |
46 |
Correct |
134 ms |
4004 KB |
Output is correct |
47 |
Correct |
4 ms |
3796 KB |
Output is correct |
48 |
Correct |
67 ms |
4020 KB |
Output is correct |
49 |
Correct |
71 ms |
4000 KB |
Output is correct |
50 |
Correct |
148 ms |
3924 KB |
Output is correct |
51 |
Correct |
139 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
384 ms |
5716 KB |
Output is correct |
2 |
Correct |
10 ms |
5460 KB |
Output is correct |
3 |
Correct |
391 ms |
5716 KB |
Output is correct |
4 |
Correct |
11 ms |
5460 KB |
Output is correct |
5 |
Correct |
413 ms |
5720 KB |
Output is correct |
6 |
Correct |
10 ms |
5460 KB |
Output is correct |
7 |
Correct |
343 ms |
5724 KB |
Output is correct |
8 |
Correct |
11 ms |
5520 KB |
Output is correct |
9 |
Correct |
338 ms |
5716 KB |
Output is correct |
10 |
Correct |
9 ms |
5520 KB |
Output is correct |
11 |
Correct |
360 ms |
5832 KB |
Output is correct |
12 |
Correct |
12 ms |
5460 KB |
Output is correct |
13 |
Correct |
385 ms |
5720 KB |
Output is correct |
14 |
Correct |
319 ms |
5716 KB |
Output is correct |
15 |
Correct |
12 ms |
5576 KB |
Output is correct |
16 |
Correct |
412 ms |
5716 KB |
Output is correct |
17 |
Correct |
9 ms |
5460 KB |
Output is correct |
18 |
Correct |
323 ms |
5840 KB |
Output is correct |
19 |
Correct |
9 ms |
5588 KB |
Output is correct |
20 |
Correct |
329 ms |
5716 KB |
Output is correct |
21 |
Correct |
13 ms |
5460 KB |
Output is correct |
22 |
Correct |
199 ms |
5712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3540 KB |
Output is correct |
2 |
Correct |
2 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
3540 KB |
Output is correct |
4 |
Correct |
2 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
4 ms |
3540 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
2 ms |
3540 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
3 ms |
3540 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
3 ms |
3540 KB |
Output is correct |
23 |
Correct |
2 ms |
3540 KB |
Output is correct |
24 |
Correct |
2 ms |
3540 KB |
Output is correct |
25 |
Correct |
3 ms |
3540 KB |
Output is correct |
26 |
Correct |
137 ms |
4000 KB |
Output is correct |
27 |
Correct |
30 ms |
3924 KB |
Output is correct |
28 |
Correct |
144 ms |
4000 KB |
Output is correct |
29 |
Correct |
20 ms |
3924 KB |
Output is correct |
30 |
Correct |
153 ms |
4012 KB |
Output is correct |
31 |
Correct |
21 ms |
3924 KB |
Output is correct |
32 |
Correct |
132 ms |
3924 KB |
Output is correct |
33 |
Correct |
21 ms |
4024 KB |
Output is correct |
34 |
Correct |
19 ms |
4012 KB |
Output is correct |
35 |
Correct |
26 ms |
4016 KB |
Output is correct |
36 |
Correct |
139 ms |
3924 KB |
Output is correct |
37 |
Correct |
24 ms |
3924 KB |
Output is correct |
38 |
Correct |
127 ms |
4000 KB |
Output is correct |
39 |
Correct |
20 ms |
4024 KB |
Output is correct |
40 |
Correct |
142 ms |
3996 KB |
Output is correct |
41 |
Correct |
27 ms |
3936 KB |
Output is correct |
42 |
Correct |
141 ms |
3996 KB |
Output is correct |
43 |
Correct |
20 ms |
4020 KB |
Output is correct |
44 |
Correct |
129 ms |
3996 KB |
Output is correct |
45 |
Correct |
20 ms |
4016 KB |
Output is correct |
46 |
Correct |
134 ms |
4004 KB |
Output is correct |
47 |
Correct |
4 ms |
3796 KB |
Output is correct |
48 |
Correct |
67 ms |
4020 KB |
Output is correct |
49 |
Correct |
71 ms |
4000 KB |
Output is correct |
50 |
Correct |
148 ms |
3924 KB |
Output is correct |
51 |
Correct |
139 ms |
3924 KB |
Output is correct |
52 |
Correct |
384 ms |
5716 KB |
Output is correct |
53 |
Correct |
10 ms |
5460 KB |
Output is correct |
54 |
Correct |
391 ms |
5716 KB |
Output is correct |
55 |
Correct |
11 ms |
5460 KB |
Output is correct |
56 |
Correct |
413 ms |
5720 KB |
Output is correct |
57 |
Correct |
10 ms |
5460 KB |
Output is correct |
58 |
Correct |
343 ms |
5724 KB |
Output is correct |
59 |
Correct |
11 ms |
5520 KB |
Output is correct |
60 |
Correct |
338 ms |
5716 KB |
Output is correct |
61 |
Correct |
9 ms |
5520 KB |
Output is correct |
62 |
Correct |
360 ms |
5832 KB |
Output is correct |
63 |
Correct |
12 ms |
5460 KB |
Output is correct |
64 |
Correct |
385 ms |
5720 KB |
Output is correct |
65 |
Correct |
319 ms |
5716 KB |
Output is correct |
66 |
Correct |
12 ms |
5576 KB |
Output is correct |
67 |
Correct |
412 ms |
5716 KB |
Output is correct |
68 |
Correct |
9 ms |
5460 KB |
Output is correct |
69 |
Correct |
323 ms |
5840 KB |
Output is correct |
70 |
Correct |
9 ms |
5588 KB |
Output is correct |
71 |
Correct |
329 ms |
5716 KB |
Output is correct |
72 |
Correct |
13 ms |
5460 KB |
Output is correct |
73 |
Correct |
199 ms |
5712 KB |
Output is correct |
74 |
Correct |
1980 ms |
10716 KB |
Output is correct |
75 |
Correct |
143 ms |
10696 KB |
Output is correct |
76 |
Correct |
2055 ms |
10808 KB |
Output is correct |
77 |
Correct |
136 ms |
10764 KB |
Output is correct |
78 |
Correct |
1986 ms |
10776 KB |
Output is correct |
79 |
Correct |
2118 ms |
10876 KB |
Output is correct |
80 |
Correct |
119 ms |
10772 KB |
Output is correct |
81 |
Correct |
141 ms |
10740 KB |
Output is correct |
82 |
Correct |
2040 ms |
10876 KB |
Output is correct |
83 |
Correct |
2068 ms |
10768 KB |
Output is correct |
84 |
Correct |
132 ms |
10772 KB |
Output is correct |
85 |
Correct |
1954 ms |
10768 KB |
Output is correct |
86 |
Correct |
156 ms |
10748 KB |
Output is correct |
87 |
Correct |
2080 ms |
10884 KB |
Output is correct |
88 |
Correct |
142 ms |
10772 KB |
Output is correct |
89 |
Correct |
1947 ms |
10772 KB |
Output is correct |
90 |
Correct |
143 ms |
10768 KB |
Output is correct |
91 |
Correct |
2165 ms |
10872 KB |
Output is correct |
92 |
Correct |
132 ms |
10764 KB |
Output is correct |
93 |
Correct |
2200 ms |
10768 KB |
Output is correct |
94 |
Correct |
47 ms |
10464 KB |
Output is correct |
95 |
Correct |
1159 ms |
10764 KB |
Output is correct |
96 |
Correct |
1143 ms |
10760 KB |
Output is correct |
97 |
Correct |
2078 ms |
10768 KB |
Output is correct |
98 |
Correct |
1979 ms |
10892 KB |
Output is correct |