Submission #337748

# Submission time Handle Problem Language Result Execution time Memory
337748 2020-12-21T13:32:41 Z HynDuf Tug of War (BOI15_tug) C++11
100 / 100
897 ms 14504 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define eb emplace_back
#define pb push_back
#define pf push_front
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define Rep(i, r, l) for (int i = (r); i >= (l); i--)
#define DB(X) {cerr << #X << " = " << X << '\n';}
#define DB1(A, _) {cerr << #A << "[" << _ << "] = " << A[_] << '\n';}
#define DB2(A, _, __) {cerr << #A << "[" << _ << "][" << __ << "] = " << A[_][__] << '\n';}
#define DB3(A, _, __, ____) {cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << A[_][__][___] << '\n';}
#define PR(A, l, r) {cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define left ___left
#define right ___right
#define next ___next
#define prev ___prev
#define y1 ___y1
#define y0 ___y0
#define j0 ___j0
#define SZ(x) ((int)(x).size())
using ll = long long;
using ld = long double;
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 6e4 + 2, NN = 600002;
int n, cnt[N], K;
struct Tug
{
    int l, r, s;
} tug[N];
set<int> tu[N];
vii g[N];
vi obj;
bool vis[N];
int cur;
void dfs(int u, int r)
{
    cnt[u] = 0;
    for (const ii &v : g[u]) if (!vis[v.S])
    {
        vis[v.S] = 1;
        if (u > n) cur -= tug[v.S].s;
        else cur += tug[v.S].s;
        if (v.F != r) dfs(v.F, r);
    }
}
bitset<NN> dp;
int main()
{
//    freopen("T.in", "r", stdin);
//    freopen("T.out", "w", stdout);
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> K;
    rep(i, 1, n << 1)
    {
        int l, r, s;
        cin >> l >> r >> s;
        r += n;
        tug[i] = {l, r, s};
        cnt[l]++, cnt[r]++;
        tu[l].insert(i);
        tu[r].insert(i);
    }
    queue<int> q;
    rep(i, 1, (n << 1)) if (cnt[i] == 0)
    {
        cout << "NO";
        return 0;
    } else if (cnt[i] == 1) q.push(i);
    int ans = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (cnt[u] == 0)
        {
            cout << "NO";
            return 0;
        }
        assert(cnt[u] == 1);
        assert(SZ(tu[u]) == 1);
        cnt[u] = 0;
        int id = *tu[u].begin();
        int ano = (tug[id].l == u ? tug[id].r : tug[id].l);
        if (--cnt[ano] == 1) q.push(ano);
        if (u > n) ans -= tug[id].s;
        else ans += tug[id].s;
        tu[ano].erase(id);
    }
    rep(u, 1, (n << 1)) if (cnt[u])
    {
        assert(cnt[u] == 2);
        assert(SZ(tu[u]) == 2);
        auto it = tu[u].begin();
        int ano = (tug[*it].l == u ? tug[*it].r : tug[*it].l);
        g[u].eb(ano, *it);
        it++;
        ano = (tug[*it].l == u ? tug[*it].r : tug[*it].l);
        g[u].eb(ano, *it);
    }
    rep(i, 1, (n << 1)) if (cnt[i] == 2)
    {
        cur = 0;
        dfs(i, i);
        obj.eb(abs(cur));
    }
    obj.eb(abs(ans));
    dp[0] = 1;
    int tot = 0;
    for (int v : obj) dp |= dp << v, tot += v;
    rep(i, 0, tot) if (dp[i] && abs(tot - i - i) <= K)
    {
        cout << "YES";
        return 0;
    }
    cout << "NO";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4844 KB Output is correct
