답안 #719835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719835 2023-04-06T20:01:46 Z four_specks Tug of War (BOI15_tug) C++17
100 / 100
762 ms 5992 KB
#include <bits/stdc++.h>

using namespace std;

namespace
{
    template <typename Fun>
    struct YCombinator
    {
        template <typename T>
        YCombinator(T &&_fun) : fun(forward<T>(_fun)) {}

        template <typename... Args>
        decltype(auto) operator()(Args &&...args) { return fun(ref(*this), forward<Args>(args)...); }

    private:
        Fun fun;
    };

    template <typename T>
    YCombinator(T &&) -> YCombinator<decay_t<T>>;

    template <typename T>
    bool ckmin(T &lhs, const T &rhs) { return rhs < lhs ? lhs = rhs, 1 : 0; }

    template <typename T>
    bool ckmax(T &lhs, const T &rhs) { return rhs > lhs ? lhs = rhs, 1 : 0; }

} // namespace

using BS = bitset<400'001>;

void solve()
{
    int n, k;
    cin >> n >> k;

    vector<int> l(2 * n), r(2 * n), s(2 * n);
    for (int i = 0; i < 2 * n; i++)
        cin >> l[i] >> r[i] >> s[i], --l[i], --r[i];

    vector<int> par(2 * n, -1);
    vector<bool> fin(2 * n, 0);

    YCombinator find = [&](auto self, int x) -> int
    { return par[x] < 0 ? x : par[x] = self(par[x]); };

    auto unite = [&](int x, int y) -> bool
    {
        x = find(x);
        y = find(y);

        if (x == y)
        {
            fin[x] = 1;
            return 0;
        }

        if (par[x] > par[y])
            swap(x, y);

        if (fin[y])
            fin[x] = 1;

        par[x] += par[y];
        par[y] = x;

        return 1;
    };

    for (int i = 0; i < 2 * n; i++)
        unite(l[i], r[i] + n);

    bool good = 0;

    bool ok = 1;
    for (int i = 0; i < 2 * n; i++)
        ok &= fin[find(i)];

    if (ok)
    {
        int sum = 0;
        for (int i = 0; i < 2 * n; i++)
            sum += s[i];

        vector<vector<pair<int, int>>> adj(2 * n);
        for (int i = 0; i < 2 * n; i++)
        {
            adj[l[i]].emplace_back(r[i] + n, i);
            adj[r[i] + n].emplace_back(l[i], i);
        }

        vector<bool> done_p(2 * n, 0), done_q(2 * n, 0), used(2 * n, 0), vis(2 * n, 0);

        auto calc = [&](int w) -> array<int, 2>
        {
            int j = -1;
            {
                queue<int> que;

                que.push(w);
                vis[w] = 1;
                while (j == -1)
                {
                    int u = que.front();
                    que.pop();

                    for (auto [v, i] : adj[u])
                    {
                        if (!used[i])
                        {
                            if (vis[v])
                            {
                                j = i;
                                break;
                            }

                            que.push(v);
                            vis[v] = 1;

                            used[i] = 1;
                        }
                    }
                }
            }

            int p = 0, q = 0;
            {
                queue<int> que;
                p += s[j];
                que.push(l[j]);
                done_p[l[j]] = 1;
                while (!que.empty())
                {
                    int u = que.front();
                    que.pop();

                    for (auto [v, i] : adj[u])
                    {
                        if (!done_p[v] && i != j)
                        {
                            if (v < n)
                                p += s[i];

                            que.push(v);
                            done_p[v] = 1;
                        }
                    }
                }
            }
            {
                queue<int> que;
                que.push(r[j] + n);
                done_q[r[j] + n] = 1;
                while (!que.empty())
                {
                    int u = que.front();
                    que.pop();

                    for (auto [v, i] : adj[u])
                    {
                        if (!done_q[v] && i != j)
                        {
                            if (v < n)
                                q += s[i];

                            que.push(v);
                            done_q[v] = 1;
                        }
                    }
                }
            }

            return {p, q};
        };

        BS bs = 1;
        for (int i = 0; i < 2 * n; i++)
        {
            if (!done_p[i])
            {
                auto [p, q] = calc(i);
                bs |= (bs << p) | (bs << q);
            }
        }

        for (int p = (int)bs._Find_first(); p != (int)bs.size(); p = (int)bs._Find_next(p))
            good |= abs(sum - 2 * p) <= k;
    }

    cout << (good ? "YES" : "NO") << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 508 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 516 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 508 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 516 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 50 ms 724 KB Output is correct
27 Correct 9 ms 776 KB Output is correct
28 Correct 52 ms 824 KB Output is correct
29 Correct 9 ms 724 KB Output is correct
30 Correct 49 ms 824 KB Output is correct
31 Correct 8 ms 724 KB Output is correct
32 Correct 49 ms 724 KB Output is correct
33 Correct 12 ms 724 KB Output is correct
34 Correct 6 ms 724 KB Output is correct
35 Correct 9 ms 776 KB Output is correct
36 Correct 53 ms 724 KB Output is correct
37 Correct 9 ms 732 KB Output is correct
38 Correct 51 ms 828 KB Output is correct
39 Correct 9 ms 724 KB Output is correct
40 Correct 49 ms 824 KB Output is correct
41 Correct 8 ms 724 KB Output is correct
42 Correct 48 ms 724 KB Output is correct
43 Correct 8 ms 780 KB Output is correct
44 Correct 49 ms 824 KB Output is correct
45 Correct 9 ms 724 KB Output is correct
46 Correct 49 ms 832 KB Output is correct
47 Correct 1 ms 520 KB Output is correct
48 Correct 26 ms 852 KB Output is correct
49 Correct 26 ms 852 KB Output is correct
50 Correct 49 ms 724 KB Output is correct
51 Correct 52 ms 820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 2060 KB Output is correct
2 Correct 6 ms 980 KB Output is correct
3 Correct 249 ms 2132 KB Output is correct
4 Correct 5 ms 980 KB Output is correct
5 Correct 245 ms 2064 KB Output is correct
6 Correct 6 ms 980 KB Output is correct
7 Correct 244 ms 2064 KB Output is correct
8 Correct 5 ms 996 KB Output is correct
9 Correct 246 ms 2140 KB Output is correct
10 Correct 5 ms 980 KB Output is correct
11 Correct 249 ms 2132 KB Output is correct
12 Correct 6 ms 980 KB Output is correct
13 Correct 263 ms 2132 KB Output is correct
14 Correct 247 ms 2132 KB Output is correct
15 Correct 5 ms 1036 KB Output is correct
16 Correct 253 ms 2136 KB Output is correct
17 Correct 5 ms 980 KB Output is correct
18 Correct 248 ms 2132 KB Output is correct
19 Correct 5 ms 980 KB Output is correct
20 Correct 243 ms 2132 KB Output is correct
21 Correct 5 ms 980 KB Output is correct
22 Correct 131 ms 2264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 508 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 516 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 50 ms 724 KB Output is correct
27 Correct 9 ms 776 KB Output is correct
28 Correct 52 ms 824 KB Output is correct
29 Correct 9 ms 724 KB Output is correct
30 Correct 49 ms 824 KB Output is correct
31 Correct 8 ms 724 KB Output is correct
32 Correct 49 ms 724 KB Output is correct
33 Correct 12 ms 724 KB Output is correct
34 Correct 6 ms 724 KB Output is correct
35 Correct 9 ms 776 KB Output is correct
36 Correct 53 ms 724 KB Output is correct
37 Correct 9 ms 732 KB Output is correct
38 Correct 51 ms 828 KB Output is correct
39 Correct 9 ms 724 KB Output is correct
40 Correct 49 ms 824 KB Output is correct
41 Correct 8 ms 724 KB Output is correct
42 Correct 48 ms 724 KB Output is correct
43 Correct 8 ms 780 KB Output is correct
44 Correct 49 ms 824 KB Output is correct
45 Correct 9 ms 724 KB Output is correct
46 Correct 49 ms 832 KB Output is correct
47 Correct 1 ms 520 KB Output is correct
48 Correct 26 ms 852 KB Output is correct
49 Correct 26 ms 852 KB Output is correct
50 Correct 49 ms 724 KB Output is correct
51 Correct 52 ms 820 KB Output is correct
52 Correct 245 ms 2060 KB Output is correct
53 Correct 6 ms 980 KB Output is correct
54 Correct 249 ms 2132 KB Output is correct
55 Correct 5 ms 980 KB Output is correct
56 Correct 245 ms 2064 KB Output is correct
57 Correct 6 ms 980 KB Output is correct
58 Correct 244 ms 2064 KB Output is correct
59 Correct 5 ms 996 KB Output is correct
60 Correct 246 ms 2140 KB Output is correct
61 Correct 5 ms 980 KB Output is correct
62 Correct 249 ms 2132 KB Output is correct
63 Correct 6 ms 980 KB Output is correct
64 Correct 263 ms 2132 KB Output is correct
65 Correct 247 ms 2132 KB Output is correct
66 Correct 5 ms 1036 KB Output is correct
67 Correct 253 ms 2136 KB Output is correct
68 Correct 5 ms 980 KB Output is correct
69 Correct 248 ms 2132 KB Output is correct
70 Correct 5 ms 980 KB Output is correct
71 Correct 243 ms 2132 KB Output is correct
72 Correct 5 ms 980 KB Output is correct
73 Correct 131 ms 2264 KB Output is correct
74 Correct 737 ms 5696 KB Output is correct
75 Correct 62 ms 5684 KB Output is correct
76 Correct 738 ms 5708 KB Output is correct
77 Correct 66 ms 5588 KB Output is correct
78 Correct 743 ms 5500 KB Output is correct
79 Correct 762 ms 5644 KB Output is correct
80 Correct 57 ms 5588 KB Output is correct
81 Correct 61 ms 5488 KB Output is correct
82 Correct 708 ms 5588 KB Output is correct
83 Correct 719 ms 5468 KB Output is correct
84 Correct 59 ms 5588 KB Output is correct
85 Correct 730 ms 5592 KB Output is correct
86 Correct 58 ms 5564 KB Output is correct
87 Correct 733 ms 5604 KB Output is correct
88 Correct 58 ms 5568 KB Output is correct
89 Correct 735 ms 5624 KB Output is correct
90 Correct 58 ms 5560 KB Output is correct
91 Correct 727 ms 5708 KB Output is correct
92 Correct 57 ms 5560 KB Output is correct
93 Correct 745 ms 5588 KB Output is correct
94 Correct 16 ms 2260 KB Output is correct
95 Correct 400 ms 5992 KB Output is correct
96 Correct 395 ms 5976 KB Output is correct
97 Correct 731 ms 5632 KB Output is correct
98 Correct 748 ms 5592 KB Output is correct