답안 #705380

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705380 2023-03-04T08:01:10 Z LittleCube Tug of War (BOI15_tug) C++14
71 / 100
3000 ms 14736 KB
#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:21:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   21 |     for (auto [v, w] : E[u])
      |               ^
tug.cpp: In function 'int matching(int)':
tug.cpp:35:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   35 |     for (auto [v, w] : T[u])
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7124 KB Output is correct
2 Correct 5 ms 7048 KB Output is correct
3 Correct 5 ms 7124 KB Output is correct
4 Correct 6 ms 7068 KB Output is correct
5 Correct 5 ms 7124 KB Output is correct
6 Correct 5 ms 7124 KB Output is correct
7 Correct 5 ms 7148 KB Output is correct
8 Correct 5 ms 7068 KB Output is correct
9 Correct 5 ms 7124 KB Output is correct
10 Correct 6 ms 7124 KB Output is correct
11 Correct 5 ms 7124 KB Output is correct
12 Correct 5 ms 7124 KB Output is correct
13 Correct 5 ms 7124 KB Output is correct
14 Correct 5 ms 7124 KB Output is correct
15 Correct 5 ms 7068 KB Output is correct
16 Correct 7 ms 7052 KB Output is correct
17 Correct 5 ms 7124 KB Output is correct
18 Correct 5 ms 7068 KB Output is correct
19 Correct 5 ms 7068 KB Output is correct
20 Correct 5 ms 7124 KB Output is correct
21 Correct 4 ms 6484 KB Output is correct
22 Correct 4 ms 7124 KB Output is correct
23 Correct 4 ms 7124 KB Output is correct
24 Correct 5 ms 7068 KB Output is correct
25 Correct 8 ms 7124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7124 KB Output is correct
2 Correct 5 ms 7048 KB Output is correct
3 Correct 5 ms 7124 KB Output is correct
4 Correct 6 ms 7068 KB Output is correct
5 Correct 5 ms 7124 KB Output is correct
6 Correct 5 ms 7124 KB Output is correct
7 Correct 5 ms 7148 KB Output is correct
8 Correct 5 ms 7068 KB Output is correct
9 Correct 5 ms 7124 KB Output is correct
10 Correct 6 ms 7124 KB Output is correct
11 Correct 5 ms 7124 KB Output is correct
12 Correct 5 ms 7124 KB Output is correct
13 Correct 5 ms 7124 KB Output is correct
14 Correct 5 ms 7124 KB Output is correct
15 Correct 5 ms 7068 KB Output is correct
16 Correct 7 ms 7052 KB Output is correct
17 Correct 5 ms 7124 KB Output is correct
18 Correct 5 ms 7068 KB Output is correct
19 Correct 5 ms 7068 KB Output is correct
20 Correct 5 ms 7124 KB Output is correct
21 Correct 4 ms 6484 KB Output is correct
22 Correct 4 ms 7124 KB Output is correct
23 Correct 4 ms 7124 KB Output is correct
24 Correct 5 ms 7068 KB Output is correct
25 Correct 8 ms 7124 KB Output is correct
26 Correct 412 ms 7748 KB Output is correct
27 Correct 55 ms 7680 KB Output is correct
28 Correct 415 ms 7744 KB Output is correct
29 Correct 67 ms 7676 KB Output is correct
30 Correct 388 ms 7668 KB Output is correct
31 Correct 61 ms 7756 KB Output is correct
32 Correct 434 ms 7744 KB Output is correct
33 Correct 60 ms 7716 KB Output is correct
34 Correct 35 ms 7852 KB Output is correct
35 Correct 60 ms 7652 KB Output is correct
36 Correct 423 ms 7744 KB Output is correct
37 Correct 69 ms 7732 KB Output is correct
38 Correct 380 ms 7824 KB Output is correct
39 Correct 61 ms 7756 KB Output is correct
40 Correct 433 ms 7660 KB Output is correct
41 Correct 63 ms 7772 KB Output is correct
42 Correct 415 ms 7660 KB Output is correct
43 Correct 63 ms 7768 KB Output is correct
44 Correct 427 ms 7664 KB Output is correct
45 Correct 57 ms 7756 KB Output is correct
46 Correct 378 ms 7836 KB Output is correct
47 Correct 6 ms 7124 KB Output is correct
48 Correct 220 ms 7696 KB Output is correct
49 Correct 202 ms 7728 KB Output is correct
50 Correct 396 ms 7688 KB Output is correct
51 Correct 435 ms 7628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1551 ms 9996 KB Output is correct
2 Correct 11 ms 8276 KB Output is correct
3 Correct 1565 ms 9976 KB Output is correct
4 Correct 12 ms 8276 KB Output is correct
5 Correct 1531 ms 10028 KB Output is correct
6 Correct 10 ms 8268 KB Output is correct
7 Correct 1574 ms 10032 KB Output is correct
8 Correct 11 ms 8276 KB Output is correct
9 Correct 1440 ms 9964 KB Output is correct
10 Correct 12 ms 8020 KB Output is correct
11 Correct 1310 ms 9964 KB Output is correct
12 Correct 11 ms 8252 KB Output is correct
13 Correct 1539 ms 9988 KB Output is correct
14 Correct 1560 ms 9996 KB Output is correct
15 Correct 11 ms 8404 KB Output is correct
16 Correct 1410 ms 10228 KB Output is correct
17 Correct 13 ms 8148 KB Output is correct
18 Correct 1322 ms 10116 KB Output is correct
19 Correct 12 ms 8276 KB Output is correct
20 Correct 1395 ms 10176 KB Output is correct
21 Correct 17 ms 9776 KB Output is correct
22 Correct 966 ms 10088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7124 KB Output is correct
2 Correct 5 ms 7048 KB Output is correct
3 Correct 5 ms 7124 KB Output is correct
4 Correct 6 ms 7068 KB Output is correct
5 Correct 5 ms 7124 KB Output is correct
6 Correct 5 ms 7124 KB Output is correct
7 Correct 5 ms 7148 KB Output is correct
8 Correct 5 ms 7068 KB Output is correct
9 Correct 5 ms 7124 KB Output is correct
10 Correct 6 ms 7124 KB Output is correct
11 Correct 5 ms 7124 KB Output is correct
12 Correct 5 ms 7124 KB Output is correct
13 Correct 5 ms 7124 KB Output is correct
14 Correct 5 ms 7124 KB Output is correct
15 Correct 5 ms 7068 KB Output is correct
16 Correct 7 ms 7052 KB Output is correct
17 Correct 5 ms 7124 KB Output is correct
18 Correct 5 ms 7068 KB Output is correct
19 Correct 5 ms 7068 KB Output is correct
20 Correct 5 ms 7124 KB Output is correct
21 Correct 4 ms 6484 KB Output is correct
22 Correct 4 ms 7124 KB Output is correct
23 Correct 4 ms 7124 KB Output is correct
24 Correct 5 ms 7068 KB Output is correct
25 Correct 8 ms 7124 KB Output is correct
26 Correct 412 ms 7748 KB Output is correct
27 Correct 55 ms 7680 KB Output is correct
28 Correct 415 ms 7744 KB Output is correct
29 Correct 67 ms 7676 KB Output is correct
30 Correct 388 ms 7668 KB Output is correct
31 Correct 61 ms 7756 KB Output is correct
32 Correct 434 ms 7744 KB Output is correct
33 Correct 60 ms 7716 KB Output is correct
34 Correct 35 ms 7852 KB Output is correct
35 Correct 60 ms 7652 KB Output is correct
36 Correct 423 ms 7744 KB Output is correct
37 Correct 69 ms 7732 KB Output is correct
38 Correct 380 ms 7824 KB Output is correct
39 Correct 61 ms 7756 KB Output is correct
40 Correct 433 ms 7660 KB Output is correct
41 Correct 63 ms 7772 KB Output is correct
42 Correct 415 ms 7660 KB Output is correct
43 Correct 63 ms 7768 KB Output is correct
44 Correct 427 ms 7664 KB Output is correct
45 Correct 57 ms 7756 KB Output is correct
46 Correct 378 ms 7836 KB Output is correct
47 Correct 6 ms 7124 KB Output is correct
48 Correct 220 ms 7696 KB Output is correct
49 Correct 202 ms 7728 KB Output is correct
50 Correct 396 ms 7688 KB Output is correct
51 Correct 435 ms 7628 KB Output is correct
52 Correct 1551 ms 9996 KB Output is correct
53 Correct 11 ms 8276 KB Output is correct
54 Correct 1565 ms 9976 KB Output is correct
55 Correct 12 ms 8276 KB Output is correct
56 Correct 1531 ms 10028 KB Output is correct
57 Correct 10 ms 8268 KB Output is correct
58 Correct 1574 ms 10032 KB Output is correct
59 Correct 11 ms 8276 KB Output is correct
60 Correct 1440 ms 9964 KB Output is correct
61 Correct 12 ms 8020 KB Output is correct
62 Correct 1310 ms 9964 KB Output is correct
63 Correct 11 ms 8252 KB Output is correct
64 Correct 1539 ms 9988 KB Output is correct
65 Correct 1560 ms 9996 KB Output is correct
66 Correct 11 ms 8404 KB Output is correct
67 Correct 1410 ms 10228 KB Output is correct
68 Correct 13 ms 8148 KB Output is correct
69 Correct 1322 ms 10116 KB Output is correct
70 Correct 12 ms 8276 KB Output is correct
71 Correct 1395 ms 10176 KB Output is correct
72 Correct 17 ms 9776 KB Output is correct
73 Correct 966 ms 10088 KB Output is correct
74 Execution timed out 3102 ms 14736 KB Time limit exceeded
75 Halted 0 ms 0 KB -