#include <bits/stdc++.h>
using namespace std;
const int N = 30005;
int n, m, sum;
int a[2 * N];
int deg[4 * N];
int par[4 * N];
int b[20 * N];
int f[20 * N];
bool vis[4 * N];
vector<int> G[4 * N];
vector<int> vec[4 * N];
int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }
void join(int u, int v) { par[find(u)] = find(v); }
void add(int u, int v) {
// cout << u << ' ' << v << '\n';
deg[u]++, deg[v]++, G[u].push_back(v), G[v].push_back(u), join(u, v);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= 4 * n; ++i) par[i] = i;
for (int i = 1; i <= 2 * n; ++i) {
int l, r; cin >> l >> r >> a[i];
add(i, l + 2 * n), add(i, r + 3 * n);
}
for (int i = 1; i <= 4 * n; ++i) vec[find(i)].push_back(i);
for (int i = 1; i <= 4 * n; ++i) {
if (i != find(i)) continue;
int cnt = 0;
for (auto j : vec[i]) cnt += deg[j];
if (cnt != vec[i].size() * 2) {
cout << "NO"; return 0;
}
}
queue<int> qu;
for (int i = 1; i <= 4 * n; ++i) {
if (deg[i] == 1) qu.push(i);
}
while (qu.size()) {
int u = qu.front(); qu.pop();
for (auto v : G[u]) {
deg[v]--;
if (deg[v] == 1) {
if (v <= n * 2) {
if (u > 3 * n) sum += a[v];
else sum -= a[v];
}
qu.push(v);
}
}
}
for (int i = 1; i <= 2 * n; ++i) {
if (deg[i] != 2 || vis[i]) continue;
int cur = i;
vector<int> cir;
while (1) {
bool found = 0;
vis[cur] = 1, cir.push_back(cur);
for (auto j : G[cur]) {
if (!vis[j] && deg[j] == 2) {
cur = j, found = 1;
}
}
if (!found) break;
}
int tmp = 0;
for (int j = 0; j < cir.size(); j += 2) {
if (j % 4 == 0) tmp += a[cir[j]];
else tmp -= a[cir[j]];
}
if (tmp < 0) tmp = -tmp;
// cout << tmp << '\n';
sum += tmp, b[tmp]++;
}
f[0] = 1;
int lim = 0;
for (int i = 1; i <= 20 * n; ++i) {
if (!b[i]) continue;
for (int j = 0; j <= lim; ++j) {
if (f[j]) f[j] = b[i] + 1;
}
lim += b[i] * i;
for (int j = 0; j <= lim - i; ++j) {
f[j + i] = max(f[j + i], f[j] - 1);
}
}
for (int i = 0; i <= lim; ++i) {
// if (f[i]) cout << i << '\n';
if (f[i] && abs(sum - 2 * i) <= m) {
cout << "YES"; return 0;
}
}
cout << "NO";
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:39:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (cnt != vec[i].size() * 2) {
~~~~^~~~~~~~~~~~~~~~~~~~
tug.cpp:75:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < cir.size(); j += 2) {
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6008 KB |
Output is correct |
2 |
Correct |
8 ms |
6120 KB |
Output is correct |
3 |
Correct |
8 ms |
6120 KB |
Output is correct |
4 |
Correct |
7 ms |
6228 KB |
Output is correct |
5 |
Correct |
7 ms |
6228 KB |
Output is correct |
6 |
Correct |
7 ms |
6228 KB |
Output is correct |
7 |
Correct |
8 ms |
6348 KB |
Output is correct |
8 |
Correct |
7 ms |
6348 KB |
Output is correct |
9 |
Correct |
7 ms |
6348 KB |
Output is correct |
10 |
Correct |
8 ms |
6540 KB |
Output is correct |
11 |
Correct |
7 ms |
6540 KB |
Output is correct |
12 |
Correct |
8 ms |
6540 KB |
Output is correct |
13 |
Correct |
8 ms |
6540 KB |
Output is correct |
14 |
Correct |
7 ms |
6540 KB |
Output is correct |
15 |
Correct |
8 ms |
6548 KB |
Output is correct |
16 |
Correct |
14 ms |
6548 KB |
Output is correct |
17 |
Correct |
9 ms |
6548 KB |
Output is correct |
18 |
Correct |
7 ms |
6548 KB |
Output is correct |
19 |
Correct |
7 ms |
6548 KB |
Output is correct |
20 |
Correct |
8 ms |
6568 KB |
Output is correct |
21 |
Correct |
16 ms |
6568 KB |
Output is correct |
22 |
Correct |
7 ms |
6568 KB |
Output is correct |
23 |
Correct |
7 ms |
6568 KB |
Output is correct |
24 |
Correct |
10 ms |
6568 KB |
Output is correct |
25 |
Correct |
9 ms |
6568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6008 KB |
Output is correct |
2 |
Correct |
8 ms |
6120 KB |
Output is correct |
3 |
Correct |
8 ms |
6120 KB |
Output is correct |
4 |
Correct |
7 ms |
6228 KB |
Output is correct |
5 |
Correct |
7 ms |
6228 KB |
Output is correct |
6 |
Correct |
7 ms |
6228 KB |
Output is correct |
7 |
Correct |
8 ms |
6348 KB |
Output is correct |
8 |
Correct |
7 ms |
6348 KB |
Output is correct |
9 |
Correct |
7 ms |
6348 KB |
Output is correct |
10 |
Correct |
8 ms |
6540 KB |
Output is correct |
11 |
Correct |
7 ms |
6540 KB |
Output is correct |
12 |
Correct |
8 ms |
6540 KB |
Output is correct |
13 |
Correct |
8 ms |
6540 KB |
Output is correct |
14 |
Correct |
7 ms |
6540 KB |
Output is correct |
15 |
Correct |
8 ms |
6548 KB |
Output is correct |
16 |
Correct |
14 ms |
6548 KB |
Output is correct |
17 |
Correct |
9 ms |
6548 KB |
Output is correct |
18 |
Correct |
7 ms |
6548 KB |
Output is correct |
19 |
Correct |
7 ms |
6548 KB |
Output is correct |
20 |
Correct |
8 ms |
6568 KB |
Output is correct |
21 |
Correct |
16 ms |
6568 KB |
Output is correct |
22 |
Correct |
7 ms |
6568 KB |
Output is correct |
23 |
Correct |
7 ms |
6568 KB |
Output is correct |
24 |
Correct |
10 ms |
6568 KB |
Output is correct |
25 |
Correct |
9 ms |
6568 KB |
Output is correct |
26 |
Correct |
12 ms |
6980 KB |
Output is correct |
27 |
Correct |
16 ms |
7156 KB |
Output is correct |
28 |
Correct |
13 ms |
7156 KB |
Output is correct |
29 |
Correct |
24 ms |
7156 KB |
Output is correct |
30 |
Correct |
13 ms |
7204 KB |
Output is correct |
31 |
Correct |
18 ms |
7284 KB |
Output is correct |
32 |
Correct |
13 ms |
7316 KB |
Output is correct |
33 |
Correct |
19 ms |
7492 KB |
Output is correct |
34 |
Correct |
16 ms |
7492 KB |
Output is correct |
35 |
Correct |
20 ms |
7584 KB |
Output is correct |
36 |
Correct |
14 ms |
7584 KB |
Output is correct |
37 |
Correct |
18 ms |
7764 KB |
Output is correct |
38 |
Correct |
14 ms |
7764 KB |
Output is correct |
39 |
Correct |
18 ms |
7792 KB |
Output is correct |
40 |
Correct |
11 ms |
7792 KB |
Output is correct |
41 |
Correct |
15 ms |
7792 KB |
Output is correct |
42 |
Correct |
10 ms |
7808 KB |
Output is correct |
43 |
Correct |
16 ms |
7856 KB |
Output is correct |
44 |
Correct |
11 ms |
7904 KB |
Output is correct |
45 |
Correct |
17 ms |
8080 KB |
Output is correct |
46 |
Correct |
15 ms |
8080 KB |
Output is correct |
47 |
Correct |
10 ms |
8080 KB |
Output is correct |
48 |
Correct |
12 ms |
8096 KB |
Output is correct |
49 |
Correct |
12 ms |
8104 KB |
Output is correct |
50 |
Correct |
11 ms |
8244 KB |
Output is correct |
51 |
Correct |
11 ms |
8368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
9952 KB |
Output is correct |
2 |
Correct |
22 ms |
10092 KB |
Output is correct |
3 |
Correct |
41 ms |
10588 KB |
Output is correct |
4 |
Correct |
25 ms |
10608 KB |
Output is correct |
5 |
Correct |
32 ms |
10968 KB |
Output is correct |
6 |
Correct |
31 ms |
11092 KB |
Output is correct |
7 |
Correct |
41 ms |
11344 KB |
Output is correct |
8 |
Correct |
31 ms |
11472 KB |
Output is correct |
9 |
Correct |
42 ms |
11820 KB |
Output is correct |
10 |
Correct |
38 ms |
12040 KB |
Output is correct |
11 |
Correct |
41 ms |
12400 KB |
Output is correct |
12 |
Correct |
29 ms |
12408 KB |
Output is correct |
13 |
Correct |
39 ms |
12904 KB |
Output is correct |
14 |
Correct |
38 ms |
13096 KB |
Output is correct |
15 |
Correct |
28 ms |
13096 KB |
Output is correct |
16 |
Correct |
49 ms |
13448 KB |
Output is correct |
17 |
Correct |
28 ms |
13576 KB |
Output is correct |
18 |
Correct |
32 ms |
13944 KB |
Output is correct |
19 |
Correct |
28 ms |
14096 KB |
Output is correct |
20 |
Correct |
37 ms |
14464 KB |
Output is correct |
21 |
Correct |
31 ms |
14596 KB |
Output is correct |
22 |
Correct |
32 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6008 KB |
Output is correct |
2 |
Correct |
8 ms |
6120 KB |
Output is correct |
3 |
Correct |
8 ms |
6120 KB |
Output is correct |
4 |
Correct |
7 ms |
6228 KB |
Output is correct |
5 |
Correct |
7 ms |
6228 KB |
Output is correct |
6 |
Correct |
7 ms |
6228 KB |
Output is correct |
7 |
Correct |
8 ms |
6348 KB |
Output is correct |
8 |
Correct |
7 ms |
6348 KB |
Output is correct |
9 |
Correct |
7 ms |
6348 KB |
Output is correct |
10 |
Correct |
8 ms |
6540 KB |
Output is correct |
11 |
Correct |
7 ms |
6540 KB |
Output is correct |
12 |
Correct |
8 ms |
6540 KB |
Output is correct |
13 |
Correct |
8 ms |
6540 KB |
Output is correct |
14 |
Correct |
7 ms |
6540 KB |
Output is correct |
15 |
Correct |
8 ms |
6548 KB |
Output is correct |
16 |
Correct |
14 ms |
6548 KB |
Output is correct |
17 |
Correct |
9 ms |
6548 KB |
Output is correct |
18 |
Correct |
7 ms |
6548 KB |
Output is correct |
19 |
Correct |
7 ms |
6548 KB |
Output is correct |
20 |
Correct |
8 ms |
6568 KB |
Output is correct |
21 |
Correct |
16 ms |
6568 KB |
Output is correct |
22 |
Correct |
7 ms |
6568 KB |
Output is correct |
23 |
Correct |
7 ms |
6568 KB |
Output is correct |
24 |
Correct |
10 ms |
6568 KB |
Output is correct |
25 |
Correct |
9 ms |
6568 KB |
Output is correct |
26 |
Correct |
12 ms |
6980 KB |
Output is correct |
27 |
Correct |
16 ms |
7156 KB |
Output is correct |
28 |
Correct |
13 ms |
7156 KB |
Output is correct |
29 |
Correct |
24 ms |
7156 KB |
Output is correct |
30 |
Correct |
13 ms |
7204 KB |
Output is correct |
31 |
Correct |
18 ms |
7284 KB |
Output is correct |
32 |
Correct |
13 ms |
7316 KB |
Output is correct |
33 |
Correct |
19 ms |
7492 KB |
Output is correct |
34 |
Correct |
16 ms |
7492 KB |
Output is correct |
35 |
Correct |
20 ms |
7584 KB |
Output is correct |
36 |
Correct |
14 ms |
7584 KB |
Output is correct |
37 |
Correct |
18 ms |
7764 KB |
Output is correct |
38 |
Correct |
14 ms |
7764 KB |
Output is correct |
39 |
Correct |
18 ms |
7792 KB |
Output is correct |
40 |
Correct |
11 ms |
7792 KB |
Output is correct |
41 |
Correct |
15 ms |
7792 KB |
Output is correct |
42 |
Correct |
10 ms |
7808 KB |
Output is correct |
43 |
Correct |
16 ms |
7856 KB |
Output is correct |
44 |
Correct |
11 ms |
7904 KB |
Output is correct |
45 |
Correct |
17 ms |
8080 KB |
Output is correct |
46 |
Correct |
15 ms |
8080 KB |
Output is correct |
47 |
Correct |
10 ms |
8080 KB |
Output is correct |
48 |
Correct |
12 ms |
8096 KB |
Output is correct |
49 |
Correct |
12 ms |
8104 KB |
Output is correct |
50 |
Correct |
11 ms |
8244 KB |
Output is correct |
51 |
Correct |
11 ms |
8368 KB |
Output is correct |
52 |
Correct |
30 ms |
9952 KB |
Output is correct |
53 |
Correct |
22 ms |
10092 KB |
Output is correct |
54 |
Correct |
41 ms |
10588 KB |
Output is correct |
55 |
Correct |
25 ms |
10608 KB |
Output is correct |
56 |
Correct |
32 ms |
10968 KB |
Output is correct |
57 |
Correct |
31 ms |
11092 KB |
Output is correct |
58 |
Correct |
41 ms |
11344 KB |
Output is correct |
59 |
Correct |
31 ms |
11472 KB |
Output is correct |
60 |
Correct |
42 ms |
11820 KB |
Output is correct |
61 |
Correct |
38 ms |
12040 KB |
Output is correct |
62 |
Correct |
41 ms |
12400 KB |
Output is correct |
63 |
Correct |
29 ms |
12408 KB |
Output is correct |
64 |
Correct |
39 ms |
12904 KB |
Output is correct |
65 |
Correct |
38 ms |
13096 KB |
Output is correct |
66 |
Correct |
28 ms |
13096 KB |
Output is correct |
67 |
Correct |
49 ms |
13448 KB |
Output is correct |
68 |
Correct |
28 ms |
13576 KB |
Output is correct |
69 |
Correct |
32 ms |
13944 KB |
Output is correct |
70 |
Correct |
28 ms |
14096 KB |
Output is correct |
71 |
Correct |
37 ms |
14464 KB |
Output is correct |
72 |
Correct |
31 ms |
14596 KB |
Output is correct |
73 |
Correct |
32 ms |
14956 KB |
Output is correct |
74 |
Correct |
120 ms |
20400 KB |
Output is correct |
75 |
Correct |
581 ms |
22604 KB |
Output is correct |
76 |
Correct |
120 ms |
22604 KB |
Output is correct |
77 |
Correct |
647 ms |
24148 KB |
Output is correct |
78 |
Correct |
133 ms |
24148 KB |
Output is correct |
79 |
Correct |
139 ms |
24572 KB |
Output is correct |
80 |
Correct |
415 ms |
26556 KB |
Output is correct |
81 |
Correct |
97 ms |
27020 KB |
Output is correct |
82 |
Correct |
64 ms |
28208 KB |
Output is correct |
83 |
Correct |
105 ms |
28208 KB |
Output is correct |
84 |
Correct |
415 ms |
29772 KB |
Output is correct |
85 |
Correct |
96 ms |
29772 KB |
Output is correct |
86 |
Correct |
409 ms |
31448 KB |
Output is correct |
87 |
Correct |
102 ms |
31448 KB |
Output is correct |
88 |
Correct |
403 ms |
33216 KB |
Output is correct |
89 |
Correct |
107 ms |
33216 KB |
Output is correct |
90 |
Correct |
403 ms |
34692 KB |
Output is correct |
91 |
Correct |
108 ms |
34692 KB |
Output is correct |
92 |
Correct |
410 ms |
36512 KB |
Output is correct |
93 |
Correct |
125 ms |
36512 KB |
Output is correct |
94 |
Correct |
78 ms |
36512 KB |
Output is correct |
95 |
Correct |
167 ms |
37288 KB |
Output is correct |
96 |
Correct |
152 ms |
38148 KB |
Output is correct |
97 |
Correct |
153 ms |
40716 KB |
Output is correct |
98 |
Correct |
104 ms |
41368 KB |
Output is correct |