// _| _|
// _| _| _| _| _|_| _|_|_| _|_|_| _| _| _|_| _|_|
// _|_| _| _| _|_|_|_| _| _| _|_| _|_| _|_|_|_| _|_|_|_|
// _| _| _| _| _| _| _| _|_| _| _| _| _|
// _| _| _|_|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|
// _| _|
// _|_| _|
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
typedef int64_t ll;
int read() {
int n = 0; bool b = 0; char c = getchar();
for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
if (b) return -n;
return n;
}
vec<vec<pair<int, int>>> g;
vec<int> ct;
vec<char> used;
// edges, vertices
pair<int, int> count(int v) {
used[v] = 1;
int se = ct[v], sv = 1;
for (auto [c, _] : g[v]) {
if (used[c]) continue;
auto [ce, cv] = count(c);
se += ce, sv += cv;
}
return {se, sv};
}
int cycle(int v, int p) {
// df("visiting %d\n", v);
used[v] = 1;
int ret = -1;
for (auto [c, _] : g[v]) {
if (c == p) continue;
if (used[c]) { ret = v; continue; }
int res = cycle(c, v);
if (res != -1) ret = res;
}
return ret;
}
pair<int, int> dfs(int v, int p, int val, int costdown) {
used[v] = 1;
int sum = 0, cycle = 1e9;
bool covered = 0;
// df("costdown: %d\n", costdown);
for (auto [c, cost] : g[v]) {
if (c == p && cost == -costdown && !covered) {
covered = 1;
continue;
}
if (used[c]) {
if (v == p) continue;
// cout << "set cycle " << v << " " << c << " " << cost << endl;
cycle = val + cost;
sum += cost;
} else {
auto [ss, cyc] = dfs(c, v, val + cost, cost);
if (cyc != 1e9) cycle = cyc;
sum += ss + cost;
}
}
return {sum, cycle};
}
void fail() {
cout << "NO\n";
exit(0);
}
void succeed() {
cout << "YES\n";
exit(0);
}
int main() {
df("debug mode\n");
#ifndef DEBUG
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
int n, k;
cin >> n >> k;
g.resize(2*n);
ct.resize(2*n);
used.resize(2*n);
foru (i, 2*n) {
int v, u, s;
cin >> v >> u >> s;
u += n;
--v, --u;
df("%d--%d\n", v, u);
g[v].pb({u, s});
g[u].pb({v, -s});
++ct[u], ++ct[v];
}
foru (v, 2*n) {
if (used[v]) continue;
auto [ce, cv] = count(v);
ce /= 2; // in and out counted twice
if (ce != cv) fail();
}
used.assign(2*n, 0);
foru (v, 2*n) {
if (used[v]) continue;
auto [ce, cv] = count(v);
ce /= 2; // in and out counted twice
if (ce != cv) fail();
}
used.assign(2*n, 0);
vec<int> roots;
foru (v, 2*n) {
if (used[v]) continue;
roots.pb(cycle(v, v));
df("added root %d\n", roots.back());
dassert(roots.back() != -1);
}
used.assign(2*n, 0);
int zero = 0;
vec<int> ns;
for (auto root : roots) {
df("root: %d\n", root);
auto [sum, cycle] = dfs(root, root, 0, 0);
// cout << sum << " " << cycle << "\n";
int a = sum, b = sum - 2*cycle;
if (b < a) swap(a, b);
zero -= a, b -= a, a -= a; // moving everything by -a
df("b: %d\n", b);
if (b) ns.pb(b);
}
df("zero: %d\n", zero);
// we want a number between zero - k, zero + k
if (zero - k <= 0 && zero + k >= 0) succeed();
if (zero < 0) fail();
vec<char> dp(zero + k + 1);
dp[0] = 1;
for (auto x : ns) {
// cout << x << endl;
for (int i = zero + k; i > 0; --i) {
if (i - x >= 0 && dp[i - x]) {
dp[i] = 1;
}
}
}
for (int i = zero - k; i <= zero + k; ++i) {
if (dp[i]) succeed();
}
fail();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
31 ms |
596 KB |
Output is correct |
27 |
Correct |
13 ms |
608 KB |
Output is correct |
28 |
Correct |
32 ms |
576 KB |
Output is correct |
29 |
Correct |
14 ms |
596 KB |
Output is correct |
30 |
Correct |
30 ms |
596 KB |
Output is correct |
31 |
Correct |
14 ms |
596 KB |
Output is correct |
32 |
Correct |
38 ms |
604 KB |
Output is correct |
33 |
Correct |
13 ms |
596 KB |
Output is correct |
34 |
Correct |
7 ms |
580 KB |
Output is correct |
35 |
Correct |
18 ms |
604 KB |
Output is correct |
36 |
Correct |
34 ms |
468 KB |
Output is correct |
37 |
Correct |
14 ms |
604 KB |
Output is correct |
38 |
Correct |
35 ms |
596 KB |
Output is correct |
39 |
Correct |
14 ms |
596 KB |
Output is correct |
40 |
Correct |
32 ms |
468 KB |
Output is correct |
41 |
Correct |
13 ms |
592 KB |
Output is correct |
42 |
Correct |
32 ms |
576 KB |
Output is correct |
43 |
Correct |
14 ms |
604 KB |
Output is correct |
44 |
Correct |
32 ms |
468 KB |
Output is correct |
45 |
Correct |
17 ms |
600 KB |
Output is correct |
46 |
Correct |
34 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
2 ms |
468 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
89 ms |
648 KB |
Output is correct |
51 |
Correct |
88 ms |
652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1620 KB |
Output is correct |
2 |
Correct |
7 ms |
1536 KB |
Output is correct |
3 |
Correct |
8 ms |
1620 KB |
Output is correct |
4 |
Correct |
7 ms |
1608 KB |
Output is correct |
5 |
Correct |
8 ms |
1620 KB |
Output is correct |
6 |
Correct |
8 ms |
1596 KB |
Output is correct |
7 |
Correct |
7 ms |
1620 KB |
Output is correct |
8 |
Correct |
7 ms |
1620 KB |
Output is correct |
9 |
Correct |
7 ms |
1620 KB |
Output is correct |
10 |
Correct |
7 ms |
1620 KB |
Output is correct |
11 |
Correct |
9 ms |
1632 KB |
Output is correct |
12 |
Correct |
8 ms |
1620 KB |
Output is correct |
13 |
Correct |
7 ms |
1620 KB |
Output is correct |
14 |
Correct |
8 ms |
1528 KB |
Output is correct |
15 |
Correct |
8 ms |
1576 KB |
Output is correct |
16 |
Correct |
8 ms |
1620 KB |
Output is correct |
17 |
Correct |
7 ms |
1620 KB |
Output is correct |
18 |
Correct |
9 ms |
1620 KB |
Output is correct |
19 |
Correct |
7 ms |
1620 KB |
Output is correct |
20 |
Correct |
8 ms |
1620 KB |
Output is correct |
21 |
Correct |
8 ms |
1620 KB |
Output is correct |
22 |
Correct |
10 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
31 ms |
596 KB |
Output is correct |
27 |
Correct |
13 ms |
608 KB |
Output is correct |
28 |
Correct |
32 ms |
576 KB |
Output is correct |
29 |
Correct |
14 ms |
596 KB |
Output is correct |
30 |
Correct |
30 ms |
596 KB |
Output is correct |
31 |
Correct |
14 ms |
596 KB |
Output is correct |
32 |
Correct |
38 ms |
604 KB |
Output is correct |
33 |
Correct |
13 ms |
596 KB |
Output is correct |
34 |
Correct |
7 ms |
580 KB |
Output is correct |
35 |
Correct |
18 ms |
604 KB |
Output is correct |
36 |
Correct |
34 ms |
468 KB |
Output is correct |
37 |
Correct |
14 ms |
604 KB |
Output is correct |
38 |
Correct |
35 ms |
596 KB |
Output is correct |
39 |
Correct |
14 ms |
596 KB |
Output is correct |
40 |
Correct |
32 ms |
468 KB |
Output is correct |
41 |
Correct |
13 ms |
592 KB |
Output is correct |
42 |
Correct |
32 ms |
576 KB |
Output is correct |
43 |
Correct |
14 ms |
604 KB |
Output is correct |
44 |
Correct |
32 ms |
468 KB |
Output is correct |
45 |
Correct |
17 ms |
600 KB |
Output is correct |
46 |
Correct |
34 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
2 ms |
468 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
89 ms |
648 KB |
Output is correct |
51 |
Correct |
88 ms |
652 KB |
Output is correct |
52 |
Correct |
7 ms |
1620 KB |
Output is correct |
53 |
Correct |
7 ms |
1536 KB |
Output is correct |
54 |
Correct |
8 ms |
1620 KB |
Output is correct |
55 |
Correct |
7 ms |
1608 KB |
Output is correct |
56 |
Correct |
8 ms |
1620 KB |
Output is correct |
57 |
Correct |
8 ms |
1596 KB |
Output is correct |
58 |
Correct |
7 ms |
1620 KB |
Output is correct |
59 |
Correct |
7 ms |
1620 KB |
Output is correct |
60 |
Correct |
7 ms |
1620 KB |
Output is correct |
61 |
Correct |
7 ms |
1620 KB |
Output is correct |
62 |
Correct |
9 ms |
1632 KB |
Output is correct |
63 |
Correct |
8 ms |
1620 KB |
Output is correct |
64 |
Correct |
7 ms |
1620 KB |
Output is correct |
65 |
Correct |
8 ms |
1528 KB |
Output is correct |
66 |
Correct |
8 ms |
1576 KB |
Output is correct |
67 |
Correct |
8 ms |
1620 KB |
Output is correct |
68 |
Correct |
7 ms |
1620 KB |
Output is correct |
69 |
Correct |
9 ms |
1620 KB |
Output is correct |
70 |
Correct |
7 ms |
1620 KB |
Output is correct |
71 |
Correct |
8 ms |
1620 KB |
Output is correct |
72 |
Correct |
8 ms |
1620 KB |
Output is correct |
73 |
Correct |
10 ms |
1620 KB |
Output is correct |
74 |
Execution timed out |
3063 ms |
5180 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |