#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int n, k, pre[120005], vis[120005], match[120005], cnt, ans;
bitset<30000 * 20 * 4> b;
vector<pii> E[120005], T[120005];
pair<pii, int> nT;
vector<int> cV;
bool valid = 1;
void dfs(int u)
{
cnt += (u <= 2 * n ? 1 : -1);
cV.emplace_back(u);
vis[u] = 1;
for (auto [v, w] : E[u])
if (!vis[v])
{
T[u].emplace_back(pii(v, w));
pre[v] = u;
dfs(v);
}
else if (v != pre[u])
nT = make_pair(pii(u, v), w);
}
int matching(int u)
{
int tot = 0;
for (auto [v, w] : T[u])
{
tot += matching(v);
if (!match[u] && !match[v])
match[u] = match[v] = 1, tot += w;
}
return tot;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= 2 * n; i++)
{
int l, r, w;
cin >> l >> r >> w;
E[i].emplace_back(pii(2 * n + l, w));
E[2 * n + l].emplace_back(pii(i, w));
E[i].emplace_back(pii(3 * n + r, -w));
E[3 * n + r].emplace_back(pii(i, -w));
}
b[40 * n + 1] = 1;
for (int i = 1; i <= 2 * n; i++)
if (!vis[i])
{
cV.clear();
dfs(i);
if (cnt != 0)
{
cout << "NO\n";
cerr << "there is no perfect matching (vertex)\n";
return 0;
}
vector<int> sol;
bool mvalid = 1;
int res = matching(i);
for (auto j : cV)
{
mvalid &= match[j];
match[j] = 0;
}
if (mvalid)
sol.emplace_back(res);
match[nT.F.F] = match[nT.F.S] = 1;
mvalid = 1;
res = nT.S + matching(i);
for (auto j : cV)
mvalid &= match[j];
if (mvalid)
sol.emplace_back(res);
if (sol.size() == 0)
{
cout << "NO\n";
cerr << "there is no perfect matching (dfs)\n";
return 0;
}
ans += sol[0];
if (sol.size() == 2)
{
int j = sol[1] - sol[0];
if (j < 0)
b |= b >> (-j);
else
b |= b << j;
}
}
for (int i = 40 * n + 1 - k - ans; i <= 40 * n + 1 + k - ans; i++)
if (b[i])
{
cout << "YES\n";
return 0;
}
cout << "NO\n";
cerr << "k is too small\n";
}
Compilation message
tug.cpp: In function 'void dfs(int)':
tug.cpp:22:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | for (auto [v, w] : E[u])
| ^
tug.cpp: In function 'int matching(int)':
tug.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for (auto [v, w] : T[u])
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6740 KB |
Output is correct |
2 |
Correct |
4 ms |
6740 KB |
Output is correct |
3 |
Correct |
4 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6720 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6812 KB |
Output is correct |
9 |
Correct |
4 ms |
6740 KB |
Output is correct |
10 |
Correct |
4 ms |
6740 KB |
Output is correct |
11 |
Correct |
4 ms |
6740 KB |
Output is correct |
12 |
Correct |
4 ms |
6840 KB |
Output is correct |
13 |
Correct |
4 ms |
6740 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
5 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6824 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
4 ms |
6740 KB |
Output is correct |
21 |
Correct |
4 ms |
6484 KB |
Output is correct |
22 |
Correct |
4 ms |
6740 KB |
Output is correct |
23 |
Correct |
4 ms |
6740 KB |
Output is correct |
24 |
Correct |
4 ms |
6740 KB |
Output is correct |
25 |
Correct |
4 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6740 KB |
Output is correct |
2 |
Correct |
4 ms |
6740 KB |
Output is correct |
3 |
Correct |
4 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6720 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6812 KB |
Output is correct |
9 |
Correct |
4 ms |
6740 KB |
Output is correct |
10 |
Correct |
4 ms |
6740 KB |
Output is correct |
11 |
Correct |
4 ms |
6740 KB |
Output is correct |
12 |
Correct |
4 ms |
6840 KB |
Output is correct |
13 |
Correct |
4 ms |
6740 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
5 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6824 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
4 ms |
6740 KB |
Output is correct |
21 |
Correct |
4 ms |
6484 KB |
Output is correct |
22 |
Correct |
4 ms |
6740 KB |
Output is correct |
23 |
Correct |
4 ms |
6740 KB |
Output is correct |
24 |
Correct |
4 ms |
6740 KB |
Output is correct |
25 |
Correct |
4 ms |
6740 KB |
Output is correct |
26 |
Correct |
111 ms |
7452 KB |
Output is correct |
27 |
Correct |
20 ms |
7412 KB |
Output is correct |
28 |
Correct |
106 ms |
7436 KB |
Output is correct |
29 |
Correct |
19 ms |
7380 KB |
Output is correct |
30 |
Correct |
106 ms |
7336 KB |
Output is correct |
31 |
Correct |
18 ms |
7444 KB |
Output is correct |
32 |
Correct |
109 ms |
7324 KB |
Output is correct |
33 |
Correct |
20 ms |
7380 KB |
Output is correct |
34 |
Correct |
12 ms |
7380 KB |
Output is correct |
35 |
Correct |
18 ms |
7380 KB |
Output is correct |
36 |
Correct |
100 ms |
7472 KB |
Output is correct |
37 |
Correct |
19 ms |
7336 KB |
Output is correct |
38 |
Correct |
107 ms |
7348 KB |
Output is correct |
39 |
Correct |
20 ms |
7416 KB |
Output is correct |
40 |
Correct |
95 ms |
7244 KB |
Output is correct |
41 |
Correct |
19 ms |
7340 KB |
Output is correct |
42 |
Correct |
101 ms |
7252 KB |
Output is correct |
43 |
Correct |
20 ms |
7380 KB |
Output is correct |
44 |
Correct |
107 ms |
7352 KB |
Output is correct |
45 |
Correct |
20 ms |
7384 KB |
Output is correct |
46 |
Correct |
96 ms |
7268 KB |
Output is correct |
47 |
Correct |
6 ms |
7124 KB |
Output is correct |
48 |
Correct |
55 ms |
7288 KB |
Output is correct |
49 |
Correct |
51 ms |
7372 KB |
Output is correct |
50 |
Correct |
107 ms |
7356 KB |
Output is correct |
51 |
Correct |
97 ms |
7336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
9500 KB |
Output is correct |
2 |
Correct |
11 ms |
8020 KB |
Output is correct |
3 |
Correct |
342 ms |
9496 KB |
Output is correct |
4 |
Correct |
11 ms |
8020 KB |
Output is correct |
5 |
Correct |
311 ms |
9512 KB |
Output is correct |
6 |
Correct |
11 ms |
7988 KB |
Output is correct |
7 |
Correct |
291 ms |
9512 KB |
Output is correct |
8 |
Correct |
12 ms |
8096 KB |
Output is correct |
9 |
Correct |
302 ms |
9436 KB |
Output is correct |
10 |
Correct |
11 ms |
7860 KB |
Output is correct |
11 |
Correct |
287 ms |
9460 KB |
Output is correct |
12 |
Correct |
11 ms |
8096 KB |
Output is correct |
13 |
Correct |
300 ms |
9412 KB |
Output is correct |
14 |
Correct |
289 ms |
9392 KB |
Output is correct |
15 |
Correct |
11 ms |
8148 KB |
Output is correct |
16 |
Correct |
351 ms |
9504 KB |
Output is correct |
17 |
Correct |
11 ms |
7892 KB |
Output is correct |
18 |
Correct |
295 ms |
9376 KB |
Output is correct |
19 |
Correct |
12 ms |
8100 KB |
Output is correct |
20 |
Correct |
298 ms |
9368 KB |
Output is correct |
21 |
Correct |
16 ms |
9680 KB |
Output is correct |
22 |
Correct |
157 ms |
9516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6740 KB |
Output is correct |
2 |
Correct |
4 ms |
6740 KB |
Output is correct |
3 |
Correct |
4 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6720 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6812 KB |
Output is correct |
9 |
Correct |
4 ms |
6740 KB |
Output is correct |
10 |
Correct |
4 ms |
6740 KB |
Output is correct |
11 |
Correct |
4 ms |
6740 KB |
Output is correct |
12 |
Correct |
4 ms |
6840 KB |
Output is correct |
13 |
Correct |
4 ms |
6740 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
5 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6824 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
4 ms |
6740 KB |
Output is correct |
21 |
Correct |
4 ms |
6484 KB |
Output is correct |
22 |
Correct |
4 ms |
6740 KB |
Output is correct |
23 |
Correct |
4 ms |
6740 KB |
Output is correct |
24 |
Correct |
4 ms |
6740 KB |
Output is correct |
25 |
Correct |
4 ms |
6740 KB |
Output is correct |
26 |
Correct |
111 ms |
7452 KB |
Output is correct |
27 |
Correct |
20 ms |
7412 KB |
Output is correct |
28 |
Correct |
106 ms |
7436 KB |
Output is correct |
29 |
Correct |
19 ms |
7380 KB |
Output is correct |
30 |
Correct |
106 ms |
7336 KB |
Output is correct |
31 |
Correct |
18 ms |
7444 KB |
Output is correct |
32 |
Correct |
109 ms |
7324 KB |
Output is correct |
33 |
Correct |
20 ms |
7380 KB |
Output is correct |
34 |
Correct |
12 ms |
7380 KB |
Output is correct |
35 |
Correct |
18 ms |
7380 KB |
Output is correct |
36 |
Correct |
100 ms |
7472 KB |
Output is correct |
37 |
Correct |
19 ms |
7336 KB |
Output is correct |
38 |
Correct |
107 ms |
7348 KB |
Output is correct |
39 |
Correct |
20 ms |
7416 KB |
Output is correct |
40 |
Correct |
95 ms |
7244 KB |
Output is correct |
41 |
Correct |
19 ms |
7340 KB |
Output is correct |
42 |
Correct |
101 ms |
7252 KB |
Output is correct |
43 |
Correct |
20 ms |
7380 KB |
Output is correct |
44 |
Correct |
107 ms |
7352 KB |
Output is correct |
45 |
Correct |
20 ms |
7384 KB |
Output is correct |
46 |
Correct |
96 ms |
7268 KB |
Output is correct |
47 |
Correct |
6 ms |
7124 KB |
Output is correct |
48 |
Correct |
55 ms |
7288 KB |
Output is correct |
49 |
Correct |
51 ms |
7372 KB |
Output is correct |
50 |
Correct |
107 ms |
7356 KB |
Output is correct |
51 |
Correct |
97 ms |
7336 KB |
Output is correct |
52 |
Correct |
307 ms |
9500 KB |
Output is correct |
53 |
Correct |
11 ms |
8020 KB |
Output is correct |
54 |
Correct |
342 ms |
9496 KB |
Output is correct |
55 |
Correct |
11 ms |
8020 KB |
Output is correct |
56 |
Correct |
311 ms |
9512 KB |
Output is correct |
57 |
Correct |
11 ms |
7988 KB |
Output is correct |
58 |
Correct |
291 ms |
9512 KB |
Output is correct |
59 |
Correct |
12 ms |
8096 KB |
Output is correct |
60 |
Correct |
302 ms |
9436 KB |
Output is correct |
61 |
Correct |
11 ms |
7860 KB |
Output is correct |
62 |
Correct |
287 ms |
9460 KB |
Output is correct |
63 |
Correct |
11 ms |
8096 KB |
Output is correct |
64 |
Correct |
300 ms |
9412 KB |
Output is correct |
65 |
Correct |
289 ms |
9392 KB |
Output is correct |
66 |
Correct |
11 ms |
8148 KB |
Output is correct |
67 |
Correct |
351 ms |
9504 KB |
Output is correct |
68 |
Correct |
11 ms |
7892 KB |
Output is correct |
69 |
Correct |
295 ms |
9376 KB |
Output is correct |
70 |
Correct |
12 ms |
8100 KB |
Output is correct |
71 |
Correct |
298 ms |
9368 KB |
Output is correct |
72 |
Correct |
16 ms |
9680 KB |
Output is correct |
73 |
Correct |
157 ms |
9516 KB |
Output is correct |
74 |
Correct |
1597 ms |
14880 KB |
Output is correct |
75 |
Correct |
119 ms |
16536 KB |
Output is correct |
76 |
Correct |
1561 ms |
15568 KB |
Output is correct |
77 |
Correct |
120 ms |
16508 KB |
Output is correct |
78 |
Correct |
1458 ms |
15724 KB |
Output is correct |
79 |
Correct |
1415 ms |
15816 KB |
Output is correct |
80 |
Correct |
128 ms |
16496 KB |
Output is correct |
81 |
Correct |
117 ms |
16484 KB |
Output is correct |
82 |
Correct |
1444 ms |
15708 KB |
Output is correct |
83 |
Correct |
1501 ms |
15708 KB |
Output is correct |
84 |
Correct |
125 ms |
16472 KB |
Output is correct |
85 |
Correct |
1464 ms |
15708 KB |
Output is correct |
86 |
Correct |
119 ms |
16460 KB |
Output is correct |
87 |
Correct |
1410 ms |
15520 KB |
Output is correct |
88 |
Correct |
115 ms |
16484 KB |
Output is correct |
89 |
Correct |
1465 ms |
15588 KB |
Output is correct |
90 |
Correct |
121 ms |
16496 KB |
Output is correct |
91 |
Correct |
1531 ms |
15512 KB |
Output is correct |
92 |
Correct |
132 ms |
16464 KB |
Output is correct |
93 |
Correct |
1441 ms |
15620 KB |
Output is correct |
94 |
Correct |
53 ms |
16812 KB |
Output is correct |
95 |
Correct |
851 ms |
15828 KB |
Output is correct |
96 |
Correct |
827 ms |
15732 KB |
Output is correct |
97 |
Correct |
1509 ms |
15748 KB |
Output is correct |
98 |
Correct |
1532 ms |
15604 KB |
Output is correct |