답안 #871828

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
871828 2023-11-11T16:24:59 Z 1075508020060209tc Tug of War (BOI15_tug) C++14
100 / 100
966 ms 31316 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);
}//return;
if(cnt>=3){
    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.tie(0);
ios_base::sync_with_stdio(0);    

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++){
    mrg(ar[i],br[i]);
}
for(int i=1;i<=n+n;i++){
    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);
    }
}
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[id]^br[id]^nw;
        dgr[v]--;
        if(dgr[v]==1){
            q.push(v);
        }
        if(nw<=n){
            basea+=cr[id];
        }
        vise[id]=1;
    }
}/*
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]<<" ";
}*/

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

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(int)':
tug.cpp:27:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     for(int i=0;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
tug.cpp:156:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 | for(int i=0;i<vc.size();i++){
      |             ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 25688 KB Output is correct
2 Correct 5 ms 25944 KB Output is correct
3 Correct 5 ms 25692 KB Output is correct
4 Correct 5 ms 25692 KB Output is correct
5 Correct 5 ms 25692 KB Output is correct
6 Correct 5 ms 25692 KB Output is correct
7 Correct 5 ms 25688 KB Output is correct
8 Correct 6 ms 25692 KB Output is correct
9 Correct 5 ms 25692 KB Output is correct
10 Correct 5 ms 25692 KB Output is correct
11 Correct 5 ms 25692 KB Output is correct
12 Correct 5 ms 25688 KB Output is correct
13 Correct 5 ms 25692 KB Output is correct
14 Correct 6 ms 25692 KB Output is correct
15 Correct 5 ms 25692 KB Output is correct
16 Correct 5 ms 25692 KB Output is correct
17 Correct 5 ms 25692 KB Output is correct
18 Correct 5 ms 25692 KB Output is correct
19 Correct 5 ms 25692 KB Output is correct
20 Correct 5 ms 25916 KB Output is correct
21 Correct 5 ms 21596 KB Output is correct
22 Correct 5 ms 27740 KB Output is correct
23 Correct 5 ms 27740 KB Output is correct
24 Correct 5 ms 25880 KB Output is correct
25 Correct 5 ms 25692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 25688 KB Output is correct
2 Correct 5 ms 25944 KB Output is correct
3 Correct 5 ms 25692 KB Output is correct
4 Correct 5 ms 25692 KB Output is correct
5 Correct 5 ms 25692 KB Output is correct
6 Correct 5 ms 25692 KB Output is correct
7 Correct 5 ms 25688 KB Output is correct
8 Correct 6 ms 25692 KB Output is correct
9 Correct 5 ms 25692 KB Output is correct
10 Correct 5 ms 25692 KB Output is correct
11 Correct 5 ms 25692 KB Output is correct
12 Correct 5 ms 25688 KB Output is correct
13 Correct 5 ms 25692 KB Output is correct
14 Correct 6 ms 25692 KB Output is correct
15 Correct 5 ms 25692 KB Output is correct
16 Correct 5 ms 25692 KB Output is correct
17 Correct 5 ms 25692 KB Output is correct
18 Correct 5 ms 25692 KB Output is correct
19 Correct 5 ms 25692 KB Output is correct
20 Correct 5 ms 25916 KB Output is correct
21 Correct 5 ms 21596 KB Output is correct
22 Correct 5 ms 27740 KB Output is correct
23 Correct 5 ms 27740 KB Output is correct
24 Correct 5 ms 25880 KB Output is correct
25 Correct 5 ms 25692 KB Output is correct
26 Correct 67 ms 25948 KB Output is correct
27 Correct 13 ms 25948 KB Output is correct
28 Correct 65 ms 25944 KB Output is correct
29 Correct 14 ms 25944 KB Output is correct
30 Correct 72 ms 25944 KB Output is correct
31 Correct 15 ms 25948 KB Output is correct
32 Correct 68 ms 25948 KB Output is correct
33 Correct 14 ms 25948 KB Output is correct
34 Correct 10 ms 25948 KB Output is correct
35 Correct 14 ms 25948 KB Output is correct
36 Correct 64 ms 25944 KB Output is correct
37 Correct 14 ms 25948 KB Output is correct
38 Correct 67 ms 26096 KB Output is correct
39 Correct 15 ms 26100 KB Output is correct
40 Correct 64 ms 25948 KB Output is correct
41 Correct 14 ms 25944 KB Output is correct
42 Correct 69 ms 25944 KB Output is correct
43 Correct 14 ms 25948 KB Output is correct
44 Correct 69 ms 26092 KB Output is correct
45 Correct 14 ms 25948 KB Output is correct
46 Correct 68 ms 26096 KB Output is correct
47 Correct 5 ms 21852 KB Output is correct
48 Correct 36 ms 27996 KB Output is correct
49 Correct 38 ms 27996 KB Output is correct
50 Correct 67 ms 25948 KB Output is correct
51 Correct 66 ms 26148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 26972 KB Output is correct
2 Correct 9 ms 22364 KB Output is correct
3 Correct 314 ms 26972 KB Output is correct
4 Correct 9 ms 22364 KB Output is correct
5 Correct 316 ms 26972 KB Output is correct
6 Correct 9 ms 22360 KB Output is correct
7 Correct 323 ms 27160 KB Output is correct
8 Correct 8 ms 22360 KB Output is correct
9 Correct 314 ms 26972 KB Output is correct
10 Correct 9 ms 22360 KB Output is correct
11 Correct 322 ms 26908 KB Output is correct
12 Correct 9 ms 22360 KB Output is correct
13 Correct 307 ms 26968 KB Output is correct
14 Correct 317 ms 27160 KB Output is correct
15 Correct 9 ms 22364 KB Output is correct
16 Correct 319 ms 26896 KB Output is correct
17 Correct 9 ms 22364 KB Output is correct
18 Correct 313 ms 26972 KB Output is correct
19 Correct 9 ms 22364 KB Output is correct
20 Correct 310 ms 26760 KB Output is correct
21 Correct 9 ms 22360 KB Output is correct
22 Correct 161 ms 28852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 25688 KB Output is correct
2 Correct 5 ms 25944 KB Output is correct
3 Correct 5 ms 25692 KB Output is correct
4 Correct 5 ms 25692 KB Output is correct
5 Correct 5 ms 25692 KB Output is correct
6 Correct 5 ms 25692 KB Output is correct
7 Correct 5 ms 25688 KB Output is correct
8 Correct 6 ms 25692 KB Output is correct
9 Correct 5 ms 25692 KB Output is correct
10 Correct 5 ms 25692 KB Output is correct
11 Correct 5 ms 25692 KB Output is correct
12 Correct 5 ms 25688 KB Output is correct
13 Correct 5 ms 25692 KB Output is correct
14 Correct 6 ms 25692 KB Output is correct
15 Correct 5 ms 25692 KB Output is correct
16 Correct 5 ms 25692 KB Output is correct
17 Correct 5 ms 25692 KB Output is correct
18 Correct 5 ms 25692 KB Output is correct
19 Correct 5 ms 25692 KB Output is correct
20 Correct 5 ms 25916 KB Output is correct
21 Correct 5 ms 21596 KB Output is correct
22 Correct 5 ms 27740 KB Output is correct
23 Correct 5 ms 27740 KB Output is correct
24 Correct 5 ms 25880 KB Output is correct
25 Correct 5 ms 25692 KB Output is correct
26 Correct 67 ms 25948 KB Output is correct
27 Correct 13 ms 25948 KB Output is correct
28 Correct 65 ms 25944 KB Output is correct
29 Correct 14 ms 25944 KB Output is correct
30 Correct 72 ms 25944 KB Output is correct
31 Correct 15 ms 25948 KB Output is correct
32 Correct 68 ms 25948 KB Output is correct
33 Correct 14 ms 25948 KB Output is correct
34 Correct 10 ms 25948 KB Output is correct
35 Correct 14 ms 25948 KB Output is correct
36 Correct 64 ms 25944 KB Output is correct
37 Correct 14 ms 25948 KB Output is correct
38 Correct 67 ms 26096 KB Output is correct
39 Correct 15 ms 26100 KB Output is correct
40 Correct 64 ms 25948 KB Output is correct
41 Correct 14 ms 25944 KB Output is correct
42 Correct 69 ms 25944 KB Output is correct
43 Correct 14 ms 25948 KB Output is correct
44 Correct 69 ms 26092 KB Output is correct
45 Correct 14 ms 25948 KB Output is correct
46 Correct 68 ms 26096 KB Output is correct
47 Correct 5 ms 21852 KB Output is correct
48 Correct 36 ms 27996 KB Output is correct
49 Correct 38 ms 27996 KB Output is correct
50 Correct 67 ms 25948 KB Output is correct
51 Correct 66 ms 26148 KB Output is correct
52 Correct 317 ms 26972 KB Output is correct
53 Correct 9 ms 22364 KB Output is correct
54 Correct 314 ms 26972 KB Output is correct
55 Correct 9 ms 22364 KB Output is correct
56 Correct 316 ms 26972 KB Output is correct
57 Correct 9 ms 22360 KB Output is correct
58 Correct 323 ms 27160 KB Output is correct
59 Correct 8 ms 22360 KB Output is correct
60 Correct 314 ms 26972 KB Output is correct
61 Correct 9 ms 22360 KB Output is correct
62 Correct 322 ms 26908 KB Output is correct
63 Correct 9 ms 22360 KB Output is correct
64 Correct 307 ms 26968 KB Output is correct
65 Correct 317 ms 27160 KB Output is correct
66 Correct 9 ms 22364 KB Output is correct
67 Correct 319 ms 26896 KB Output is correct
68 Correct 9 ms 22364 KB Output is correct
69 Correct 313 ms 26972 KB Output is correct
70 Correct 9 ms 22364 KB Output is correct
71 Correct 310 ms 26760 KB Output is correct
72 Correct 9 ms 22360 KB Output is correct
73 Correct 161 ms 28852 KB Output is correct
74 Correct 912 ms 29512 KB Output is correct
75 Correct 60 ms 29256 KB Output is correct
76 Correct 935 ms 29524 KB Output is correct
77 Correct 58 ms 29276 KB Output is correct
78 Correct 918 ms 29512 KB Output is correct
79 Correct 937 ms 29528 KB Output is correct
80 Correct 63 ms 29276 KB Output is correct
81 Correct 59 ms 29020 KB Output is correct
82 Correct 941 ms 29404 KB Output is correct
83 Correct 937 ms 29524 KB Output is correct
84 Correct 57 ms 29012 KB Output is correct
85 Correct 932 ms 29532 KB Output is correct
86 Correct 57 ms 29044 KB Output is correct
87 Correct 957 ms 29532 KB Output is correct
88 Correct 67 ms 29524 KB Output is correct
89 Correct 936 ms 29532 KB Output is correct
90 Correct 57 ms 29012 KB Output is correct
91 Correct 945 ms 29532 KB Output is correct
92 Correct 56 ms 29256 KB Output is correct
93 Correct 961 ms 29524 KB Output is correct
94 Correct 20 ms 24156 KB Output is correct
95 Correct 498 ms 31236 KB Output is correct
96 Correct 473 ms 31316 KB Output is correct
97 Correct 966 ms 29524 KB Output is correct
98 Correct 928 ms 29524 KB Output is correct