#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <functional>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef set<ll> sll;
typedef vector<sll> vsll;
typedef map<ll, ll> mll_ll;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define sz(a) a.size()
const ll MAXW = 20 * 30001;
ll n, k;
vll v;
vpll e;
vsll g;
ll getE(ll i, ll o) {
return (o == e[i].first) ? e[i].second: e[i].first;
}
vll diffs;
ll DFS(ll cur) {
if (sz(g[cur]) == 0) {
return 0;
}
ll p = *g[cur].begin();
ll nxt = getE(p, cur);
g[cur].erase(p);
g[nxt].erase(p);
return v[p] - DFS(nxt);
}
bitset<MAXW> dp;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
v.resize(2 * n);
e.resize(2 * n);
g.resize(2 * n);
rep(i, 0, 2 * n) {
ll l, r;
cin >> l >> r >> v[i];
l--; r--;
e[i] = mp(l, r + n);
g[l].insert(i);
g[r + n].insert(i);
}
vll leaves;
rep(i, 0, 2 * n) {
if (sz(g[i]) == 1) leaves.pb(i);
}
ll diff = 0;
while (!leaves.empty()) {
ll nxt = leaves.back();
leaves.pop_back();
ll p = *g[nxt].begin();
diff += (nxt >= n) ? v[p] : -v[p];
ll oth = getE(p, nxt);
g[oth].erase(p);
g[nxt].clear();
if (sz(g[oth]) == 1) leaves.pb(oth);
}
rep(i, 0, 2 * n) {
if ((sz(g[i]) != 0) && (sz(g[i]) != 2)) {
cout << "NO" << endl;
return 0;
}
}
rep(i, 0, 2 * n) {
if (sz(g[i])) {
diffs.pb(llabs(DFS(i)));
}
}
mll_ll w;
rep(i, 0, sz(diffs)) {
ll t = diffs[i];
w[t]++;
while (w[t] == 3) {
w[t] -= 2;
t *= 2;
w[t]++;
}
}
diff = llabs(diff);
dp[0] = true;
ll S = 0;
for (mll_ll::iterator itr = w.begin(); itr != w.end(); itr++) {
rep(i, 0, itr->second) {
dp |= (dp << itr->first);
S += itr->first;
}
}
bool is_poss = false;
rep(i, 0, MAXW) {
is_poss |= (llabs((2 * i) - S + diff) <= k) && dp[i];
}
if (is_poss) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
// now we check whether we have attained a sum S in dp for which: |2 * S - diff| ≤ k
return 0;
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:21:39: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | #define rep(i, a, b) for (ll i = a; i < b; i++)
| ^
tug.cpp:104:5: note: in expansion of macro 'rep'
104 | rep(i, 0, sz(diffs)) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
472 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
472 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1248 KB |
Output is correct |
27 |
Correct |
7 ms |
1124 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
12 ms |
1264 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
7 ms |
1252 KB |
Output is correct |
32 |
Correct |
6 ms |
1252 KB |
Output is correct |
33 |
Correct |
8 ms |
1264 KB |
Output is correct |
34 |
Correct |
3 ms |
1124 KB |
Output is correct |
35 |
Correct |
8 ms |
1364 KB |
Output is correct |
36 |
Correct |
5 ms |
1236 KB |
Output is correct |
37 |
Correct |
7 ms |
1136 KB |
Output is correct |
38 |
Correct |
5 ms |
1236 KB |
Output is correct |
39 |
Correct |
8 ms |
1264 KB |
Output is correct |
40 |
Correct |
5 ms |
1264 KB |
Output is correct |
41 |
Correct |
8 ms |
1232 KB |
Output is correct |
42 |
Correct |
5 ms |
1232 KB |
Output is correct |
43 |
Correct |
7 ms |
1236 KB |
Output is correct |
44 |
Correct |
4 ms |
1236 KB |
Output is correct |
45 |
Correct |
7 ms |
1236 KB |
Output is correct |
46 |
Correct |
5 ms |
1248 KB |
Output is correct |
47 |
Correct |
3 ms |
1120 KB |
Output is correct |
48 |
Correct |
5 ms |
1240 KB |
Output is correct |
49 |
Correct |
5 ms |
1284 KB |
Output is correct |
50 |
Correct |
4 ms |
1356 KB |
Output is correct |
51 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4196 KB |
Output is correct |
2 |
Correct |
10 ms |
4112 KB |
Output is correct |
3 |
Correct |
11 ms |
4188 KB |
Output is correct |
4 |
Correct |
10 ms |
4004 KB |
Output is correct |
5 |
Correct |
11 ms |
4196 KB |
Output is correct |
6 |
Correct |
13 ms |
4060 KB |
Output is correct |
7 |
Correct |
10 ms |
4180 KB |
Output is correct |
8 |
Correct |
10 ms |
4052 KB |
Output is correct |
9 |
Correct |
11 ms |
4180 KB |
Output is correct |
10 |
Correct |
11 ms |
4112 KB |
Output is correct |
11 |
Correct |
17 ms |
4200 KB |
Output is correct |
12 |
Correct |
16 ms |
4052 KB |
Output is correct |
13 |
Correct |
13 ms |
4220 KB |
Output is correct |
14 |
Correct |
10 ms |
4180 KB |
Output is correct |
15 |
Correct |
10 ms |
4040 KB |
Output is correct |
16 |
Correct |
12 ms |
4256 KB |
Output is correct |
17 |
Correct |
9 ms |
4016 KB |
Output is correct |
18 |
Correct |
11 ms |
4180 KB |
Output is correct |
19 |
Correct |
14 ms |
4008 KB |
Output is correct |
20 |
Correct |
11 ms |
4192 KB |
Output is correct |
21 |
Correct |
18 ms |
4072 KB |
Output is correct |
22 |
Correct |
21 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
472 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
1248 KB |
Output is correct |
27 |
Correct |
7 ms |
1124 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
12 ms |
1264 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
7 ms |
1252 KB |
Output is correct |
32 |
Correct |
6 ms |
1252 KB |
Output is correct |
33 |
Correct |
8 ms |
1264 KB |
Output is correct |
34 |
Correct |
3 ms |
1124 KB |
Output is correct |
35 |
Correct |
8 ms |
1364 KB |
Output is correct |
36 |
Correct |
5 ms |
1236 KB |
Output is correct |
37 |
Correct |
7 ms |
1136 KB |
Output is correct |
38 |
Correct |
5 ms |
1236 KB |
Output is correct |
39 |
Correct |
8 ms |
1264 KB |
Output is correct |
40 |
Correct |
5 ms |
1264 KB |
Output is correct |
41 |
Correct |
8 ms |
1232 KB |
Output is correct |
42 |
Correct |
5 ms |
1232 KB |
Output is correct |
43 |
Correct |
7 ms |
1236 KB |
Output is correct |
44 |
Correct |
4 ms |
1236 KB |
Output is correct |
45 |
Correct |
7 ms |
1236 KB |
Output is correct |
46 |
Correct |
5 ms |
1248 KB |
Output is correct |
47 |
Correct |
3 ms |
1120 KB |
Output is correct |
48 |
Correct |
5 ms |
1240 KB |
Output is correct |
49 |
Correct |
5 ms |
1284 KB |
Output is correct |
50 |
Correct |
4 ms |
1356 KB |
Output is correct |
51 |
Correct |
4 ms |
1236 KB |
Output is correct |
52 |
Correct |
11 ms |
4196 KB |
Output is correct |
53 |
Correct |
10 ms |
4112 KB |
Output is correct |
54 |
Correct |
11 ms |
4188 KB |
Output is correct |
55 |
Correct |
10 ms |
4004 KB |
Output is correct |
56 |
Correct |
11 ms |
4196 KB |
Output is correct |
57 |
Correct |
13 ms |
4060 KB |
Output is correct |
58 |
Correct |
10 ms |
4180 KB |
Output is correct |
59 |
Correct |
10 ms |
4052 KB |
Output is correct |
60 |
Correct |
11 ms |
4180 KB |
Output is correct |
61 |
Correct |
11 ms |
4112 KB |
Output is correct |
62 |
Correct |
17 ms |
4200 KB |
Output is correct |
63 |
Correct |
16 ms |
4052 KB |
Output is correct |
64 |
Correct |
13 ms |
4220 KB |
Output is correct |
65 |
Correct |
10 ms |
4180 KB |
Output is correct |
66 |
Correct |
10 ms |
4040 KB |
Output is correct |
67 |
Correct |
12 ms |
4256 KB |
Output is correct |
68 |
Correct |
9 ms |
4016 KB |
Output is correct |
69 |
Correct |
11 ms |
4180 KB |
Output is correct |
70 |
Correct |
14 ms |
4008 KB |
Output is correct |
71 |
Correct |
11 ms |
4192 KB |
Output is correct |
72 |
Correct |
18 ms |
4072 KB |
Output is correct |
73 |
Correct |
21 ms |
4172 KB |
Output is correct |
74 |
Correct |
68 ms |
11544 KB |
Output is correct |
75 |
Correct |
74 ms |
11160 KB |
Output is correct |
76 |
Correct |
48 ms |
11468 KB |
Output is correct |
77 |
Correct |
72 ms |
11240 KB |
Output is correct |
78 |
Correct |
48 ms |
11472 KB |
Output is correct |
79 |
Correct |
57 ms |
11592 KB |
Output is correct |
80 |
Correct |
83 ms |
11260 KB |
Output is correct |
81 |
Correct |
61 ms |
11112 KB |
Output is correct |
82 |
Correct |
30 ms |
11112 KB |
Output is correct |
83 |
Correct |
55 ms |
11524 KB |
Output is correct |
84 |
Correct |
66 ms |
11144 KB |
Output is correct |
85 |
Correct |
61 ms |
11472 KB |
Output is correct |
86 |
Correct |
65 ms |
11324 KB |
Output is correct |
87 |
Correct |
50 ms |
11584 KB |
Output is correct |
88 |
Correct |
72 ms |
11160 KB |
Output is correct |
89 |
Correct |
47 ms |
11584 KB |
Output is correct |
90 |
Correct |
73 ms |
11224 KB |
Output is correct |
91 |
Correct |
52 ms |
11468 KB |
Output is correct |
92 |
Correct |
66 ms |
11240 KB |
Output is correct |
93 |
Correct |
40 ms |
11520 KB |
Output is correct |
94 |
Correct |
39 ms |
11536 KB |
Output is correct |
95 |
Correct |
49 ms |
11576 KB |
Output is correct |
96 |
Correct |
57 ms |
11636 KB |
Output is correct |
97 |
Correct |
41 ms |
11500 KB |
Output is correct |
98 |
Correct |
38 ms |
11544 KB |
Output is correct |