2 Correct 4 ms 4844 KB Output is correct
3 Correct 4 ms 4844 KB Output is correct
4 Correct 4 ms 4844 KB Output is correct
5 Correct 4 ms 4844 KB Output is correct
6 Correct 4 ms 4844 KB Output is correct
7 Correct 4 ms 4844 KB Output is correct
8 Correct 4 ms 4844 KB Output is correct
9 Correct 4 ms 4844 KB Output is correct
10 Correct 4 ms 4844 KB Output is correct
11 Correct 4 ms 4844 KB Output is correct
12 Correct 4 ms 4844 KB Output is correct
13 Correct 4 ms 4844 KB Output is correct
14 Correct 4 ms 4844 KB Output is correct
15 Correct 4 ms 4844 KB Output is correct
16 Correct 4 ms 4844 KB Output is correct
17 Correct 4 ms 4844 KB Output is correct
18 Correct 4 ms 4844 KB Output is correct
19 Correct 4 ms 4844 KB Output is correct
20 Correct 4 ms 4736 KB Output is correct
21 Correct 4 ms 4588 KB Output is correct
22 Correct 4 ms 4844 KB Output is correct
23 Correct 4 ms 4844 KB Output is correct
24 Correct 4 ms 4844 KB Output is correct
25 Correct 4 ms 4844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4844 KB Output is correct
2 Correct 4 ms 4844 KB Output is correct
3 Correct 4 ms 4844 KB Output is correct
4 Correct 4 ms 4844 KB Output is correct
5 Correct 4 ms 4844 KB Output is correct
6 Correct 4 ms 4844 KB Output is correct
7 Correct 4 ms 4844 KB Output is correct
8 Correct 4 ms 4844 KB Output is correct
9 Correct 4 ms 4844 KB Output is correct
10 Correct 4 ms 4844 KB Output is correct
11 Correct 4 ms 4844 KB Output is correct
12 Correct 4 ms 4844 KB Output is correct
13 Correct 4 ms 4844 KB Output is correct
14 Correct 4 ms 4844 KB Output is correct
15 Correct 4 ms 4844 KB Output is correct
16 Correct 4 ms 4844 KB Output is correct
17 Correct 4 ms 4844 KB Output is correct
18 Correct 4 ms 4844 KB Output is correct
19 Correct 4 ms 4844 KB Output is correct
20 Correct 4 ms 4736 KB Output is correct
21 Correct 4 ms 4588 KB Output is correct
22 Correct 4 ms 4844 KB Output is correct
23 Correct 4 ms 4844 KB Output is correct
24 Correct 4 ms 4844 KB Output is correct
25 Correct 4 ms 4844 KB Output is correct
26 Correct 58 ms 5504 KB Output is correct
27 Correct 14 ms 5356 KB Output is correct
28 Correct 56 ms 5356 KB Output is correct
29 Correct 14 ms 5356 KB Output is correct
30 Correct 57 ms 5356 KB Output is correct
31 Correct 13 ms 5376 KB Output is correct
32 Correct 59 ms 5356 KB Output is correct
33 Correct 13 ms 5356 KB Output is correct
34 Correct 10 ms 5356 KB Output is correct
35 Correct 13 ms 5356 KB Output is correct
36 Correct 57 ms 5356 KB Output is correct
37 Correct 13 ms 5356 KB Output is correct
38 Correct 56 ms 5376 KB Output is correct
39 Correct 13 ms 5356 KB Output is correct
40 Correct 59 ms 5356 KB Output is correct
41 Correct 13 ms 5356 KB Output is correct
42 Correct 57 ms 5356 KB Output is correct
43 Correct 13 ms 5356 KB Output is correct
44 Correct 57 ms 5356 KB Output is correct
45 Correct 13 ms 5356 KB Output is correct
46 Correct 60 ms 5356 KB Output is correct
47 Correct 5 ms 5100 KB Output is correct
48 Correct 31 ms 5356 KB Output is correct
49 Correct 32 ms 5356 KB Output is correct
50 Correct 58 ms 5356 KB Output is correct
51 Correct 57 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 7916 KB Output is correct
2 Correct 15 ms 6764 KB Output is correct
3 Correct 176 ms 7916 KB Output is correct
4 Correct 14 ms 6764 KB Output is correct
5 Correct 175 ms 7788 KB Output is correct
6 Correct 14 ms 6764 KB Output is correct
7 Correct 179 ms 8064 KB Output is correct
8 Correct 14 ms 6764 KB Output is correct
9 Correct 175 ms 7788 KB Output is correct
10 Correct 15 ms 6892 KB Output is correct
11 Correct 176 ms 7788 KB Output is correct
12 Correct 14 ms 6764 KB Output is correct
13 Correct 178 ms 8044 KB Output is correct
14 Correct 180 ms 7916 KB Output is correct
15 Correct 15 ms 6764 KB Output is correct
16 Correct 175 ms 7916 KB Output is correct
17 Correct 14 ms 6764 KB Output is correct
18 Correct 175 ms 7916 KB Output is correct
19 Correct 14 ms 6764 KB Output is correct
20 Correct 178 ms 7916 KB Output is correct
21 Correct 14 ms 6764 KB Output is correct
22 Correct 98 ms 7148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4844 KB Output is correct
2 Correct 4 ms 4844 KB Output is correct
3 Correct 4 ms 4844 KB Output is correct
4 Correct 4 ms 4844 KB Output is correct
5 Correct 4 ms 4844 KB Output is correct
6 Correct 4 ms 4844 KB Output is correct
7 Correct 4 ms 4844 KB Output is correct
8 Correct 4 ms 4844 KB Output is correct
9 Correct 4 ms 4844 KB Output is correct
10 Correct 4 ms 4844 KB Output is correct
11 Correct 4 ms 4844 KB Output is correct
12 Correct 4 ms 4844 KB Output is correct
13 Correct 4 ms 4844 KB Output is correct
14 Correct 4 ms 4844 KB Output is correct
15 Correct 4 ms 4844 KB Output is correct
16 Correct 4 ms 4844 KB Output is correct
17 Correct 4 ms 4844 KB Output is correct
18 Correct 4 ms 4844 KB Output is correct
19 Correct 4 ms 4844 KB Output is correct
20 Correct 4 ms 4736 KB Output is correct
21 Correct 4 ms 4588 KB Output is correct
22 Correct 4 ms 4844 KB Output is correct
23 Correct 4 ms 4844 KB Output is correct
24 Correct 4 ms 4844 KB Output is correct
25 Correct 4 ms 4844 KB Output is correct
26 Correct 58 ms 5504 KB Output is correct
27 Correct 14 ms 5356 KB Output is correct
28 Correct 56 ms 5356 KB Output is correct
29 Correct 14 ms 5356 KB Output is correct
30 Correct 57 ms 5356 KB Output is correct
31 Correct 13 ms 5376 KB Output is correct
32 Correct 59 ms 5356 KB Output is correct
33 Correct 13 ms 5356 KB Output is correct
34 Correct 10 ms 5356 KB Output is correct
35 Correct 13 ms 5356 KB Output is correct
36 Correct 57 ms 5356 KB Output is correct
37 Correct 13 ms 5356 KB Output is correct
38 Correct 56 ms 5376 KB Output is correct
39 Correct 13 ms 5356 KB Output is correct
40 Correct 59 ms 5356 KB Output is correct
41 Correct 13 ms 5356 KB Output is correct
42 Correct 57 ms 5356 KB Output is correct
43 Correct 13 ms 5356 KB Output is correct
44 Correct 57 ms 5356 KB Output is correct
45 Correct 13 ms 5356 KB Output is correct
46 Correct 60 ms 5356 KB Output is correct
47 Correct 5 ms 5100 KB Output is correct
48 Correct 31 ms 5356 KB Output is correct
49 Correct 32 ms 5356 KB Output is correct
50 Correct 58 ms 5356 KB Output is correct
51 Correct 57 ms 5356 KB Output is correct
52 Correct 176 ms 7916 KB Output is correct
53 Correct 15 ms 6764 KB Output is correct
54 Correct 176 ms 7916 KB Output is correct
55 Correct 14 ms 6764 KB Output is correct
56 Correct 175 ms 7788 KB Output is correct
57 Correct 14 ms 6764 KB Output is correct
58 Correct 179 ms 8064 KB Output is correct
59 Correct 14 ms 6764 KB Output is correct
60 Correct 175 ms 7788 KB Output is correct
61 Correct 15 ms 6892 KB Output is correct
62 Correct 176 ms 7788 KB Output is correct
63 Correct 14 ms 6764 KB Output is correct
64 Correct 178 ms 8044 KB Output is correct
65 Correct 180 ms 7916 KB Output is correct
66 Correct 15 ms 6764 KB Output is correct
67 Correct 175 ms 7916 KB Output is correct
68 Correct 14 ms 6764 KB Output is correct
69 Correct 175 ms 7916 KB Output is correct
70 Correct 14 ms 6764 KB Output is correct
71 Correct 178 ms 7916 KB Output is correct
72 Correct 14 ms 6764 KB Output is correct
73 Correct 98 ms 7148 KB Output is correct
74 Correct 833 ms 14504 KB Output is correct
75 Correct 111 ms 14188 KB Output is correct
76 Correct 828 ms 14444 KB Output is correct
77 Correct 111 ms 14188 KB Output is correct
78 Correct 827 ms 14444 KB Output is correct
79 Correct 835 ms 14444 KB Output is correct
80 Correct 108 ms 14188 KB Output is correct
81 Correct 105 ms 14060 KB Output is correct
82 Correct 819 ms 14444 KB Output is correct
83 Correct 841 ms 14444 KB Output is correct
84 Correct 113 ms 14196 KB Output is correct
85 Correct 835 ms 14456 KB Output is correct
86 Correct 106 ms 14316 KB Output is correct
87 Correct 840 ms 14444 KB Output is correct
88 Correct 110 ms 14188 KB Output is correct
89 Correct 840 ms 14316 KB Output is correct
90 Correct 127 ms 14172 KB Output is correct
91 Correct 897 ms 14444 KB Output is correct
92 Correct 117 ms 14316 KB Output is correct
93 Correct 844 ms 14444 KB Output is correct
94 Correct 57 ms 12012 KB Output is correct
95 Correct 494 ms 12564 KB Output is correct
96 Correct 463 ms 12652 KB Output is correct
97 Correct 857 ms 14316 KB Output is correct
98 Correct 847 ms 14444 KB Output is correct