Submission #985884

# Submission time Handle Problem Language Result Execution time Memory
985884 2024-05-19T08:23:04 Z hengliao Tug of War (BOI15_tug) C++17
100 / 100
1166 ms 23400 KB
#pragma GCC target("avx2,bmi,bmi2,popcnt")
#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;

const ll mxN=6e4+5;
const ll MAX=300005;

ll l[mxN];
ll r[mxN];
ll s[mxN];
ll lg[mxN];
ll rg[mxN];
ll lsum=0;
ll rsum=0;
set<pll> adj[mxN][2];
vll in[mxN];
ll cnt=0;
pll val[mxN];
bool visited[mxN][2];
bool done[mxN][2];
vector<pll> stk;
ll dt;

bitset<2*MAX> dp;
bitset<2*MAX> new_dp;

ll dfs(ll cur, ll idx, ll type){
    if(visited[cur][type]){
        return 0;
    }
    ll re=s[idx];
    if(type==0) re*=-1;
    //cout<<cur<<' '<<type<<' '<<re<<'\n';
    visited[cur][type]=1;
    done[cur][type]=1;
    for(auto &[x, y]:adj[cur][type]){
        if(y==dt) continue;
        re+=dfs(x, y, (type+1)%2);
    }
    stk.pb({cur, type});
    return re;
}

ll getidx(ll a){
    return a+MAX;
}

