Submission #871810

# Submission time Handle Problem Language Result Execution time Memory
871810 2023-11-11T15:55:52 Z 1075508020060209tc Tug of War (BOI15_tug) C++14
18 / 100
357 ms 30044 KB
//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
//using namespace __gnu_pbds;
using namespace std;
#define int long long
#define X first
#define Y second
int lowbit(int x){return x&-x;}
int n;int K;
vector<int>e[500005];
int vis[500005];
int vise[500005];
int dgr[500005];
int ar[500005];int br[500005];int cr[500005];
int basea;
int baseb;
bitset<600005>dp;
bitset<600005>dp2;
bitset<600005>zer;
int tota;int totb;
int tp;
void dfs(int nw){
vis[nw]=1;
//cout<<nw<<"kk"<<endl;
int cnt=0;
for(int i=0;i<e[nw].size();i++){
    int id=e[nw][i];
    if(vise[id]){continue;}
    int v=nw^ar[id]^br[id];
    if(tp==0){
        tota+=cr[id];
    }else{
        totb+=cr[id];
    }
    vise[id]=1;

    tp^=1;
    cnt++;
 //   if(vis[v]){continue;}
    dfs(v);
}
if(cnt>=2){
    exit(cnt);
}

}

int uf[500005];
int usz[500005];
int ues[500005];

int fin(int x){
if(uf[x]==x){return x;}
uf[x]=fin(uf[x]);
return uf[x];
}
void mrg(int a,int b){
int pa=fin(a);int pb=fin(b);
ues[pa]++;
if(pa==pb){return;}
usz[pa]+=usz[pb];
ues[pa]+=ues[pb];
uf[pb]=pa;
}


signed main(){
cin>>n>>K;
int ftot=0;
for(int i=1;i<=n+n;i++){
    int a;int b;int c;
    cin>>a>>b>>c;
    b+=n;
    ar[i]=a;br[i]=b;cr[i]=c;
    ftot+=c;
    dgr[a]++;
    dgr[b]++;
    e[a].push_back(i);
    e[b].push_back(i);
}

for(int i=1;i<=n+n;i++){
    if(dgr[i]==0){
        cout<<"NO\n";return 0;
    }
}

for(int i=1;i<=n+n;i++){
    uf[i]=i;
    usz[i]=1;
    ues[i]=0;
}
for(int i=1;i<=n+n;i++){
    //if(vise[i]){continue;}
    mrg(ar[i],br[i]);
}
for(int i=1;i<=n+n;i++){
    //if(vis[i]){continue;}
    if( usz[fin(i)]!=ues[fin(i)] ){
        cout<<"NO\n";return 0;
    }
}


queue<int>q;
for(int i=1;i<=n+n;i++){
    if(dgr[i]==1){
        q.push(i);
        vise[e[i][0]]=1;
        if( i<=n ){
            basea+=cr[e[i][0]];
        }
        vis[i]=1;
    }
}
while(q.size()){
    int nw=q.front();
    q.pop();
    vis[nw]=1;
    for(int i=0;i<e[nw].size();i++){
        int id=e[nw][i];
        if(vise[id]){continue;}
        int v=ar[i]^br[i]^nw;
        dgr[v]--;
        if(dgr[v]==1){
            for(int j=0;j<e[v].size();j++){
                int idd=e[v][j];
                if(vise[idd]){continue;}
                if(v<=n){
                    basea+=cr[idd];
                }
                vise[idd]=1;
            }
            q.push(v);
        }
    }
}/*
cout<<basea<<"\n";
for(int i=1;i<=n+n;i++){
    cout<<vis[i]<<" ";
}cout<<endl;
for(int i=1;i<=n+n;i++){
    cout<<vise[i]<<" ";
}*/



vector<pair<int,int>>vc;

for(int i=1;i<=n;i++){
    if(vis[i]){continue;}
    tota=0;
    totb=0;
    tp=0;
    dfs(i);
    vc.push_back({tota,totb});
}
dp[basea]=1;
for(int i=0;i<vc.size();i++){
    dp2=(dp<<vc[i].first);
    dp2=(dp2|(dp<<vc[i].second));
    swap(dp,dp2);
    dp2=zer;
}
for(int i=0;i<=n*20;i++){
    if(dp[i]&&(abs(ftot-i-i)<=K)){
        cout<<"YES\n";return 0;
    }
}
cout<<"NO\n";

}

