Submission #890757

# Submission time Handle Problem Language Result Execution time Memory
890757 2023-12-21T23:20:53 Z zeta7532 Tug of War (BOI15_tug) C++17
41 / 100
124 ms 1880 KB
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)

struct unionfind{
  	unionfind() {}
    vector<ll> par,sz;
    unionfind(ll n):par(n),sz(n){
        for(ll i=0;i<n;i++) par[i]=i, sz[i]=1;
    }
    ll root(ll x){
        if(par[x]==x) return x;
        ll t=root(par[x]);
        par[x]=t;
        return t;
    }
    ll size(ll x){
        return sz[x];
    }
    void unite(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        if(rx==ry) return;
        if (sz[rx]>sz[ry]) swap(rx,ry);
        par[rx]=ry;sz[ry]+=sz[rx];
    }
    bool same(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        return rx==ry;
    }
};

int main() {
    ll N,K;
    cin >> N >> K;
    N*=2;
    vector<ll> l(N),r(N),s(N);
    ll max_s=0;
    rep(i,N){
        cin >> l[i] >> r[i] >> s[i];
        l[i]--,r[i]--;
        max_s=max(max_s,s[i]);
    }
    if(N<=20){
        bool ans=false;
        rep(bit,1<<N){
            vector<ll> L(N/2,-1),R(N/2,-1);
            rep(i,N){
                if(bit&(1<<i)) L[l[i]]=i;
                else R[r[i]]=i;
            }
            ll cnt_l=0,cnt_r=0;
            rep(i,N/2){
                if(L[i]==-1){
                    cnt_l=-1;
                    break;
                }
                cnt_l+=s[L[i]];
            }
            rep(i,N/2){
                if(R[i]==-1){
                    cnt_r=-1;
                    break;
                }
                cnt_r+=s[R[i]];
            }
            if(cnt_l==-1||cnt_r==-1) continue;
            if(abs(cnt_l-cnt_r)<=K) ans=true;
        }
        if(ans) cout << "YES" << endl;
        else cout << "NO" << endl;
        return 0;
    }
    if(max_s==1){
        unionfind tree(N);
        rep(i,N) tree.unite(l[i],r[i]+N/2);
        vector<ll> cnt(N,0);
        rep(i,N) cnt[tree.root(l[i])]++;
        bool ans=true;
        rep(i,N){
            if(tree.root(i)!=i) continue;
            if(cnt[i]!=tree.size(i)) ans=false;
        }
        if(ans) cout << "YES" << endl;
        else cout << "NO" << endl;
        return 0;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 115 ms 412 KB Output is correct
2 Correct 120 ms 600 KB Output is correct
3 Correct 115 ms 344 KB Output is correct
4 Correct 120 ms 408 KB Output is correct
5 Correct 122 ms 344 KB Output is correct
6 Correct 116 ms 412 KB Output is correct
7 Correct 121 ms 408 KB Output is correct
8 Correct 119 ms 400 KB Output is correct
9 Correct 117 ms 344 KB Output is correct
10 Correct 118 ms 348 KB Output is correct
11 Correct 118 ms 408 KB Output is correct
12 Correct 117 ms 408 KB Output is correct
13 Correct 119 ms 348 KB Output is correct
14 Correct 117 ms 404 KB Output is correct
15 Correct 116 ms 344 KB Output is correct
16 Correct 122 ms 348 KB Output is correct
17 Correct 119 ms 348 KB Output is correct
18 Correct 116 ms 412 KB Output is correct
19 Correct 117 ms 408 KB Output is correct
20 Correct 117 ms 344 KB Output is correct
21 Correct 120 ms 408 KB Output is correct
22 Correct 124 ms 592 KB Output is correct
23 Correct 115 ms 344 KB Output is correct
24 Correct 123 ms 348 KB Output is correct
25 Correct 118 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 412 KB Output is correct
2 Correct 120 ms 600 KB Output is correct
3 Correct 115 ms 344 KB Output is correct
4 Correct 120 ms 408 KB Output is correct
5 Correct 122 ms 344 KB Output is correct
6 Correct 116 ms 412 KB Output is correct
7 Correct 121 ms 408 KB Output is correct
8 Correct 119 ms 400 KB Output is correct
9 Correct 117 ms 344 KB Output is correct
10 Correct 118 ms 348 KB Output is correct
11 Correct 118 ms 408 KB Output is correct
12 Correct 117 ms 408 KB Output is correct
13 Correct 119 ms 348 KB Output is correct
14 Correct 117 ms 404 KB Output is correct
15 Correct 116 ms 344 KB Output is correct
16 Correct 122 ms 348 KB Output is correct
17 Correct 119 ms 348 KB Output is correct
18 Correct 116 ms 412 KB Output is correct
19 Correct 117 ms 408 KB Output is correct
20 Correct 117 ms 344 KB Output is correct
21 Correct 120 ms 408 KB Output is correct
22 Correct 124 ms 592 KB Output is correct
23 Correct 115 ms 344 KB Output is correct
24 Correct 123 ms 348 KB Output is correct
25 Correct 118 ms 348 KB Output is correct
26 Incorrect 2 ms 348 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1372 KB Output is correct
2 Correct 10 ms 1416 KB Output is correct
3 Correct 10 ms 1628 KB Output is correct
4 Correct 10 ms 1628 KB Output is correct
5 Correct 10 ms 1628 KB Output is correct
6 Correct 10 ms 1488 KB Output is correct
7 Correct 12 ms 1408 KB Output is correct
8 Correct 15 ms 1412 KB Output is correct
9 Correct 10 ms 1880 KB Output is correct
10 Correct 10 ms 1628 KB Output is correct
11 Correct 10 ms 1628 KB Output is correct
12 Correct 10 ms 1624 KB Output is correct
13 Correct 10 ms 1628 KB Output is correct
14 Correct 10 ms 1628 KB Output is correct
15 Correct 13 ms 1628 KB Output is correct
16 Correct 10 ms 1484 KB Output is correct
17 Correct 10 ms 1368 KB Output is correct
18 Correct 10 ms 1628 KB Output is correct
19 Correct 10 ms 1476 KB Output is correct
20 Correct 11 ms 1628 KB Output is correct
21 Correct 10 ms 1628 KB Output is correct
22 Correct 10 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 412 KB Output is correct
2 Correct 120 ms 600 KB Output is correct
3 Correct 115 ms 344 KB Output is correct
4 Correct 120 ms 408 KB Output is correct
5 Correct 122 ms 344 KB Output is correct
6 Correct 116 ms 412 KB Output is correct
7 Correct 121 ms 408 KB Output is correct
8 Correct 119 ms 400 KB Output is correct
9 Correct 117 ms 344 KB Output is correct
10 Correct 118 ms 348 KB Output is correct
11 Correct 118 ms 408 KB Output is correct
12 Correct 117 ms 408 KB Output is correct
13 Correct 119 ms 348 KB Output is correct
14 Correct 117 ms 404 KB Output is correct
15 Correct 116 ms 344 KB Output is correct
16 Correct 122 ms 348 KB Output is correct
17 Correct 119 ms 348 KB Output is correct
18 Correct 116 ms 412 KB Output is correct
19 Correct 117 ms 408 KB Output is correct
20 Correct 117 ms 344 KB Output is correct
21 Correct 120 ms 408 KB Output is correct
22 Correct 124 ms 592 KB Output is correct
23 Correct 115 ms 344 KB Output is correct
24 Correct 123 ms 348 KB Output is correct
25 Correct 118 ms 348 KB Output is correct
26 Incorrect 2 ms 348 KB Output isn't correct
27 Halted 0 ms 0 KB -