Submission #1005772

# Submission time Handle Problem Language Result Execution time Memory
1005772 2024-06-23T04:11:44 Z TrinhKhanhDung Tug of War (BOI15_tug) C++14
0 / 100
554 ms 6836 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)
#define int ll

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 = 6e4 + 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();
        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].erase(it);
        adj[v].erase(make_pair(u, cost));
        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++){
        if(sz(adj[i]) > 2){
            cout << -1;
            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";
}

signed 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 2 ms 4184 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 3 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Correct 2 ms 4188 KB Output is correct
6 Correct 2 ms 4184 KB Output is correct
7 Correct 1 ms 4188 KB Output is correct
8 Correct 2 ms 4188 KB Output is correct
9 Correct 2 ms 4188 KB Output is correct
10 Correct 5 ms 4188 KB Output is correct
11 Correct 3 ms 4184 KB Output is correct
12 Correct 2 ms 4188 KB Output is correct
13 Correct 2 ms 4188 KB Output is correct
14 Correct 2 ms 4188 KB Output is correct
15 Correct 2 ms 4188 KB Output is correct
16 Correct 3 ms 4188 KB Output is correct
17 Correct 2 ms 4188 KB Output is correct
18 Correct 2 ms 4188 KB Output is correct
19 Correct 2 ms 4188 KB Output is correct
20 Correct 2 ms 4000 KB Output is correct
21 Correct 1 ms 4188 KB Output is correct
22 Incorrect 1 ms 4024 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 3 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Correct 2 ms 4188 KB Output is correct
6 Correct 2 ms 4184 KB Output is correct
7 Correct 1 ms 4188 KB Output is correct
8 Correct 2 ms 4188 KB Output is correct
9 Correct 2 ms 4188 KB Output is correct
10 Correct 5 ms 4188 KB Output is correct
11 Correct 3 ms 4184 KB Output is correct
12 Correct 2 ms 4188 KB Output is correct
13 Correct 2 ms 4188 KB Output is correct
14 Correct 2 ms 4188 KB Output is correct
15 Correct 2 ms 4188 KB Output is correct
16 Correct 3 ms 4188 KB Output is correct
17 Correct 2 ms 4188 KB Output is correct
18 Correct 2 ms 4188 KB Output is correct
19 Correct 2 ms 4188 KB Output is correct
20 Correct 2 ms 4000 KB Output is correct
21 Correct 1 ms 4188 KB Output is correct
22 Incorrect 1 ms 4024 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 554 ms 6832 KB Output is correct
2 Correct 7 ms 6744 KB Output is correct
3 Correct 466 ms 6812 KB Output is correct
4 Correct 8 ms 6748 KB Output is correct
5 Correct 453 ms 6748 KB Output is correct
6 Correct 7 ms 6744 KB Output is correct
7 Correct 512 ms 6748 KB Output is correct
8 Correct 6 ms 6744 KB Output is correct
9 Correct 474 ms 6748 KB Output is correct
10 Correct 6 ms 6744 KB Output is correct
11 Correct 484 ms 6748 KB Output is correct
12 Correct 6 ms 6748 KB Output is correct
13 Correct 529 ms 6832 KB Output is correct
14 Correct 465 ms 6748 KB Output is correct
15 Correct 6 ms 6744 KB Output is correct
16 Correct 477 ms 6836 KB Output is correct
17 Correct 6 ms 6768 KB Output is correct
18 Correct 524 ms 6824 KB Output is correct
19 Correct 6 ms 6744 KB Output is correct
20 Correct 457 ms 6748 KB Output is correct
21 Correct 7 ms 6748 KB Output is correct
22 Incorrect 6 ms 6748 KB Output isn't correct
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB Output is correct
2 Correct 2 ms 4188 KB Output is correct
3 Correct 3 ms 4188 KB Output is correct
4 Correct 2 ms 4188 KB Output is correct
5 Correct 2 ms 4188 KB Output is correct
6 Correct 2 ms 4184 KB Output is correct
7 Correct 1 ms 4188 KB Output is correct
8 Correct 2 ms 4188 KB Output is correct
9 Correct 2 ms 4188 KB Output is correct
10 Correct 5 ms 4188 KB Output is correct
11 Correct 3 ms 4184 KB Output is correct
12 Correct 2 ms 4188 KB Output is correct
13 Correct 2 ms 4188 KB Output is correct
14 Correct 2 ms 4188 KB Output is correct
15 Correct 2 ms 4188 KB Output is correct
16 Correct 3 ms 4188 KB Output is correct
17 Correct 2 ms 4188 KB Output is correct
18 Correct 2 ms 4188 KB Output is correct
19 Correct 2 ms 4188 KB Output is correct
20 Correct 2 ms 4000 KB Output is correct
21 Correct 1 ms 4188 KB Output is correct
22 Incorrect 1 ms 4024 KB Output isn't correct
23 Halted 0 ms 0 KB -