// In the name of God
#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int N = 3e4 * 2 + 5, M = 3e4 * 17 + 5;
int n, k, l[N], r[N], s[N];
vector<int> adj[N];
int par[N], cntn, vec[N], ver[N], veci, veri;
int cnt[N][2], h[N];
bool vis[N], mark[N], vis2[N];
int _get(int i, int v) {
return l[i] == v ? r[i] : l[i];
}
int dfs3(int v) {
int c = adj[v].size();
cntn++;
vis2[v] = true;
for (auto e : adj[v]) {
int i = e, u = _get(e, v), w = s[e];
if (!vis2[u])
c += dfs3(u);
}
return c;
}
void dfs(int v, int p) {
par[v] = p;
vis[v] = true;
for (auto e : adj[v]) {
int i = e, u = _get(e, v), w = s[e];
if (p == i)
continue;
if (!vis[u]) {
h[u] = h[v] + 1;
dfs(u, i);
}
else if (h[u] < h[v]) {
int u2 = _get(i, v), v2 = v;
ver[veri++] = v;
while (v2 != u2) {
vec[veci++] = par[v2];
v2 = _get(par[v2], v2);
ver[veri++] = v2;
}
vec[veci++] = i;
}
}
}
void dfs2(int v) {
mark[v] = true;
for (auto e : adj[v]) {
int i = e, u = _get(i, v), w = s[e];
if (mark[u])
continue;
dfs2(u);
cnt[v][0] += cnt[u][1];
cnt[v][1] += cnt[u][0] + w;
}
}
bitset<M> dp;
void solve() {
cin >> n >> k;
int S = 0;
int tmpp = 1;
for (int i = 0; i < 2 * n; i++) {
cin >> l[i] >> r[i] >> s[i];
S += s[i];
r[i] += n;
adj[l[i]].pb(i);
adj[r[i]].pb(i);
}
vector<pair<int, int> > V;
for (int i = 1; i <= 2 * n; i++) {
if (!vis[i]) {
veci = 0, veri = 0;
cntn = 0;
int cnte = dfs3(i);
if (cnte != cntn * 2) {
cout << "NO" << '\n';
return;
}
dfs(i, -1);
for (int j = 0; j < veri; j++) {
int u = ver[j];
mark[u] = true;
}
for (int j = 0; j < veri; j++) {
int u = ver[j];
dfs2(u);
}
int sum[2] = {0, 0};
int tmp[2] = {0, 0};
for (int i = 0; i < veci; i++) {
sum[i % 2] += s[vec[i]];
tmp[0] += cnt[ver[i]][i % 2];
tmp[1] += cnt[ver[i]][(i % 2) ^ 1];
}
int x = ver[0] > n;
V.pb(make_pair(tmp[x] + sum[0], tmp[x] + sum[1]));
}
}
dp[0] = true;
for (auto [a, b] : V) {
dp |= (dp << b) | (dp << a);
}
for (int i = 0; i < M; i++) {
if (abs(2 * i - S) <= k && dp[i]) {
cout << "YES" << endl;
return;
}
}
cout << "NO";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
Compilation message
tug.cpp: In function 'int dfs3(int)':
tug.cpp:25:13: warning: unused variable 'i' [-Wunused-variable]
25 | int i = e, u = _get(e, v), w = s[e];
| ^
tug.cpp:25:36: warning: unused variable 'w' [-Wunused-variable]
25 | int i = e, u = _get(e, v), w = s[e];
| ^
tug.cpp: In function 'void dfs(int, int)':
tug.cpp:36:36: warning: unused variable 'w' [-Wunused-variable]
36 | int i = e, u = _get(e, v), w = s[e];
| ^
tug.cpp: In function 'void solve()':
tug.cpp:113:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
113 | for (auto [a, b] : V) {
| ^
tug.cpp:72:9: warning: unused variable 'tmpp' [-Wunused-variable]
72 | int tmpp = 1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
1928 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2004 KB |
Output is correct |
12 |
Correct |
1 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
1924 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
2004 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
2004 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
1 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
1928 KB |
Output is correct |
24 |
Correct |
2 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
1928 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2004 KB |
Output is correct |
12 |
Correct |
1 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
1924 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
2004 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
2004 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
1 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
1928 KB |
Output is correct |
24 |
Correct |
2 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
75 ms |
2304 KB |
Output is correct |
27 |
Correct |
11 ms |
2288 KB |
Output is correct |
28 |
Correct |
62 ms |
2260 KB |
Output is correct |
29 |
Correct |
15 ms |
2196 KB |
Output is correct |
30 |
Correct |
64 ms |
2260 KB |
Output is correct |
31 |
Correct |
13 ms |
2388 KB |
Output is correct |
32 |
Correct |
64 ms |
2264 KB |
Output is correct |
33 |
Correct |
12 ms |
2260 KB |
Output is correct |
34 |
Correct |
7 ms |
2260 KB |
Output is correct |
35 |
Correct |
12 ms |
2196 KB |
Output is correct |
36 |
Correct |
68 ms |
2260 KB |
Output is correct |
37 |
Correct |
11 ms |
2260 KB |
Output is correct |
38 |
Correct |
68 ms |
2288 KB |
Output is correct |
39 |
Correct |
12 ms |
2236 KB |
Output is correct |
40 |
Correct |
66 ms |
2260 KB |
Output is correct |
41 |
Correct |
13 ms |
2296 KB |
Output is correct |
42 |
Correct |
94 ms |
2288 KB |
Output is correct |
43 |
Correct |
12 ms |
2296 KB |
Output is correct |
44 |
Correct |
65 ms |
2300 KB |
Output is correct |
45 |
Correct |
11 ms |
2260 KB |
Output is correct |
46 |
Correct |
65 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2204 KB |
Output is correct |
48 |
Correct |
41 ms |
2444 KB |
Output is correct |
49 |
Correct |
38 ms |
2320 KB |
Output is correct |
50 |
Correct |
81 ms |
2260 KB |
Output is correct |
51 |
Correct |
70 ms |
2288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
344 ms |
3492 KB |
Output is correct |
2 |
Correct |
11 ms |
3028 KB |
Output is correct |
3 |
Correct |
318 ms |
3496 KB |
Output is correct |
4 |
Correct |
8 ms |
2900 KB |
Output is correct |
5 |
Correct |
320 ms |
3492 KB |
Output is correct |
6 |
Correct |
9 ms |
2972 KB |
Output is correct |
7 |
Correct |
328 ms |
3496 KB |
Output is correct |
8 |
Correct |
8 ms |
2948 KB |
Output is correct |
9 |
Correct |
357 ms |
3532 KB |
Output is correct |
10 |
Correct |
7 ms |
3028 KB |
Output is correct |
11 |
Correct |
317 ms |
3492 KB |
Output is correct |
12 |
Correct |
7 ms |
3028 KB |
Output is correct |
13 |
Correct |
321 ms |
3496 KB |
Output is correct |
14 |
Correct |
314 ms |
3496 KB |
Output is correct |
15 |
Correct |
7 ms |
2964 KB |
Output is correct |
16 |
Correct |
329 ms |
3496 KB |
Output is correct |
17 |
Correct |
7 ms |
3028 KB |
Output is correct |
18 |
Correct |
305 ms |
3412 KB |
Output is correct |
19 |
Correct |
7 ms |
3028 KB |
Output is correct |
20 |
Correct |
315 ms |
3508 KB |
Output is correct |
21 |
Correct |
10 ms |
3192 KB |
Output is correct |
22 |
Correct |
166 ms |
3588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2004 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
2 ms |
2004 KB |
Output is correct |
4 |
Correct |
2 ms |
2004 KB |
Output is correct |
5 |
Correct |
2 ms |
1928 KB |
Output is correct |
6 |
Correct |
2 ms |
2004 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2004 KB |
Output is correct |
10 |
Correct |
3 ms |
2004 KB |
Output is correct |
11 |
Correct |
2 ms |
2004 KB |
Output is correct |
12 |
Correct |
1 ms |
2004 KB |
Output is correct |
13 |
Correct |
2 ms |
1924 KB |
Output is correct |
14 |
Correct |
1 ms |
2004 KB |
Output is correct |
15 |
Correct |
2 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
2004 KB |
Output is correct |
17 |
Correct |
2 ms |
2004 KB |
Output is correct |
18 |
Correct |
2 ms |
2004 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
1 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
1928 KB |
Output is correct |
24 |
Correct |
2 ms |
2004 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
75 ms |
2304 KB |
Output is correct |
27 |
Correct |
11 ms |
2288 KB |
Output is correct |
28 |
Correct |
62 ms |
2260 KB |
Output is correct |
29 |
Correct |
15 ms |
2196 KB |
Output is correct |
30 |
Correct |
64 ms |
2260 KB |
Output is correct |
31 |
Correct |
13 ms |
2388 KB |
Output is correct |
32 |
Correct |
64 ms |
2264 KB |
Output is correct |
33 |
Correct |
12 ms |
2260 KB |
Output is correct |
34 |
Correct |
7 ms |
2260 KB |
Output is correct |
35 |
Correct |
12 ms |
2196 KB |
Output is correct |
36 |
Correct |
68 ms |
2260 KB |
Output is correct |
37 |
Correct |
11 ms |
2260 KB |
Output is correct |
38 |
Correct |
68 ms |
2288 KB |
Output is correct |
39 |
Correct |
12 ms |
2236 KB |
Output is correct |
40 |
Correct |
66 ms |
2260 KB |
Output is correct |
41 |
Correct |
13 ms |
2296 KB |
Output is correct |
42 |
Correct |
94 ms |
2288 KB |
Output is correct |
43 |
Correct |
12 ms |
2296 KB |
Output is correct |
44 |
Correct |
65 ms |
2300 KB |
Output is correct |
45 |
Correct |
11 ms |
2260 KB |
Output is correct |
46 |
Correct |
65 ms |
2260 KB |
Output is correct |
47 |
Correct |
3 ms |
2204 KB |
Output is correct |
48 |
Correct |
41 ms |
2444 KB |
Output is correct |
49 |
Correct |
38 ms |
2320 KB |
Output is correct |
50 |
Correct |
81 ms |
2260 KB |
Output is correct |
51 |
Correct |
70 ms |
2288 KB |
Output is correct |
52 |
Correct |
344 ms |
3492 KB |
Output is correct |
53 |
Correct |
11 ms |
3028 KB |
Output is correct |
54 |
Correct |
318 ms |
3496 KB |
Output is correct |
55 |
Correct |
8 ms |
2900 KB |
Output is correct |
56 |
Correct |
320 ms |
3492 KB |
Output is correct |
57 |
Correct |
9 ms |
2972 KB |
Output is correct |
58 |
Correct |
328 ms |
3496 KB |
Output is correct |
59 |
Correct |
8 ms |
2948 KB |
Output is correct |
60 |
Correct |
357 ms |
3532 KB |
Output is correct |
61 |
Correct |
7 ms |
3028 KB |
Output is correct |
62 |
Correct |
317 ms |
3492 KB |
Output is correct |
63 |
Correct |
7 ms |
3028 KB |
Output is correct |
64 |
Correct |
321 ms |
3496 KB |
Output is correct |
65 |
Correct |
314 ms |
3496 KB |
Output is correct |
66 |
Correct |
7 ms |
2964 KB |
Output is correct |
67 |
Correct |
329 ms |
3496 KB |
Output is correct |
68 |
Correct |
7 ms |
3028 KB |
Output is correct |
69 |
Correct |
305 ms |
3412 KB |
Output is correct |
70 |
Correct |
7 ms |
3028 KB |
Output is correct |
71 |
Correct |
315 ms |
3508 KB |
Output is correct |
72 |
Correct |
10 ms |
3192 KB |
Output is correct |
73 |
Correct |
166 ms |
3588 KB |
Output is correct |
74 |
Correct |
932 ms |
6276 KB |
Output is correct |
75 |
Correct |
60 ms |
6048 KB |
Output is correct |
76 |
Correct |
920 ms |
6160 KB |
Output is correct |
77 |
Correct |
61 ms |
5964 KB |
Output is correct |
78 |
Correct |
912 ms |
6276 KB |
Output is correct |
79 |
Correct |
928 ms |
6276 KB |
Output is correct |
80 |
Correct |
69 ms |
5964 KB |
Output is correct |
81 |
Correct |
81 ms |
5964 KB |
Output is correct |
82 |
Correct |
947 ms |
6272 KB |
Output is correct |
83 |
Correct |
930 ms |
6220 KB |
Output is correct |
84 |
Correct |
69 ms |
6040 KB |
Output is correct |
85 |
Correct |
935 ms |
6176 KB |
Output is correct |
86 |
Correct |
62 ms |
5964 KB |
Output is correct |
87 |
Correct |
934 ms |
6276 KB |
Output is correct |
88 |
Correct |
65 ms |
6044 KB |
Output is correct |
89 |
Correct |
919 ms |
6276 KB |
Output is correct |
90 |
Correct |
61 ms |
6052 KB |
Output is correct |
91 |
Correct |
916 ms |
6220 KB |
Output is correct |
92 |
Correct |
62 ms |
6056 KB |
Output is correct |
93 |
Correct |
931 ms |
6280 KB |
Output is correct |
94 |
Correct |
22 ms |
5084 KB |
Output is correct |
95 |
Correct |
485 ms |
6748 KB |
Output is correct |
96 |
Correct |
486 ms |
6744 KB |
Output is correct |
97 |
Correct |
941 ms |
6280 KB |
Output is correct |
98 |
Correct |
937 ms |
6356 KB |
Output is correct |