Submission #1005776

# Submission time Handle Problem Language Result Execution time Memory
1005776 2024-06-23T04:30:12 Z TrinhKhanhDung Tug of War (BOI15_tug) C++14
100 / 100
1717 ms 12928 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 1e5 + 10;

int N, K;
set<pair<int, int>> adj[MAX];
int s[MAX], L, R;
bool vis[MAX];

void dfs(int u, int p, vector<int> &val){
//    cout << u << ' ';
    vis[u] = true;
    for(auto it: adj[u]){
        int v = it.fi;
        int cost = s[it.se];
        if(it.se != p) val.push_back(cost);
        if(vis[v]) continue;
        dfs(v, it.se, val);
    }
}

void solve(){
    //input
    cin >> N >> K;
    for(int i=1; i<=N*2; i++){
        int l, r;
        cin >> l >> r >> s[i];
        adj[l].insert(make_pair(r + N, i));
        adj[r + N].insert(make_pair(l, i));
    }

    queue<int> q;
    for(int i=1; i<=N*2; i++){
        if(sz(adj[i]) == 0){
            cout << "NO\n";
            return;
        }
        if(sz(adj[i]) == 1){
            q.push(i);
        }
    }

    while(!q.empty()){
        int u = q.front();
//        cout << u << ' ';
        q.pop();

        if(sz(adj[u]) == 0) continue;

        auto it = adj[u].begin();
        int v = (*it).fi, cost = s[(*it).se];
        if(u <= N){
            L += cost;
        }
        else{
            R += cost;
        }
        adj[u].clear();
        adj[v].erase(make_pair(u, (*it).se));
        if(sz(adj[v]) == 1){
            q.push(v);
        }
    }

    bitset<1200003> bs;
    bs[0] = true;

    int S = 0;
    for(int i=1; i<=N*2; i++){
//        cout << i << ' ' << sz(adj[i]) << '\n';
        if(sz(adj[i]) > 2){
            cout << "NO\n";
            return;
        }
        if(sz(adj[i]) == 2 && !vis[i]){
            vector<int> val;
            dfs(i, 0, val);
            int c1 = 0, c2 = 0;
            for(int i=0; i+1<sz(val); i++){
                if(i & 1) c2 += val[i];
                else c1 += val[i];
            }
            bs = (bs << c1) | (bs << c2);
            S += c1 + c2;
        }
    }

    for(int i=0; i<1200001; i++){
        if(bs[i]){
            int j = S - i;
            if(abs(L + i - (R + j)) <= K){
                cout << "YES\n";
                return;
            }
        }
    }
    cout << "NO\n";
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("netw.inp", "r", stdin);
//    freopen("netw.out", "w", stdout);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5724 KB Output is correct
2 Correct 3 ms 5724 KB Output is correct
3 Correct 4 ms 5740 KB Output is correct
4 Correct 3 ms 5724 KB Output is correct
5 Correct 4 ms 5724 KB Output is correct
6 Correct 3 ms 5724 KB Output is correct
7 Correct 3 ms 5724 KB Output is correct
8 Correct 3 ms 5724 KB Output is correct
9 Correct 3 ms 5724 KB Output is correct
10 Correct 3 ms 5748 KB Output is correct
11 Correct 3 ms 5720 KB Output is correct
12 Correct 2 ms 5724 KB Output is correct
13 Correct 2 ms 5736 KB Output is correct
14 Correct 3 ms 5724 KB Output is correct
15 Correct 3 ms 5724 KB Output is correct
16 Correct 4 ms 5724 KB Output is correct
17 Correct 3 ms 5724 KB Output is correct
18 Correct 3 ms 5720 KB Output is correct
19 Correct 3 ms 5724 KB Output is correct
20 Correct 3 ms 5652 KB Output is correct
21 Correct 2 ms 5724 KB Output is correct
22 Correct 3 ms 5724 KB Output is correct
23 Correct 3 ms 5748 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 4 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5724 KB Output is correct
2 Correct 3 ms 5724 KB Output is correct
3 Correct 4 ms 5740 KB Output is correct
4 Correct 3 ms 5724 KB Output is correct
5 Correct 4 ms 5724 KB Output is correct
6 Correct 3 ms 5724 KB Output is correct
7 Correct 3 ms 5724 KB Output is correct
8 Correct 3 ms 5724 KB Output is correct
9 Correct 3 ms 5724 KB Output is correct
10 Correct 3 ms 5748 KB Output is correct
11 Correct 3 ms 5720 KB Output is correct
12 Correct 2 ms 5724 KB Output is correct
13 Correct 2 ms 5736 KB Output is correct
14 Correct 3 ms 5724 KB Output is correct
15 Correct 3 ms 5724 KB Output is correct
16 Correct 4 ms 5724 KB Output is correct
17 Correct 3 ms 5724 KB Output is correct
18 Correct 3 ms 5720 KB Output is correct
19 Correct 3 ms 5724 KB Output is correct
20 Correct 3 ms 5652 KB Output is correct
21 Correct 2 ms 5724 KB Output is correct
22 Correct 3 ms 5724 KB Output is correct
23 Correct 3 ms 5748 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 4 ms 5724 KB Output is correct
26 Correct 105 ms 6152 KB Output is correct
27 Correct 22 ms 5980 KB Output is correct
28 Correct 101 ms 5980 KB Output is correct
29 Correct 18 ms 5980 KB Output is correct
30 Correct 118 ms 5980 KB Output is correct
31 Correct 16 ms 5980 KB Output is correct
32 Correct 97 ms 5980 KB Output is correct
33 Correct 18 ms 5980 KB Output is correct
34 Correct 10 ms 6168 KB Output is correct
35 Correct 18 ms 6176 KB Output is correct
36 Correct 106 ms 6156 KB Output is correct
37 Correct 17 ms 5980 KB Output is correct
38 Correct 96 ms 5980 KB Output is correct
39 Correct 18 ms 5976 KB Output is correct
40 Correct 100 ms 5976 KB Output is correct
41 Correct 18 ms 5976 KB Output is correct
42 Correct 102 ms 5976 KB Output is correct
43 Correct 18 ms 5976 KB Output is correct
44 Correct 107 ms 6148 KB Output is correct
45 Correct 19 ms 5980 KB Output is correct
46 Correct 110 ms 5980 KB Output is correct
47 Correct 4 ms 6160 KB Output is correct
48 Correct 63 ms 6028 KB Output is correct
49 Correct 52 ms 5980 KB Output is correct
50 Correct 104 ms 6156 KB Output is correct
51 Correct 108 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 509 ms 7516 KB Output is correct
2 Correct 10 ms 7516 KB Output is correct
3 Correct 489 ms 7720 KB Output is correct
4 Correct 7 ms 8024 KB Output is correct
5 Correct 521 ms 8216 KB Output is correct
6 Correct 7 ms 8028 KB Output is correct
7 Correct 539 ms 8224 KB Output is correct
8 Correct 7 ms 8028 KB Output is correct
9 Correct 540 ms 8480 KB Output is correct
10 Correct 7 ms 8000 KB Output is correct
11 Correct 514 ms 8000 KB Output is correct
12 Correct 9 ms 8024 KB Output is correct
13 Correct 523 ms 8032 KB Output is correct
14 Correct 503 ms 8228 KB Output is correct
15 Correct 7 ms 8024 KB Output is correct
16 Correct 497 ms 8228 KB Output is correct
17 Correct 7 ms 8212 KB Output is correct
18 Correct 585 ms 8240 KB Output is correct
19 Correct 7 ms 8032 KB Output is correct
20 Correct 495 ms 8232 KB Output is correct
21 Correct 8 ms 8024 KB Output is correct
22 Correct 241 ms 8252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5724 KB Output is correct
2 Correct 3 ms 5724 KB Output is correct
3 Correct 4 ms 5740 KB Output is correct
4 Correct 3 ms 5724 KB Output is correct
5 Correct 4 ms 5724 KB Output is correct
6 Correct 3 ms 5724 KB Output is correct
7 Correct 3 ms 5724 KB Output is correct
8 Correct 3 ms 5724 KB Output is correct
9 Correct 3 ms 5724 KB Output is correct
10 Correct 3 ms 5748 KB Output is correct
11 Correct 3 ms 5720 KB Output is correct
12 Correct 2 ms 5724 KB Output is correct
13 Correct 2 ms 5736 KB Output is correct
14 Correct 3 ms 5724 KB Output is correct
15 Correct 3 ms 5724 KB Output is correct
16 Correct 4 ms 5724 KB Output is correct
17 Correct 3 ms 5724 KB Output is correct
18 Correct 3 ms 5720 KB Output is correct
19 Correct 3 ms 5724 KB Output is correct
20 Correct 3 ms 5652 KB Output is correct
21 Correct 2 ms 5724 KB Output is correct
22 Correct 3 ms 5724 KB Output is correct
23 Correct 3 ms 5748 KB Output is correct
24 Correct 2 ms 5724 KB Output is correct
25 Correct 4 ms 5724 KB Output is correct
26 Correct 105 ms 6152 KB Output is correct
27 Correct 22 ms 5980 KB Output is correct
28 Correct 101 ms 5980 KB Output is correct
29 Correct 18 ms 5980 KB Output is correct
30 Correct 118 ms 5980 KB Output is correct
31 Correct 16 ms 5980 KB Output is correct
32 Correct 97 ms 5980 KB Output is correct
33 Correct 18 ms 5980 KB Output is correct
34 Correct 10 ms 6168 KB Output is correct
35 Correct 18 ms 6176 KB Output is correct
36 Correct 106 ms 6156 KB Output is correct
37 Correct 17 ms 5980 KB Output is correct
38 Correct 96 ms 5980 KB Output is correct
39 Correct 18 ms 5976 KB Output is correct
40 Correct 100 ms 5976 KB Output is correct
41 Correct 18 ms 5976 KB Output is correct
42 Correct 102 ms 5976 KB Output is correct
43 Correct 18 ms 5976 KB Output is correct
44 Correct 107 ms 6148 KB Output is correct
45 Correct 19 ms 5980 KB Output is correct
46 Correct 110 ms 5980 KB Output is correct
47 Correct 4 ms 6160 KB Output is correct
48 Correct 63 ms 6028 KB Output is correct
49 Correct 52 ms 5980 KB Output is correct
50 Correct 104 ms 6156 KB Output is correct
51 Correct 108 ms 5976 KB Output is correct
52 Correct 509 ms 7516 KB Output is correct
53 Correct 10 ms 7516 KB Output is correct
54 Correct 489 ms 7720 KB Output is correct
55 Correct 7 ms 8024 KB Output is correct
56 Correct 521 ms 8216 KB Output is correct
57 Correct 7 ms 8028 KB Output is correct
58 Correct 539 ms 8224 KB Output is correct
59 Correct 7 ms 8028 KB Output is correct
60 Correct 540 ms 8480 KB Output is correct
61 Correct 7 ms 8000 KB Output is correct
62 Correct 514 ms 8000 KB Output is correct
63 Correct 9 ms 8024 KB Output is correct
64 Correct 523 ms 8032 KB Output is correct
65 Correct 503 ms 8228 KB Output is correct
66 Correct 7 ms 8024 KB Output is correct
67 Correct 497 ms 8228 KB Output is correct
68 Correct 7 ms 8212 KB Output is correct
69 Correct 585 ms 8240 KB Output is correct
70 Correct 7 ms 8032 KB Output is correct
71 Correct 495 ms 8232 KB Output is correct
72 Correct 8 ms 8024 KB Output is correct
73 Correct 241 ms 8252 KB Output is correct
74 Correct 1476 ms 12596 KB Output is correct
75 Correct 92 ms 12636 KB Output is correct
76 Correct 1596 ms 12584 KB Output is correct
77 Correct 104 ms 12372 KB Output is correct
78 Correct 1587 ms 12576 KB Output is correct
79 Correct 1717 ms 12840 KB Output is correct
80 Correct 81 ms 12380 KB Output is correct
81 Correct 77 ms 12376 KB Output is correct
82 Correct 1563 ms 12576 KB Output is correct
83 Correct 1574 ms 12628 KB Output is correct
84 Correct 78 ms 12380 KB Output is correct
85 Correct 1563 ms 12588 KB Output is correct
86 Correct 84 ms 12632 KB Output is correct
87 Correct 1466 ms 12840 KB Output is correct
88 Correct 77 ms 12380 KB Output is correct
89 Correct 1572 ms 12584 KB Output is correct
90 Correct 91 ms 12616 KB Output is correct
91 Correct 1574 ms 12624 KB Output is correct
92 Correct 86 ms 12368 KB Output is correct
93 Correct 1513 ms 12372 KB Output is correct
94 Correct 21 ms 12368 KB Output is correct
95 Correct 876 ms 12928 KB Output is correct
96 Correct 816 ms 12620 KB Output is correct
97 Correct 1689 ms 12576 KB Output is correct
98 Correct 1528 ms 12376 KB Output is correct