#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <stack>
#include <cstring>
#include <map>
using namespace std;
const int MAX = 100007, MAX_K = 500007, INF = 1e8;
int n, k, l[MAX], r[MAX], s[MAX], total = 0, inDeg[MAX];
bool used[MAX], sack[MAX_K], happy[MAX];
vector<int> inV[MAX];
map<int, int> packs;
void kill() { cout << "NO"; exit(0); }
signed main()
{
// IO
memset(used, 0, sizeof used);
memset(happy, 0, sizeof happy);
ios_base::sync_with_stdio(0); cin.tie(0);
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;
inV[l[i]].push_back(i); ++inDeg[l[i]];
inV[r[i]].push_back(i); ++inDeg[r[i]];
}
// remove singles
stack<int> st;
for (int p = 0; p < 2 * n; ++p) {
if (inV[p].size() == 1) st.push(p);
}
int cur = 0;
while (!st.empty()) {
int p = st.top(); st.pop();
used[p] = 1;
int i;
for (int x : inV[p]) {
if (!happy[x]) {
i = x;
break;
}
}
happy[i] = 1;
int nxt;
if (l[i] == p) cur += s[i], nxt = r[i];
else cur -= s[i], nxt = l[i];
--inDeg[nxt];
if (inDeg[nxt] == 1) st.push(nxt);
}
// check & fix
for (int p = 0; p < 2 * n; ++p) {
if (used[p]) continue;
if (inDeg[p] != 2) kill();
vector<int> tmp; tmp.reserve(2);
for (int i : inV[p]) if (!happy[i]) tmp.push_back(i);
tmp.swap(inV[p]);
}
if (cur != 0) packs[abs(cur)] = 1;
// initialize knapsack
for (int p = 0; p < 2 * n; ++p) {
if (used[p]) continue;
int sgn = 1, cur = 0;
int prv = (*inV[p].begin()), nxt = (*inV[p].rbegin());
cur += sgn * s[prv]; sgn = -sgn; used[p] = 1;
int p1 = (l[nxt] == p) ? r[nxt] : l[nxt];
while (p1 != p) {
prv = nxt;
nxt = ((*inV[p1].begin()) == prv) ? (*inV[p1].rbegin()) : (*inV[p1].begin());
cur += sgn * s[prv]; sgn = -sgn; used[p1] = 1;
p1 = (l[nxt] == p1) ? r[nxt] : l[nxt];
}
cur = abs(cur);
if (cur == 0) continue;
if (packs.find(cur) == packs.end()) packs[cur] = 1;
else ++ packs[cur];
}
memset(sack, 0, sizeof sack);
sack[0] = 1;
// total
total = 0;
for (map<int, int>::iterator it = packs.begin(); it != packs.end(); ++it) total += it->first * it->second;
for (map<int, int>::iterator it = packs.begin(); it != packs.end(); ++it) {
int val = it->first, cnt = it->second;
// cerr << val << ' ' << cnt << endl;
for (int start = 0; start < val; ++start) {
int last = -INF;
for (int i = start; i <= total / 2; i += val) {
if (sack[i]) { last = i; continue; }
if (last + val * cnt >= i) sack[i] = 1;
}
}
}
int i = total / 2;
while (!sack[i]) --i;
int ans = total - 2 * i;
// cerr << ans << endl;
if (ans <= k) cout << "YES";
else cout << "NO";
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:53:46: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (l[i] == p) cur += s[i], nxt = r[i];
~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
4 ms |
3588 KB |
Output is correct |
4 |
Correct |
5 ms |
3588 KB |
Output is correct |
5 |
Correct |
5 ms |
3588 KB |
Output is correct |
6 |
Correct |
4 ms |
3588 KB |
Output is correct |
7 |
Correct |
5 ms |
3588 KB |
Output is correct |
8 |
Correct |
5 ms |
3604 KB |
Output is correct |
9 |
Correct |
4 ms |
3604 KB |
Output is correct |
10 |
Correct |
4 ms |
3604 KB |
Output is correct |
11 |
Correct |
4 ms |
3648 KB |
Output is correct |
12 |
Correct |
4 ms |
3648 KB |
Output is correct |
13 |
Correct |
5 ms |
3648 KB |
Output is correct |
14 |
Correct |
4 ms |
3648 KB |
Output is correct |
15 |
Correct |
5 ms |
3648 KB |
Output is correct |
16 |
Correct |
8 ms |
3648 KB |
Output is correct |
17 |
Correct |
5 ms |
3648 KB |
Output is correct |
18 |
Correct |
4 ms |
3684 KB |
Output is correct |
19 |
Correct |
5 ms |
3684 KB |
Output is correct |
20 |
Correct |
5 ms |
3684 KB |
Output is correct |
21 |
Correct |
4 ms |
3684 KB |
Output is correct |
22 |
Correct |
5 ms |
3684 KB |
Output is correct |
23 |
Correct |
4 ms |
3684 KB |
Output is correct |
24 |
Correct |
4 ms |
3684 KB |
Output is correct |
25 |
Correct |
5 ms |
3684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
4 ms |
3588 KB |
Output is correct |
4 |
Correct |
5 ms |
3588 KB |
Output is correct |
5 |
Correct |
5 ms |
3588 KB |
Output is correct |
6 |
Correct |
4 ms |
3588 KB |
Output is correct |
7 |
Correct |
5 ms |
3588 KB |
Output is correct |
8 |
Correct |
5 ms |
3604 KB |
Output is correct |
9 |
Correct |
4 ms |
3604 KB |
Output is correct |
10 |
Correct |
4 ms |
3604 KB |
Output is correct |
11 |
Correct |
4 ms |
3648 KB |
Output is correct |
12 |
Correct |
4 ms |
3648 KB |
Output is correct |
13 |
Correct |
5 ms |
3648 KB |
Output is correct |
14 |
Correct |
4 ms |
3648 KB |
Output is correct |
15 |
Correct |
5 ms |
3648 KB |
Output is correct |
16 |
Correct |
8 ms |
3648 KB |
Output is correct |
17 |
Correct |
5 ms |
3648 KB |
Output is correct |
18 |
Correct |
4 ms |
3684 KB |
Output is correct |
19 |
Correct |
5 ms |
3684 KB |
Output is correct |
20 |
Correct |
5 ms |
3684 KB |
Output is correct |
21 |
Correct |
4 ms |
3684 KB |
Output is correct |
22 |
Correct |
5 ms |
3684 KB |
Output is correct |
23 |
Correct |
4 ms |
3684 KB |
Output is correct |
24 |
Correct |
4 ms |
3684 KB |
Output is correct |
25 |
Correct |
5 ms |
3684 KB |
Output is correct |
26 |
Correct |
6 ms |
3836 KB |
Output is correct |
27 |
Correct |
11 ms |
3840 KB |
Output is correct |
28 |
Correct |
7 ms |
3840 KB |
Output is correct |
29 |
Correct |
18 ms |
3840 KB |
Output is correct |
30 |
Correct |
7 ms |
3840 KB |
Output is correct |
31 |
Correct |
13 ms |
3892 KB |
Output is correct |
32 |
Correct |
9 ms |
3892 KB |
Output is correct |
33 |
Correct |
12 ms |
3892 KB |
Output is correct |
34 |
Correct |
7 ms |
3892 KB |
Output is correct |
35 |
Correct |
14 ms |
3892 KB |
Output is correct |
36 |
Correct |
15 ms |
3892 KB |
Output is correct |
37 |
Correct |
12 ms |
3892 KB |
Output is correct |
38 |
Correct |
7 ms |
3892 KB |
Output is correct |
39 |
Correct |
12 ms |
3892 KB |
Output is correct |
40 |
Correct |
7 ms |
3892 KB |
Output is correct |
41 |
Correct |
12 ms |
3892 KB |
Output is correct |
42 |
Correct |
7 ms |
3892 KB |
Output is correct |
43 |
Correct |
11 ms |
3892 KB |
Output is correct |
44 |
Correct |
7 ms |
3892 KB |
Output is correct |
45 |
Correct |
12 ms |
3892 KB |
Output is correct |
46 |
Correct |
7 ms |
3892 KB |
Output is correct |
47 |
Correct |
6 ms |
3892 KB |
Output is correct |
48 |
Correct |
7 ms |
3964 KB |
Output is correct |
49 |
Correct |
7 ms |
4044 KB |
Output is correct |
50 |
Correct |
9 ms |
4044 KB |
Output is correct |
51 |
Correct |
7 ms |
4076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
4892 KB |
Output is correct |
2 |
Correct |
14 ms |
4892 KB |
Output is correct |
3 |
Correct |
20 ms |
5148 KB |
Output is correct |
4 |
Correct |
24 ms |
5148 KB |
Output is correct |
5 |
Correct |
16 ms |
5524 KB |
Output is correct |
6 |
Correct |
22 ms |
5524 KB |
Output is correct |
7 |
Correct |
17 ms |
6092 KB |
Output is correct |
8 |
Correct |
14 ms |
6092 KB |
Output is correct |
9 |
Correct |
16 ms |
6524 KB |
Output is correct |
10 |
Correct |
16 ms |
6524 KB |
Output is correct |
11 |
Correct |
19 ms |
7072 KB |
Output is correct |
12 |
Correct |
16 ms |
7072 KB |
Output is correct |
13 |
Correct |
15 ms |
7436 KB |
Output is correct |
14 |
Correct |
15 ms |
7768 KB |
Output is correct |
15 |
Correct |
14 ms |
7768 KB |
Output is correct |
16 |
Correct |
15 ms |
8320 KB |
Output is correct |
17 |
Correct |
14 ms |
8320 KB |
Output is correct |
18 |
Correct |
15 ms |
8624 KB |
Output is correct |
19 |
Correct |
15 ms |
8624 KB |
Output is correct |
20 |
Correct |
15 ms |
9124 KB |
Output is correct |
21 |
Correct |
22 ms |
9124 KB |
Output is correct |
22 |
Correct |
15 ms |
9624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3572 KB |
Output is correct |
3 |
Correct |
4 ms |
3588 KB |
Output is correct |
4 |
Correct |
5 ms |
3588 KB |
Output is correct |
5 |
Correct |
5 ms |
3588 KB |
Output is correct |
6 |
Correct |
4 ms |
3588 KB |
Output is correct |
7 |
Correct |
5 ms |
3588 KB |
Output is correct |
8 |
Correct |
5 ms |
3604 KB |
Output is correct |
9 |
Correct |
4 ms |
3604 KB |
Output is correct |
10 |
Correct |
4 ms |
3604 KB |
Output is correct |
11 |
Correct |
4 ms |
3648 KB |
Output is correct |
12 |
Correct |
4 ms |
3648 KB |
Output is correct |
13 |
Correct |
5 ms |
3648 KB |
Output is correct |
14 |
Correct |
4 ms |
3648 KB |
Output is correct |
15 |
Correct |
5 ms |
3648 KB |
Output is correct |
16 |
Correct |
8 ms |
3648 KB |
Output is correct |
17 |
Correct |
5 ms |
3648 KB |
Output is correct |
18 |
Correct |
4 ms |
3684 KB |
Output is correct |
19 |
Correct |
5 ms |
3684 KB |
Output is correct |
20 |
Correct |
5 ms |
3684 KB |
Output is correct |
21 |
Correct |
4 ms |
3684 KB |
Output is correct |
22 |
Correct |
5 ms |
3684 KB |
Output is correct |
23 |
Correct |
4 ms |
3684 KB |
Output is correct |
24 |
Correct |
4 ms |
3684 KB |
Output is correct |
25 |
Correct |
5 ms |
3684 KB |
Output is correct |
26 |
Correct |
6 ms |
3836 KB |
Output is correct |
27 |
Correct |
11 ms |
3840 KB |
Output is correct |
28 |
Correct |
7 ms |
3840 KB |
Output is correct |
29 |
Correct |
18 ms |
3840 KB |
Output is correct |
30 |
Correct |
7 ms |
3840 KB |
Output is correct |
31 |
Correct |
13 ms |
3892 KB |
Output is correct |
32 |
Correct |
9 ms |
3892 KB |
Output is correct |
33 |
Correct |
12 ms |
3892 KB |
Output is correct |
34 |
Correct |
7 ms |
3892 KB |
Output is correct |
35 |
Correct |
14 ms |
3892 KB |
Output is correct |
36 |
Correct |
15 ms |
3892 KB |
Output is correct |
37 |
Correct |
12 ms |
3892 KB |
Output is correct |
38 |
Correct |
7 ms |
3892 KB |
Output is correct |
39 |
Correct |
12 ms |
3892 KB |
Output is correct |
40 |
Correct |
7 ms |
3892 KB |
Output is correct |
41 |
Correct |
12 ms |
3892 KB |
Output is correct |
42 |
Correct |
7 ms |
3892 KB |
Output is correct |
43 |
Correct |
11 ms |
3892 KB |
Output is correct |
44 |
Correct |
7 ms |
3892 KB |
Output is correct |
45 |
Correct |
12 ms |
3892 KB |
Output is correct |
46 |
Correct |
7 ms |
3892 KB |
Output is correct |
47 |
Correct |
6 ms |
3892 KB |
Output is correct |
48 |
Correct |
7 ms |
3964 KB |
Output is correct |
49 |
Correct |
7 ms |
4044 KB |
Output is correct |
50 |
Correct |
9 ms |
4044 KB |
Output is correct |
51 |
Correct |
7 ms |
4076 KB |
Output is correct |
52 |
Correct |
16 ms |
4892 KB |
Output is correct |
53 |
Correct |
14 ms |
4892 KB |
Output is correct |
54 |
Correct |
20 ms |
5148 KB |
Output is correct |
55 |
Correct |
24 ms |
5148 KB |
Output is correct |
56 |
Correct |
16 ms |
5524 KB |
Output is correct |
57 |
Correct |
22 ms |
5524 KB |
Output is correct |
58 |
Correct |
17 ms |
6092 KB |
Output is correct |
59 |
Correct |
14 ms |
6092 KB |
Output is correct |
60 |
Correct |
16 ms |
6524 KB |
Output is correct |
61 |
Correct |
16 ms |
6524 KB |
Output is correct |
62 |
Correct |
19 ms |
7072 KB |
Output is correct |
63 |
Correct |
16 ms |
7072 KB |
Output is correct |
64 |
Correct |
15 ms |
7436 KB |
Output is correct |
65 |
Correct |
15 ms |
7768 KB |
Output is correct |
66 |
Correct |
14 ms |
7768 KB |
Output is correct |
67 |
Correct |
15 ms |
8320 KB |
Output is correct |
68 |
Correct |
14 ms |
8320 KB |
Output is correct |
69 |
Correct |
15 ms |
8624 KB |
Output is correct |
70 |
Correct |
15 ms |
8624 KB |
Output is correct |
71 |
Correct |
15 ms |
9124 KB |
Output is correct |
72 |
Correct |
22 ms |
9124 KB |
Output is correct |
73 |
Correct |
15 ms |
9624 KB |
Output is correct |
74 |
Correct |
50 ms |
12216 KB |
Output is correct |
75 |
Correct |
387 ms |
13168 KB |
Output is correct |
76 |
Correct |
47 ms |
14032 KB |
Output is correct |
77 |
Correct |
444 ms |
14820 KB |
Output is correct |
78 |
Correct |
53 ms |
15632 KB |
Output is correct |
79 |
Correct |
52 ms |
16440 KB |
Output is correct |
80 |
Correct |
371 ms |
17252 KB |
Output is correct |
81 |
Correct |
47 ms |
18060 KB |
Output is correct |
82 |
Correct |
35 ms |
18864 KB |
Output is correct |
83 |
Correct |
45 ms |
19672 KB |
Output is correct |
84 |
Correct |
378 ms |
20500 KB |
Output is correct |
85 |
Correct |
47 ms |
21308 KB |
Output is correct |
86 |
Correct |
406 ms |
22248 KB |
Output is correct |
87 |
Correct |
44 ms |
22928 KB |
Output is correct |
88 |
Correct |
369 ms |
23836 KB |
Output is correct |
89 |
Correct |
45 ms |
24568 KB |
Output is correct |
90 |
Correct |
391 ms |
25604 KB |
Output is correct |
91 |
Correct |
47 ms |
26168 KB |
Output is correct |
92 |
Correct |
398 ms |
27072 KB |
Output is correct |
93 |
Correct |
63 ms |
27804 KB |
Output is correct |
94 |
Correct |
39 ms |
28068 KB |
Output is correct |
95 |
Correct |
57 ms |
29556 KB |
Output is correct |
96 |
Correct |
47 ms |
30348 KB |
Output is correct |
97 |
Correct |
47 ms |
31084 KB |
Output is correct |
98 |
Correct |
81 ms |
31860 KB |
Output is correct |