#include<bits/stdc++.h>
#define doi(i) if(!adj[i].size()) {cout << "NO\n";return 0;}if(adj[i].size()<2) {q.push(i);sum+=(i>n/2?1:-1)*((*begin(adj[i])).second);}
#define shift(x) ((x)>0?bt<<(x):bt>>(-x))
using namespace std;
multiset<pair<int, int>> adj[200100];
queue<int> q;
bool del[200100];
bitset<1200000> bt;
int tot;
void dfs(int n, int N) {
if (del[n]) return;
del[n] = true;
int nxt, cost;
tie(nxt, cost) = *adj[n].begin();
if(nxt>N)
tot += cost;
else
tot-=cost;
if (!del[nxt]) {
adj[nxt].erase(adj[nxt].find({n, cost}));
adj[n].clear();
dfs(nxt, N);
}
}
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
int n, k, sum = 0;
cin >> n >> k;
n*=2;
for(int i = 1; i <= n; i++) {
int x, y, w;
cin >> x >> y >> w;
y+=n/2;
adj[x].insert({y,w});
adj[y].insert({x,w});
}
for(int i = 1; i <= n; i++) {
doi(i);
}
while(q.size()) {
int x = q.front(), i = (*adj[x].begin()).first, w= (*adj[x].begin()).second;
del[x] = 1;
q.pop();
adj[i].erase(adj[i].find({x,w}));
doi(i);
}
bt[sum+600000]=1;
for(int i = 1; i <= n; i++) {
if(!del[i]) {
tot = 0;
adj[i].erase(adj[i].begin());
dfs(i, n/2);
bt = shift(tot)|shift(-tot);
}
}
for(int i = -k; i <= k; i++) {
if(bt[600000+i]) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
Compilation message
tug.cpp: In function 'void dfs(int, int)':
tug.cpp:17:5: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
17 | else
| ^~~~
tug.cpp:19:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
19 | if (!del[nxt]) {
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10420 KB |
Output is correct |
2 |
Correct |
9 ms |
10452 KB |
Output is correct |
3 |
Correct |
7 ms |
10424 KB |
Output is correct |
4 |
Correct |
9 ms |
10420 KB |
Output is correct |
5 |
Correct |
7 ms |
10412 KB |
Output is correct |
6 |
Correct |
7 ms |
10416 KB |
Output is correct |
7 |
Correct |
10 ms |
10412 KB |
Output is correct |
8 |
Correct |
8 ms |
10428 KB |
Output is correct |
9 |
Correct |
8 ms |
10416 KB |
Output is correct |
10 |
Correct |
8 ms |
10376 KB |
Output is correct |
11 |
Correct |
9 ms |
10340 KB |
Output is correct |
12 |
Correct |
7 ms |
10416 KB |
Output is correct |
13 |
Correct |
8 ms |
10388 KB |
Output is correct |
14 |
Correct |
7 ms |
10436 KB |
Output is correct |
15 |
Correct |
7 ms |
10420 KB |
Output is correct |
16 |
Correct |
9 ms |
10456 KB |
Output is correct |
17 |
Correct |
7 ms |
10420 KB |
Output is correct |
18 |
Correct |
7 ms |
10428 KB |
Output is correct |
19 |
Correct |
8 ms |
10352 KB |
Output is correct |
20 |
Correct |
9 ms |
10452 KB |
Output is correct |
21 |
Correct |
7 ms |
10160 KB |
Output is correct |
22 |
Correct |
7 ms |
10420 KB |
Output is correct |
23 |
Correct |
6 ms |
10452 KB |
Output is correct |
24 |
Correct |
7 ms |
10424 KB |
Output is correct |
25 |
Correct |
9 ms |
10452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10420 KB |
Output is correct |
2 |
Correct |
9 ms |
10452 KB |
Output is correct |
3 |
Correct |
7 ms |
10424 KB |
Output is correct |
4 |
Correct |
9 ms |
10420 KB |
Output is correct |
5 |
Correct |
7 ms |
10412 KB |
Output is correct |
6 |
Correct |
7 ms |
10416 KB |
Output is correct |
7 |
Correct |
10 ms |
10412 KB |
Output is correct |
8 |
Correct |
8 ms |
10428 KB |
Output is correct |
9 |
Correct |
8 ms |
10416 KB |
Output is correct |
10 |
Correct |
8 ms |
10376 KB |
Output is correct |
11 |
Correct |
9 ms |
10340 KB |
Output is correct |
12 |
Correct |
7 ms |
10416 KB |
Output is correct |
13 |
Correct |
8 ms |
10388 KB |
Output is correct |
14 |
Correct |
7 ms |
10436 KB |
Output is correct |
15 |
Correct |
7 ms |
10420 KB |
Output is correct |
16 |
Correct |
9 ms |
10456 KB |
Output is correct |
17 |
Correct |
7 ms |
10420 KB |
Output is correct |
18 |
Correct |
7 ms |
10428 KB |
Output is correct |
19 |
Correct |
8 ms |
10352 KB |
Output is correct |
20 |
Correct |
9 ms |
10452 KB |
Output is correct |
21 |
Correct |
7 ms |
10160 KB |
Output is correct |
22 |
Correct |
7 ms |
10420 KB |
Output is correct |
23 |
Correct |
6 ms |
10452 KB |
Output is correct |
24 |
Correct |
7 ms |
10424 KB |
Output is correct |
25 |
Correct |
9 ms |
10452 KB |
Output is correct |
26 |
Correct |
172 ms |
10868 KB |
Output is correct |
27 |
Correct |
27 ms |
10812 KB |
Output is correct |
28 |
Correct |
175 ms |
10868 KB |
Output is correct |
29 |
Correct |
36 ms |
10864 KB |
Output is correct |
30 |
Correct |
145 ms |
10816 KB |
Output is correct |
31 |
Correct |
35 ms |
10864 KB |
Output is correct |
32 |
Correct |
165 ms |
10868 KB |
Output is correct |
33 |
Correct |
29 ms |
10836 KB |
Output is correct |
34 |
Correct |
21 ms |
10880 KB |
Output is correct |
35 |
Correct |
32 ms |
10812 KB |
Output is correct |
36 |
Correct |
165 ms |
10864 KB |
Output is correct |
37 |
Correct |
33 ms |
10860 KB |
Output is correct |
38 |
Correct |
170 ms |
10864 KB |
Output is correct |
39 |
Correct |
37 ms |
10872 KB |
Output is correct |
40 |
Correct |
189 ms |
10864 KB |
Output is correct |
41 |
Correct |
27 ms |
10808 KB |
Output is correct |
42 |
Correct |
157 ms |
10868 KB |
Output is correct |
43 |
Correct |
28 ms |
10792 KB |
Output is correct |
44 |
Correct |
166 ms |
10868 KB |
Output is correct |
45 |
Correct |
30 ms |
10836 KB |
Output is correct |
46 |
Correct |
178 ms |
10860 KB |
Output is correct |
47 |
Correct |
9 ms |
10580 KB |
Output is correct |
48 |
Correct |
82 ms |
10836 KB |
Output is correct |
49 |
Correct |
86 ms |
10868 KB |
Output is correct |
50 |
Correct |
166 ms |
10836 KB |
Output is correct |
51 |
Correct |
166 ms |
10868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
593 ms |
12568 KB |
Output is correct |
2 |
Correct |
20 ms |
12204 KB |
Output is correct |
3 |
Correct |
494 ms |
12560 KB |
Output is correct |
4 |
Correct |
15 ms |
12272 KB |
Output is correct |
5 |
Correct |
575 ms |
12564 KB |
Output is correct |
6 |
Correct |
15 ms |
12244 KB |
Output is correct |
7 |
Correct |
474 ms |
12560 KB |
Output is correct |
8 |
Correct |
16 ms |
12224 KB |
Output is correct |
9 |
Correct |
515 ms |
12568 KB |
Output is correct |
10 |
Correct |
16 ms |
12244 KB |
Output is correct |
11 |
Correct |
482 ms |
12564 KB |
Output is correct |
12 |
Correct |
15 ms |
12244 KB |
Output is correct |
13 |
Correct |
470 ms |
12572 KB |
Output is correct |
14 |
Correct |
462 ms |
12620 KB |
Output is correct |
15 |
Correct |
15 ms |
12184 KB |
Output is correct |
16 |
Correct |
463 ms |
12500 KB |
Output is correct |
17 |
Correct |
23 ms |
12152 KB |
Output is correct |
18 |
Correct |
557 ms |
12572 KB |
Output is correct |
19 |
Correct |
22 ms |
12244 KB |
Output is correct |
20 |
Correct |
457 ms |
12564 KB |
Output is correct |
21 |
Correct |
17 ms |
12244 KB |
Output is correct |
22 |
Correct |
205 ms |
12472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10420 KB |
Output is correct |
2 |
Correct |
9 ms |
10452 KB |
Output is correct |
3 |
Correct |
7 ms |
10424 KB |
Output is correct |
4 |
Correct |
9 ms |
10420 KB |
Output is correct |
5 |
Correct |
7 ms |
10412 KB |
Output is correct |
6 |
Correct |
7 ms |
10416 KB |
Output is correct |
7 |
Correct |
10 ms |
10412 KB |
Output is correct |
8 |
Correct |
8 ms |
10428 KB |
Output is correct |
9 |
Correct |
8 ms |
10416 KB |
Output is correct |
10 |
Correct |
8 ms |
10376 KB |
Output is correct |
11 |
Correct |
9 ms |
10340 KB |
Output is correct |
12 |
Correct |
7 ms |
10416 KB |
Output is correct |
13 |
Correct |
8 ms |
10388 KB |
Output is correct |
14 |
Correct |
7 ms |
10436 KB |
Output is correct |
15 |
Correct |
7 ms |
10420 KB |
Output is correct |
16 |
Correct |
9 ms |
10456 KB |
Output is correct |
17 |
Correct |
7 ms |
10420 KB |
Output is correct |
18 |
Correct |
7 ms |
10428 KB |
Output is correct |
19 |
Correct |
8 ms |
10352 KB |
Output is correct |
20 |
Correct |
9 ms |
10452 KB |
Output is correct |
21 |
Correct |
7 ms |
10160 KB |
Output is correct |
22 |
Correct |
7 ms |
10420 KB |
Output is correct |
23 |
Correct |
6 ms |
10452 KB |
Output is correct |
24 |
Correct |
7 ms |
10424 KB |
Output is correct |
25 |
Correct |
9 ms |
10452 KB |
Output is correct |
26 |
Correct |
172 ms |
10868 KB |
Output is correct |
27 |
Correct |
27 ms |
10812 KB |
Output is correct |
28 |
Correct |
175 ms |
10868 KB |
Output is correct |
29 |
Correct |
36 ms |
10864 KB |
Output is correct |
30 |
Correct |
145 ms |
10816 KB |
Output is correct |
31 |
Correct |
35 ms |
10864 KB |
Output is correct |
32 |
Correct |
165 ms |
10868 KB |
Output is correct |
33 |
Correct |
29 ms |
10836 KB |
Output is correct |
34 |
Correct |
21 ms |
10880 KB |
Output is correct |
35 |
Correct |
32 ms |
10812 KB |
Output is correct |
36 |
Correct |
165 ms |
10864 KB |
Output is correct |
37 |
Correct |
33 ms |
10860 KB |
Output is correct |
38 |
Correct |
170 ms |
10864 KB |
Output is correct |
39 |
Correct |
37 ms |
10872 KB |
Output is correct |
40 |
Correct |
189 ms |
10864 KB |
Output is correct |
41 |
Correct |
27 ms |
10808 KB |
Output is correct |
42 |
Correct |
157 ms |
10868 KB |
Output is correct |
43 |
Correct |
28 ms |
10792 KB |
Output is correct |
44 |
Correct |
166 ms |
10868 KB |
Output is correct |
45 |
Correct |
30 ms |
10836 KB |
Output is correct |
46 |
Correct |
178 ms |
10860 KB |
Output is correct |
47 |
Correct |
9 ms |
10580 KB |
Output is correct |
48 |
Correct |
82 ms |
10836 KB |
Output is correct |
49 |
Correct |
86 ms |
10868 KB |
Output is correct |
50 |
Correct |
166 ms |
10836 KB |
Output is correct |
51 |
Correct |
166 ms |
10868 KB |
Output is correct |
52 |
Correct |
593 ms |
12568 KB |
Output is correct |
53 |
Correct |
20 ms |
12204 KB |
Output is correct |
54 |
Correct |
494 ms |
12560 KB |
Output is correct |
55 |
Correct |
15 ms |
12272 KB |
Output is correct |
56 |
Correct |
575 ms |
12564 KB |
Output is correct |
57 |
Correct |
15 ms |
12244 KB |
Output is correct |
58 |
Correct |
474 ms |
12560 KB |
Output is correct |
59 |
Correct |
16 ms |
12224 KB |
Output is correct |
60 |
Correct |
515 ms |
12568 KB |
Output is correct |
61 |
Correct |
16 ms |
12244 KB |
Output is correct |
62 |
Correct |
482 ms |
12564 KB |
Output is correct |
63 |
Correct |
15 ms |
12244 KB |
Output is correct |
64 |
Correct |
470 ms |
12572 KB |
Output is correct |
65 |
Correct |
462 ms |
12620 KB |
Output is correct |
66 |
Correct |
15 ms |
12184 KB |
Output is correct |
67 |
Correct |
463 ms |
12500 KB |
Output is correct |
68 |
Correct |
23 ms |
12152 KB |
Output is correct |
69 |
Correct |
557 ms |
12572 KB |
Output is correct |
70 |
Correct |
22 ms |
12244 KB |
Output is correct |
71 |
Correct |
457 ms |
12564 KB |
Output is correct |
72 |
Correct |
17 ms |
12244 KB |
Output is correct |
73 |
Correct |
205 ms |
12472 KB |
Output is correct |
74 |
Correct |
2396 ms |
17056 KB |
Output is correct |
75 |
Correct |
152 ms |
16824 KB |
Output is correct |
76 |
Correct |
2599 ms |
16936 KB |
Output is correct |
77 |
Correct |
186 ms |
16940 KB |
Output is correct |
78 |
Correct |
2428 ms |
16884 KB |
Output is correct |
79 |
Correct |
2222 ms |
16644 KB |
Output is correct |
80 |
Correct |
117 ms |
16500 KB |
Output is correct |
81 |
Correct |
125 ms |
16492 KB |
Output is correct |
82 |
Correct |
2185 ms |
16724 KB |
Output is correct |
83 |
Correct |
2016 ms |
16500 KB |
Output is correct |
84 |
Correct |
127 ms |
16084 KB |
Output is correct |
85 |
Correct |
2211 ms |
16248 KB |
Output is correct |
86 |
Correct |
117 ms |
16084 KB |
Output is correct |
87 |
Correct |
1953 ms |
16236 KB |
Output is correct |
88 |
Correct |
113 ms |
16136 KB |
Output is correct |
89 |
Correct |
2083 ms |
16136 KB |
Output is correct |
90 |
Correct |
135 ms |
16124 KB |
Output is correct |
91 |
Correct |
2252 ms |
16376 KB |
Output is correct |
92 |
Correct |
133 ms |
16820 KB |
Output is correct |
93 |
Correct |
2412 ms |
17064 KB |
Output is correct |
94 |
Correct |
46 ms |
16560 KB |
Output is correct |
95 |
Correct |
1730 ms |
17100 KB |
Output is correct |
96 |
Correct |
1045 ms |
17028 KB |
Output is correct |
97 |
Correct |
1936 ms |
17040 KB |
Output is correct |
98 |
Correct |
1926 ms |
17184 KB |
Output is correct |