void solve(){
    memset(lg, 0, sizeof(lg));
    memset(rg, 0, sizeof(rg));
    memset(visited, 0, sizeof(visited));
    memset(done, 0, sizeof(done));
	ll n, k;
    cin>>n>>k;
    bool good=1;
    for(ll i=0;i<2*n;i++){
        cin>>l[i]>>r[i]>>s[i];
        l[i]--; r[i]--;
        if(s[i]!=1){
            good=0;
        } 
        adj[l[i]][0].insert({r[i], i});
        adj[r[i]][1].insert({l[i], i});
    }
    set<pair<ll, pll>> st;
    for(ll i=0;i<n;i++){
        for(ll j=0;j<2;j++){
            st.insert({adj[i][j].size(), {i, j}});
        }
    }
    while(!st.empty()){
        pair<ll, pll> tep=*st.begin(); 
        st.erase(tep);
        if(tep.F<=0){
            cout<<"NO\n";
            return;
        }
        if(tep.F>=2){
            break;
        }
        pll cur=*adj[tep.S.F][tep.S.S].begin();
        //adj[tep.S.F][tep.S.S].erase(cur);
        if(tep.S.S==0){
            lsum+=s[cur.S];
            done[tep.S.F][0]=1;
            st.erase({adj[cur.F][1].size(), {cur.F, 1}});
            adj[cur.F][1].erase({tep.S.F, cur.S});
            adj[tep.S.F][0].erase({cur.F, cur.S});
            st.insert({adj[cur.F][1].size(), {cur.F, 1}});
        }
        else{
            rsum+=s[cur.S];
            done[tep.S.F][1]=1;
            st.erase({adj[cur.F][0].size(), {cur.F, 0}});
            adj[cur.F][0].erase({tep.S.F, cur.S});
            adj[tep.S.F][1].erase({cur.F, cur.S});
            st.insert({adj[cur.F][0].size(), {cur.F, 0}});
        }
    }
    if(good){
        cout<<"YES\n";
        return;
    }
    for(ll i=0;i<n;i++){
        if(!done[i][0]){
            dt=(*adj[i][0].begin()).S;
            val[cnt].F=dfs(i, dt, 0);
            for(auto &[x, y]:stk){
                visited[x][y]=0;
            }
            stk.clear();
            val[cnt].S=dfs(r[dt], dt, 1);
            stk.clear();
            //cout<<val[cnt].F<<' '<<val[cnt].S<<'\n';
            cnt++;
        }
    }
    dp.reset();
    dp[getidx(rsum-lsum)]=1;
    for(ll i=0;i<cnt;i++){
        new_dp.reset();
        if(val[i].F>0){
            new_dp=(new_dp | (dp<<val[i].F));
        }
        else{
            new_dp=(new_dp | (dp>>(-val[i].F)));
        }
        if(val[i].S>0){
            new_dp=(new_dp | (dp<<val[i].S));
        }
        else{
            new_dp=(new_dp | (dp>>(-val[i].S)));
        } 
        dp=new_dp;
    }
    for(ll i=0;i<=k;i++){
        if(dp[getidx(i)]){
            //cout<<i<<' ';
            cout<<"YES\n";
            return;
        }
        if(dp[getidx(-i)]){
            //cout<<-i<<' ';
            cout<<"YES\n";
            return;
        }
    }
    cout<<"NO\n";
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 3 ms 10840 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 3 ms 10852 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 11036 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 4 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 3 ms 10936 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 3 ms 10872 KB Output is correct
20 Correct 3 ms 10840 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10840 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 3 ms 10840 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 3 ms 10852 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 11036 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 4 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 3 ms 10936 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 3 ms 10872 KB Output is correct
20 Correct 3 ms 10840 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10840 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
26 Correct 77 ms 11772 KB Output is correct
27 Correct 14 ms 11612 KB Output is correct
28 Correct 77 ms 11768 KB Output is correct
29 Correct 14 ms 11608 KB Output is correct
30 Correct 78 ms 11608 KB Output is correct
31 Correct 14 ms 11612 KB Output is correct
32 Correct 80 ms 11856 KB Output is correct
33 Correct 14 ms 11612 KB Output is correct
34 Correct 10 ms 11612 KB Output is correct
35 Correct 14 ms 11612 KB Output is correct
36 Correct 76 ms 11612 KB Output is correct
37 Correct 15 ms 11608 KB Output is correct
38 Correct 76 ms 11612 KB Output is correct
39 Correct 16 ms 11608 KB Output is correct
40 Correct 75 ms 11768 KB Output is correct
41 Correct 14 ms 11608 KB Output is correct
42 Correct 77 ms 11608 KB Output is correct
43 Correct 14 ms 11612 KB Output is correct
44 Correct 75 ms 11772 KB Output is correct
45 Correct 14 ms 11612 KB Output is correct
46 Correct 78 ms 11612 KB Output is correct
47 Correct 4 ms 11608 KB Output is correct
48 Correct 40 ms 11612 KB Output is correct
49 Correct 41 ms 11612 KB Output is correct
50 Correct 105 ms 11772 KB Output is correct
51 Correct 77 ms 11772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14424 KB Output is correct
2 Correct 13 ms 14428 KB Output is correct
3 Correct 12 ms 14680 KB Output is correct
4 Correct 12 ms 14428 KB Output is correct
5 Correct 19 ms 14428 KB Output is correct
6 Correct 14 ms 12892 KB Output is correct
7 Correct 12 ms 12892 KB Output is correct
8 Correct 12 ms 14528 KB Output is correct
9 Correct 12 ms 14428 KB Output is correct
10 Correct 13 ms 14424 KB Output is correct
11 Correct 11 ms 14408 KB Output is correct
12 Correct 22 ms 12888 KB Output is correct
13 Correct 20 ms 12856 KB Output is correct
14 Correct 16 ms 13072 KB Output is correct
15 Correct 21 ms 13020 KB Output is correct
16 Correct 13 ms 12892 KB Output is correct
17 Correct 14 ms 12968 KB Output is correct
18 Correct 19 ms 12892 KB Output is correct
19 Correct 20 ms 12892 KB Output is correct
20 Correct 13 ms 12888 KB Output is correct
21 Correct 14 ms 12892 KB Output is correct
22 Correct 26 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 3 ms 10840 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 3 ms 10852 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 11036 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 4 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 3 ms 10936 KB Output is correct
18 Correct 3 ms 10844 KB Output is correct
19 Correct 3 ms 10872 KB Output is correct
20 Correct 3 ms 10840 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 3 ms 10840 KB Output is correct
24 Correct 3 ms 10844 KB Output is correct
25 Correct 3 ms 10844 KB Output is correct
26 Correct 77 ms 11772 KB Output is correct
27 Correct 14 ms 11612 KB Output is correct
28 Correct 77 ms 11768 KB Output is correct
29 Correct 14 ms 11608 KB Output is correct
30 Correct 78 ms 11608 KB Output is correct
31 Correct 14 ms 11612 KB Output is correct
32 Correct 80 ms 11856 KB Output is correct
33 Correct 14 ms 11612 KB Output is correct
34 Correct 10 ms 11612 KB Output is correct
35 Correct 14 ms 11612 KB Output is correct
36 Correct 76 ms 11612 KB Output is correct
37 Correct 15 ms 11608 KB Output is correct
38 Correct 76 ms 11612 KB Output is correct
39 Correct 16 ms 11608 KB Output is correct
40 Correct 75 ms 11768 KB Output is correct
41 Correct 14 ms 11608 KB Output is correct
42 Correct 77 ms 11608 KB Output is correct
43 Correct 14 ms 11612 KB Output is correct
44 Correct 75 ms 11772 KB Output is correct
45 Correct 14 ms 11612 KB Output is correct
46 Correct 78 ms 11612 KB Output is correct
47 Correct 4 ms 11608 KB Output is correct
48 Correct 40 ms 11612 KB Output is correct
49 Correct 41 ms 11612 KB Output is correct
50 Correct 105 ms 11772 KB Output is correct
51 Correct 77 ms 11772 KB Output is correct
52 Correct 11 ms 14424 KB Output is correct
53 Correct 13 ms 14428 KB Output is correct
54 Correct 12 ms 14680 KB Output is correct
55 Correct 12 ms 14428 KB Output is correct
56 Correct 19 ms 14428 KB Output is correct
57 Correct 14 ms 12892 KB Output is correct
58 Correct 12 ms 12892 KB Output is correct
59 Correct 12 ms 14528 KB Output is correct
60 Correct 12 ms 14428 KB Output is correct
61 Correct 13 ms 14424 KB Output is correct
62 Correct 11 ms 14408 KB Output is correct
63 Correct 22 ms 12888 KB Output is correct
64 Correct 20 ms 12856 KB Output is correct
65 Correct 16 ms 13072 KB Output is correct
66 Correct 21 ms 13020 KB Output is correct
67 Correct 13 ms 12892 KB Output is correct
68 Correct 14 ms 12968 KB Output is correct
69 Correct 19 ms 12892 KB Output is correct
70 Correct 20 ms 12892 KB Output is correct
71 Correct 13 ms 12888 KB Output is correct
72 Correct 14 ms 12892 KB Output is correct
73 Correct 26 ms 12892 KB Output is correct
74 Correct 1163 ms 22100 KB Output is correct
75 Correct 109 ms 23204 KB Output is correct
76 Correct 1130 ms 23384 KB Output is correct
77 Correct 108 ms 23296 KB Output is correct
78 Correct 1166 ms 23388 KB Output is correct
79 Correct 1142 ms 23384 KB Output is correct
80 Correct 86 ms 23380 KB Output is correct
81 Correct 88 ms 23124 KB Output is correct
82 Correct 1094 ms 23380 KB Output is correct
83 Correct 1154 ms 23384 KB Output is correct
84 Correct 84 ms 23220 KB Output is correct
85 Correct 1119 ms 23384 KB Output is correct
86 Correct 89 ms 23256 KB Output is correct
87 Correct 1118 ms 23388 KB Output is correct
88 Correct 112 ms 23152 KB Output is correct
89 Correct 1121 ms 23388 KB Output is correct
90 Correct 88 ms 23372 KB Output is correct
91 Correct 1093 ms 23388 KB Output is correct
92 Correct 96 ms 23400 KB Output is correct
93 Correct 1118 ms 23388 KB Output is correct
94 Correct 39 ms 23120 KB Output is correct
95 Correct 611 ms 23380 KB Output is correct
96 Correct 603 ms 23288 KB Output is correct
97 Correct 1145 ms 23376 KB Output is correct
98 Correct 1115 ms 23380 KB Output is correct