#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;
bitset<30000 * 20 * 4> b, bb;
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;
}
bb = 0;
for (auto j : sol)
{
if (j < 0)
bb = bb | b >> (-j);
else
bb = bb | b << j;
}
b = bb;
}
for (int i = 40 * n + 1 - k; i <= 40 * n + 1 + k; 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])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7124 KB |
Output is correct |
2 |
Correct |
5 ms |
7124 KB |
Output is correct |
3 |
Correct |
5 ms |
7124 KB |
Output is correct |
4 |
Correct |
7 ms |
7124 KB |
Output is correct |
5 |
Correct |
6 ms |
7124 KB |
Output is correct |
6 |
Correct |
5 ms |
7140 KB |
Output is correct |
7 |
Correct |
6 ms |
7140 KB |
Output is correct |
8 |
Correct |
5 ms |
7124 KB |
Output is correct |
9 |
Correct |
5 ms |
7148 KB |
Output is correct |
10 |
Correct |
5 ms |
7124 KB |
Output is correct |
11 |
Correct |
6 ms |
7124 KB |
Output is correct |
12 |
Correct |
6 ms |
7124 KB |
Output is correct |
13 |
Correct |
7 ms |
7124 KB |
Output is correct |
14 |
Correct |
5 ms |
7124 KB |
Output is correct |
15 |
Correct |
5 ms |
7124 KB |
Output is correct |
16 |
Correct |
5 ms |
7124 KB |
Output is correct |
17 |
Correct |
5 ms |
7124 KB |
Output is correct |
18 |
Correct |
5 ms |
7124 KB |
Output is correct |
19 |
Correct |
6 ms |
7140 KB |
Output is correct |
20 |
Correct |
5 ms |
7124 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
7124 KB |
Output is correct |
23 |
Correct |
5 ms |
7124 KB |
Output is correct |
24 |
Correct |
5 ms |
7124 KB |
Output is correct |
25 |
Correct |
5 ms |
7124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7124 KB |
Output is correct |
2 |
Correct |
5 ms |
7124 KB |
Output is correct |
3 |
Correct |
5 ms |
7124 KB |
Output is correct |
4 |
Correct |
7 ms |
7124 KB |
Output is correct |
5 |
Correct |
6 ms |
7124 KB |
Output is correct |
6 |
Correct |
5 ms |
7140 KB |
Output is correct |
7 |
Correct |
6 ms |
7140 KB |
Output is correct |
8 |
Correct |
5 ms |
7124 KB |
Output is correct |
9 |
Correct |
5 ms |
7148 KB |
Output is correct |
10 |
Correct |
5 ms |
7124 KB |
Output is correct |
11 |
Correct |
6 ms |
7124 KB |
Output is correct |
12 |
Correct |
6 ms |
7124 KB |
Output is correct |
13 |
Correct |
7 ms |
7124 KB |
Output is correct |
14 |
Correct |
5 ms |
7124 KB |
Output is correct |
15 |
Correct |
5 ms |
7124 KB |
Output is correct |
16 |
Correct |
5 ms |
7124 KB |
Output is correct |
17 |
Correct |
5 ms |
7124 KB |
Output is correct |
18 |
Correct |
5 ms |
7124 KB |
Output is correct |
19 |
Correct |
6 ms |
7140 KB |
Output is correct |
20 |
Correct |
5 ms |
7124 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
7124 KB |
Output is correct |
23 |
Correct |
5 ms |
7124 KB |
Output is correct |
24 |
Correct |
5 ms |
7124 KB |
Output is correct |
25 |
Correct |
5 ms |
7124 KB |
Output is correct |
26 |
Correct |
358 ms |
7616 KB |
Output is correct |
27 |
Correct |
52 ms |
7672 KB |
Output is correct |
28 |
Correct |
349 ms |
7776 KB |
Output is correct |
29 |
Correct |
51 ms |
7676 KB |
Output is correct |
30 |
Correct |
352 ms |
7704 KB |
Output is correct |
31 |
Correct |
46 ms |
7756 KB |
Output is correct |
32 |
Correct |
356 ms |
7628 KB |
Output is correct |
33 |
Correct |
51 ms |
7636 KB |
Output is correct |
34 |
Correct |
30 ms |
7716 KB |
Output is correct |
35 |
Correct |
49 ms |
7680 KB |
Output is correct |
36 |
Correct |
336 ms |
7656 KB |
Output is correct |
37 |
Correct |
54 ms |
7612 KB |
Output is correct |
38 |
Correct |
364 ms |
7620 KB |
Output is correct |
39 |
Correct |
51 ms |
7636 KB |
Output is correct |
40 |
Correct |
344 ms |
7648 KB |
Output is correct |
41 |
Correct |
46 ms |
7688 KB |
Output is correct |
42 |
Correct |
358 ms |
7624 KB |
Output is correct |
43 |
Correct |
49 ms |
7632 KB |
Output is correct |
44 |
Correct |
324 ms |
7620 KB |
Output is correct |
45 |
Correct |
48 ms |
7620 KB |
Output is correct |
46 |
Correct |
354 ms |
7684 KB |
Output is correct |
47 |
Correct |
6 ms |
7252 KB |
Output is correct |
48 |
Correct |
172 ms |
7640 KB |
Output is correct |
49 |
Correct |
160 ms |
7600 KB |
Output is correct |
50 |
Correct |
330 ms |
7572 KB |
Output is correct |
51 |
Correct |
302 ms |
7612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1404 ms |
9784 KB |
Output is correct |
2 |
Correct |
11 ms |
8020 KB |
Output is correct |
3 |
Correct |
1392 ms |
9708 KB |
Output is correct |
4 |
Correct |
11 ms |
8020 KB |
Output is correct |
5 |
Correct |
1393 ms |
9808 KB |
Output is correct |
6 |
Correct |
11 ms |
8028 KB |
Output is correct |
7 |
Correct |
1319 ms |
10040 KB |
Output is correct |
8 |
Correct |
11 ms |
8020 KB |
Output is correct |
9 |
Correct |
1441 ms |
9748 KB |
Output is correct |
10 |
Correct |
11 ms |
7892 KB |
Output is correct |
11 |
Correct |
1380 ms |
9788 KB |
Output is correct |
12 |
Correct |
17 ms |
8020 KB |
Output is correct |
13 |
Correct |
1320 ms |
9932 KB |
Output is correct |
14 |
Correct |
1353 ms |
9912 KB |
Output is correct |
15 |
Correct |
12 ms |
8148 KB |
Output is correct |
16 |
Correct |
1250 ms |
9824 KB |
Output is correct |
17 |
Correct |
11 ms |
8000 KB |
Output is correct |
18 |
Correct |
1458 ms |
9760 KB |
Output is correct |
19 |
Correct |
11 ms |
7996 KB |
Output is correct |
20 |
Correct |
1409 ms |
9744 KB |
Output is correct |
21 |
Correct |
16 ms |
9684 KB |
Output is correct |
22 |
Correct |
781 ms |
9924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7124 KB |
Output is correct |
2 |
Correct |
5 ms |
7124 KB |
Output is correct |
3 |
Correct |
5 ms |
7124 KB |
Output is correct |
4 |
Correct |
7 ms |
7124 KB |
Output is correct |
5 |
Correct |
6 ms |
7124 KB |
Output is correct |
6 |
Correct |
5 ms |
7140 KB |
Output is correct |
7 |
Correct |
6 ms |
7140 KB |
Output is correct |
8 |
Correct |
5 ms |
7124 KB |
Output is correct |
9 |
Correct |
5 ms |
7148 KB |
Output is correct |
10 |
Correct |
5 ms |
7124 KB |
Output is correct |
11 |
Correct |
6 ms |
7124 KB |
Output is correct |
12 |
Correct |
6 ms |
7124 KB |
Output is correct |
13 |
Correct |
7 ms |
7124 KB |
Output is correct |
14 |
Correct |
5 ms |
7124 KB |
Output is correct |
15 |
Correct |
5 ms |
7124 KB |
Output is correct |
16 |
Correct |
5 ms |
7124 KB |
Output is correct |
17 |
Correct |
5 ms |
7124 KB |
Output is correct |
18 |
Correct |
5 ms |
7124 KB |
Output is correct |
19 |
Correct |
6 ms |
7140 KB |
Output is correct |
20 |
Correct |
5 ms |
7124 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
5 ms |
7124 KB |
Output is correct |
23 |
Correct |
5 ms |
7124 KB |
Output is correct |
24 |
Correct |
5 ms |
7124 KB |
Output is correct |
25 |
Correct |
5 ms |
7124 KB |
Output is correct |
26 |
Correct |
358 ms |
7616 KB |
Output is correct |
27 |
Correct |
52 ms |
7672 KB |
Output is correct |
28 |
Correct |
349 ms |
7776 KB |
Output is correct |
29 |
Correct |
51 ms |
7676 KB |
Output is correct |
30 |
Correct |
352 ms |
7704 KB |
Output is correct |
31 |
Correct |
46 ms |
7756 KB |
Output is correct |
32 |
Correct |
356 ms |
7628 KB |
Output is correct |
33 |
Correct |
51 ms |
7636 KB |
Output is correct |
34 |
Correct |
30 ms |
7716 KB |
Output is correct |
35 |
Correct |
49 ms |
7680 KB |
Output is correct |
36 |
Correct |
336 ms |
7656 KB |
Output is correct |
37 |
Correct |
54 ms |
7612 KB |
Output is correct |
38 |
Correct |
364 ms |
7620 KB |
Output is correct |
39 |
Correct |
51 ms |
7636 KB |
Output is correct |
40 |
Correct |
344 ms |
7648 KB |
Output is correct |
41 |
Correct |
46 ms |
7688 KB |
Output is correct |
42 |
Correct |
358 ms |
7624 KB |
Output is correct |
43 |
Correct |
49 ms |
7632 KB |
Output is correct |
44 |
Correct |
324 ms |
7620 KB |
Output is correct |
45 |
Correct |
48 ms |
7620 KB |
Output is correct |
46 |
Correct |
354 ms |
7684 KB |
Output is correct |
47 |
Correct |
6 ms |
7252 KB |
Output is correct |
48 |
Correct |
172 ms |
7640 KB |
Output is correct |
49 |
Correct |
160 ms |
7600 KB |
Output is correct |
50 |
Correct |
330 ms |
7572 KB |
Output is correct |
51 |
Correct |
302 ms |
7612 KB |
Output is correct |
52 |
Correct |
1404 ms |
9784 KB |
Output is correct |
53 |
Correct |
11 ms |
8020 KB |
Output is correct |
54 |
Correct |
1392 ms |
9708 KB |
Output is correct |
55 |
Correct |
11 ms |
8020 KB |
Output is correct |
56 |
Correct |
1393 ms |
9808 KB |
Output is correct |
57 |
Correct |
11 ms |
8028 KB |
Output is correct |
58 |
Correct |
1319 ms |
10040 KB |
Output is correct |
59 |
Correct |
11 ms |
8020 KB |
Output is correct |
60 |
Correct |
1441 ms |
9748 KB |
Output is correct |
61 |
Correct |
11 ms |
7892 KB |
Output is correct |
62 |
Correct |
1380 ms |
9788 KB |
Output is correct |
63 |
Correct |
17 ms |
8020 KB |
Output is correct |
64 |
Correct |
1320 ms |
9932 KB |
Output is correct |
65 |
Correct |
1353 ms |
9912 KB |
Output is correct |
66 |
Correct |
12 ms |
8148 KB |
Output is correct |
67 |
Correct |
1250 ms |
9824 KB |
Output is correct |
68 |
Correct |
11 ms |
8000 KB |
Output is correct |
69 |
Correct |
1458 ms |
9760 KB |
Output is correct |
70 |
Correct |
11 ms |
7996 KB |
Output is correct |
71 |
Correct |
1409 ms |
9744 KB |
Output is correct |
72 |
Correct |
16 ms |
9684 KB |
Output is correct |
73 |
Correct |
781 ms |
9924 KB |
Output is correct |
74 |
Execution timed out |
3024 ms |
14160 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |