#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define all(v) begin(v), end(v)
struct edge {
int to, cost;
edge(int t, int c) : to(t), cost(c) {}
};
int main() {
int n, k; cin >> n >> k;
vector<vector<edge>> g(2 * n);
rep (i, 2 * n) {
int l, r, s; cin >> l >> r >> s;
g[--l].emplace_back(--r + n, s);
g[r + n].emplace_back(l, s);
}
vector<bool> used(2 * n, false);
int sm = 0;
{
vector<int> deg(2 * n);
queue<int> que;
rep (i, 2 * n) {
deg[i] = g[i].size();
if (deg[i] == 1) que.push(i);
if (deg[i] == 0) {
puts("NO");
return 0;
}
}
while (!que.empty()) {
int v = que.front(); que.pop();
if (deg[v] != 1) {
puts("NO");
return 0;
}
used[v] = true;
for (auto e : g[v]) {
if (used[e.to]) continue;
if (v < n) sm += e.cost;
else sm -= e.cost;
--deg[e.to];
if (deg[e.to] == 1) que.push(e.to);
}
}
}
vector<int> a;
rep (i, 2 * n) {
if (used[i]) continue;
used[i] = true;
int tmp = 0, sgn = 1;
int v = -1;
for (auto e : g[i]) {
if (!used[e.to]) {
if (v == -1) {
v = e.to;
tmp += e.cost;
}
else {
tmp -= e.cost;
}
}
}
sgn = -sgn;
while (v != i) {
used[v] = true;
bool flag = true;
for (auto e : g[v]) {
if (!used[e.to]) {
v = e.to;
tmp += e.cost * sgn;
sgn = -sgn;
flag = false;
break;
}
}
if (flag) break;
}
tmp = abs(tmp);
sm -= tmp;
a.push_back(tmp * 2);
}
{
map<int, int> mp;
for (auto i : a) ++mp[i];
vector<int> b;
while (!mp.empty()) {
auto [k, v] = *mp.begin(); mp.erase(mp.begin());
int t = (v - 1) / 2;
if (t > 0) mp[k * 2] += t;
v -= t * 2;
rep (_, v) b.push_back(k);
}
a = move(b);
}
bitset<1200001> dp(1);
for (auto x : a) dp |= dp << x;
bool f = false;
rep2 (i, max(0, -k - sm), k - sm + 1) {
if (dp[i]) f = true;
}
puts(f ? "YES" : "NO");
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
672 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
0 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
672 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
0 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
6 ms |
1108 KB |
Output is correct |
27 |
Correct |
11 ms |
860 KB |
Output is correct |
28 |
Correct |
6 ms |
860 KB |
Output is correct |
29 |
Correct |
11 ms |
860 KB |
Output is correct |
30 |
Correct |
6 ms |
860 KB |
Output is correct |
31 |
Correct |
11 ms |
860 KB |
Output is correct |
32 |
Correct |
6 ms |
860 KB |
Output is correct |
33 |
Correct |
11 ms |
1108 KB |
Output is correct |
34 |
Correct |
4 ms |
860 KB |
Output is correct |
35 |
Correct |
11 ms |
860 KB |
Output is correct |
36 |
Correct |
6 ms |
860 KB |
Output is correct |
37 |
Correct |
11 ms |
1104 KB |
Output is correct |
38 |
Correct |
6 ms |
860 KB |
Output is correct |
39 |
Correct |
11 ms |
860 KB |
Output is correct |
40 |
Correct |
6 ms |
860 KB |
Output is correct |
41 |
Correct |
11 ms |
1104 KB |
Output is correct |
42 |
Correct |
9 ms |
860 KB |
Output is correct |
43 |
Correct |
11 ms |
860 KB |
Output is correct |
44 |
Correct |
6 ms |
860 KB |
Output is correct |
45 |
Correct |
11 ms |
1104 KB |
Output is correct |
46 |
Correct |
6 ms |
1104 KB |
Output is correct |
47 |
Correct |
3 ms |
1112 KB |
Output is correct |
48 |
Correct |
5 ms |
1112 KB |
Output is correct |
49 |
Correct |
6 ms |
1116 KB |
Output is correct |
50 |
Correct |
4 ms |
1028 KB |
Output is correct |
51 |
Correct |
4 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1884 KB |
Output is correct |
2 |
Correct |
11 ms |
2000 KB |
Output is correct |
3 |
Correct |
11 ms |
1996 KB |
Output is correct |
4 |
Correct |
11 ms |
2140 KB |
Output is correct |
5 |
Correct |
13 ms |
1884 KB |
Output is correct |
6 |
Correct |
15 ms |
2140 KB |
Output is correct |
7 |
Correct |
12 ms |
2004 KB |
Output is correct |
8 |
Correct |
11 ms |
2092 KB |
Output is correct |
9 |
Correct |
12 ms |
2060 KB |
Output is correct |
10 |
Correct |
11 ms |
2140 KB |
Output is correct |
11 |
Correct |
12 ms |
2052 KB |
Output is correct |
12 |
Correct |
11 ms |
2140 KB |
Output is correct |
13 |
Correct |
12 ms |
1924 KB |
Output is correct |
14 |
Correct |
12 ms |
1884 KB |
Output is correct |
15 |
Correct |
12 ms |
2140 KB |
Output is correct |
16 |
Correct |
12 ms |
1880 KB |
Output is correct |
17 |
Correct |
12 ms |
2140 KB |
Output is correct |
18 |
Correct |
12 ms |
1884 KB |
Output is correct |
19 |
Correct |
11 ms |
2140 KB |
Output is correct |
20 |
Correct |
12 ms |
1884 KB |
Output is correct |
21 |
Correct |
11 ms |
1884 KB |
Output is correct |
22 |
Correct |
12 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
672 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
0 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
6 ms |
1108 KB |
Output is correct |
27 |
Correct |
11 ms |
860 KB |
Output is correct |
28 |
Correct |
6 ms |
860 KB |
Output is correct |
29 |
Correct |
11 ms |
860 KB |
Output is correct |
30 |
Correct |
6 ms |
860 KB |
Output is correct |
31 |
Correct |
11 ms |
860 KB |
Output is correct |
32 |
Correct |
6 ms |
860 KB |
Output is correct |
33 |
Correct |
11 ms |
1108 KB |
Output is correct |
34 |
Correct |
4 ms |
860 KB |
Output is correct |
35 |
Correct |
11 ms |
860 KB |
Output is correct |
36 |
Correct |
6 ms |
860 KB |
Output is correct |
37 |
Correct |
11 ms |
1104 KB |
Output is correct |
38 |
Correct |
6 ms |
860 KB |
Output is correct |
39 |
Correct |
11 ms |
860 KB |
Output is correct |
40 |
Correct |
6 ms |
860 KB |
Output is correct |
41 |
Correct |
11 ms |
1104 KB |
Output is correct |
42 |
Correct |
9 ms |
860 KB |
Output is correct |
43 |
Correct |
11 ms |
860 KB |
Output is correct |
44 |
Correct |
6 ms |
860 KB |
Output is correct |
45 |
Correct |
11 ms |
1104 KB |
Output is correct |
46 |
Correct |
6 ms |
1104 KB |
Output is correct |
47 |
Correct |
3 ms |
1112 KB |
Output is correct |
48 |
Correct |
5 ms |
1112 KB |
Output is correct |
49 |
Correct |
6 ms |
1116 KB |
Output is correct |
50 |
Correct |
4 ms |
1028 KB |
Output is correct |
51 |
Correct |
4 ms |
860 KB |
Output is correct |
52 |
Correct |
12 ms |
1884 KB |
Output is correct |
53 |
Correct |
11 ms |
2000 KB |
Output is correct |
54 |
Correct |
11 ms |
1996 KB |
Output is correct |
55 |
Correct |
11 ms |
2140 KB |
Output is correct |
56 |
Correct |
13 ms |
1884 KB |
Output is correct |
57 |
Correct |
15 ms |
2140 KB |
Output is correct |
58 |
Correct |
12 ms |
2004 KB |
Output is correct |
59 |
Correct |
11 ms |
2092 KB |
Output is correct |
60 |
Correct |
12 ms |
2060 KB |
Output is correct |
61 |
Correct |
11 ms |
2140 KB |
Output is correct |
62 |
Correct |
12 ms |
2052 KB |
Output is correct |
63 |
Correct |
11 ms |
2140 KB |
Output is correct |
64 |
Correct |
12 ms |
1924 KB |
Output is correct |
65 |
Correct |
12 ms |
1884 KB |
Output is correct |
66 |
Correct |
12 ms |
2140 KB |
Output is correct |
67 |
Correct |
12 ms |
1880 KB |
Output is correct |
68 |
Correct |
12 ms |
2140 KB |
Output is correct |
69 |
Correct |
12 ms |
1884 KB |
Output is correct |
70 |
Correct |
11 ms |
2140 KB |
Output is correct |
71 |
Correct |
12 ms |
1884 KB |
Output is correct |
72 |
Correct |
11 ms |
1884 KB |
Output is correct |
73 |
Correct |
12 ms |
2140 KB |
Output is correct |
74 |
Correct |
41 ms |
4240 KB |
Output is correct |
75 |
Correct |
71 ms |
4200 KB |
Output is correct |
76 |
Correct |
48 ms |
4196 KB |
Output is correct |
77 |
Correct |
73 ms |
4200 KB |
Output is correct |
78 |
Correct |
50 ms |
4252 KB |
Output is correct |
79 |
Correct |
41 ms |
4200 KB |
Output is correct |
80 |
Correct |
73 ms |
4208 KB |
Output is correct |
81 |
Correct |
37 ms |
4304 KB |
Output is correct |
82 |
Correct |
36 ms |
4200 KB |
Output is correct |
83 |
Correct |
41 ms |
4204 KB |
Output is correct |
84 |
Correct |
71 ms |
4360 KB |
Output is correct |
85 |
Correct |
50 ms |
4184 KB |
Output is correct |
86 |
Correct |
70 ms |
4368 KB |
Output is correct |
87 |
Correct |
40 ms |
4200 KB |
Output is correct |
88 |
Correct |
69 ms |
4200 KB |
Output is correct |
89 |
Correct |
41 ms |
4452 KB |
Output is correct |
90 |
Correct |
74 ms |
4688 KB |
Output is correct |
91 |
Correct |
42 ms |
4244 KB |
Output is correct |
92 |
Correct |
70 ms |
4196 KB |
Output is correct |
93 |
Correct |
41 ms |
4208 KB |
Output is correct |
94 |
Correct |
36 ms |
4444 KB |
Output is correct |
95 |
Correct |
46 ms |
4764 KB |
Output is correct |
96 |
Correct |
41 ms |
4696 KB |
Output is correct |
97 |
Correct |
37 ms |
4400 KB |
Output is correct |
98 |
Correct |
37 ms |
4200 KB |
Output is correct |