Compilation message

tug.cpp: In function 'void dfs(long long int)':
tug.cpp:27:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:121:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for(int i=0;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
tug.cpp:127:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |             for(int j=0;j<e[v].size();j++){
      |                         ~^~~~~~~~~~~~
tug.cpp:160:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 | for(int i=0;i<vc.size();i++){
      |             ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 5 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 5 ms 24924 KB Output is correct
6 Correct 5 ms 25016 KB Output is correct
7 Correct 6 ms 25080 KB Output is correct
8 Correct 5 ms 24924 KB Output is correct
9 Correct 5 ms 24924 KB Output is correct
10 Correct 5 ms 24920 KB Output is correct
11 Correct 5 ms 24920 KB Output is correct
12 Correct 5 ms 24924 KB Output is correct
13 Correct 5 ms 25016 KB Output is correct
14 Correct 5 ms 24924 KB Output is correct
15 Correct 5 ms 25012 KB Output is correct
16 Correct 6 ms 24924 KB Output is correct
17 Correct 5 ms 24924 KB Output is correct
18 Correct 5 ms 25020 KB Output is correct
19 Correct 5 ms 24924 KB Output is correct
20 Correct 5 ms 24924 KB Output is correct
21 Correct 4 ms 20828 KB Output is correct
22 Correct 5 ms 29020 KB Output is correct
23 Correct 6 ms 29020 KB Output is correct
24 Correct 5 ms 25040 KB Output is correct
25 Correct 6 ms 24920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 5 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 5 ms 24924 KB Output is correct
6 Correct 5 ms 25016 KB Output is correct
7 Correct 6 ms 25080 KB Output is correct
8 Correct 5 ms 24924 KB Output is correct
9 Correct 5 ms 24924 KB Output is correct
10 Correct 5 ms 24920 KB Output is correct
11 Correct 5 ms 24920 KB Output is correct
12 Correct 5 ms 24924 KB Output is correct
13 Correct 5 ms 25016 KB Output is correct
14 Correct 5 ms 24924 KB Output is correct
15 Correct 5 ms 25012 KB Output is correct
16 Correct 6 ms 24924 KB Output is correct
17 Correct 5 ms 24924 KB Output is correct
18 Correct 5 ms 25020 KB Output is correct
19 Correct 5 ms 24924 KB Output is correct
20 Correct 5 ms 24924 KB Output is correct
21 Correct 4 ms 20828 KB Output is correct
22 Correct 5 ms 29020 KB Output is correct
23 Correct 6 ms 29020 KB Output is correct
24 Correct 5 ms 25040 KB Output is correct
25 Correct 6 ms 24920 KB Output is correct
26 Correct 71 ms 25180 KB Output is correct
27 Correct 15 ms 25176 KB Output is correct
28 Correct 69 ms 25300 KB Output is correct
29 Correct 15 ms 25180 KB Output is correct
30 Correct 67 ms 25284 KB Output is correct
31 Correct 15 ms 25180 KB Output is correct
32 Correct 68 ms 25292 KB Output is correct
33 Correct 15 ms 25180 KB Output is correct
34 Correct 11 ms 25128 KB Output is correct
35 Correct 15 ms 25272 KB Output is correct
36 Correct 65 ms 25180 KB Output is correct
37 Correct 15 ms 25180 KB Output is correct
38 Correct 66 ms 25296 KB Output is correct
39 Correct 15 ms 25272 KB Output is correct
40 Correct 74 ms 25292 KB Output is correct
41 Correct 15 ms 25180 KB Output is correct
42 Correct 65 ms 25296 KB Output is correct
43 Correct 15 ms 25180 KB Output is correct
44 Correct 66 ms 25180 KB Output is correct
45 Correct 17 ms 25248 KB Output is correct
46 Correct 78 ms 25296 KB Output is correct
47 Correct 6 ms 20824 KB Output is correct
48 Runtime error 7 ms 29276 KB Execution failed because the return code was nonzero
49 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 309 ms 26460 KB Output is correct
2 Correct 15 ms 21596 KB Output is correct
3 Correct 327 ms 26460 KB Output is correct
4 Correct 15 ms 21596 KB Output is correct
5 Correct 350 ms 26456 KB Output is correct
6 Correct 17 ms 21592 KB Output is correct
7 Correct 315 ms 26444 KB Output is correct
8 Correct 24 ms 21592 KB Output is correct
9 Correct 338 ms 26460 KB Output is correct
10 Correct 19 ms 21596 KB Output is correct
11 Correct 331 ms 26384 KB Output is correct
12 Correct 16 ms 21848 KB Output is correct
13 Correct 357 ms 26460 KB Output is correct
14 Correct 332 ms 26476 KB Output is correct
15 Correct 17 ms 21596 KB Output is correct
16 Correct 332 ms 26492 KB Output is correct
17 Correct 20 ms 21672 KB Output is correct
18 Correct 329 ms 26460 KB Output is correct
19 Correct 18 ms 21556 KB Output is correct
20 Correct 336 ms 26496 KB Output is correct
21 Correct 16 ms 21616 KB Output is correct
22 Runtime error 18 ms 30044 KB Execution failed because the return code was nonzero
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 5 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 5 ms 24924 KB Output is correct
6 Correct 5 ms 25016 KB Output is correct
7 Correct 6 ms 25080 KB Output is correct
8 Correct 5 ms 24924 KB Output is correct
9 Correct 5 ms 24924 KB Output is correct
10 Correct 5 ms 24920 KB Output is correct
11 Correct 5 ms 24920 KB Output is correct
12 Correct 5 ms 24924 KB Output is correct
13 Correct 5 ms 25016 KB Output is correct
14 Correct 5 ms 24924 KB Output is correct
15 Correct 5 ms 25012 KB Output is correct
16 Correct 6 ms 24924 KB Output is correct
17 Correct 5 ms 24924 KB Output is correct
18 Correct 5 ms 25020 KB Output is correct
19 Correct 5 ms 24924 KB Output is correct
20 Correct 5 ms 24924 KB Output is correct
21 Correct 4 ms 20828 KB Output is correct
22 Correct 5 ms 29020 KB Output is correct
23 Correct 6 ms 29020 KB Output is correct
24 Correct 5 ms 25040 KB Output is correct
25 Correct 6 ms 24920 KB Output is correct
26 Correct 71 ms 25180 KB Output is correct
27 Correct 15 ms 25176 KB Output is correct
28 Correct 69 ms 25300 KB Output is correct
29 Correct 15 ms 25180 KB Output is correct
30 Correct 67 ms 25284 KB Output is correct
31 Correct 15 ms 25180 KB Output is correct
32 Correct 68 ms 25292 KB Output is correct
33 Correct 15 ms 25180 KB Output is correct
34 Correct 11 ms 25128 KB Output is correct
35 Correct 15 ms 25272 KB Output is correct
36 Correct 65 ms 25180 KB Output is correct
37 Correct 15 ms 25180 KB Output is correct
38 Correct 66 ms 25296 KB Output is correct
39 Correct 15 ms 25272 KB Output is correct
40 Correct 74 ms 25292 KB Output is correct
41 Correct 15 ms 25180 KB Output is correct
42 Correct 65 ms 25296 KB Output is correct
43 Correct 15 ms 25180 KB Output is correct
44 Correct 66 ms 25180 KB Output is correct
45 Correct 17 ms 25248 KB Output is correct
46 Correct 78 ms 25296 KB Output is correct
47 Correct 6 ms 20824 KB Output is correct
48 Runtime error 7 ms 29276 KB Execution failed because the return code was nonzero
49 Halted 0 ms 0 KB -