답안 #940176

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940176 2024-03-07T06:24:59 Z irmuun Tug of War (BOI15_tug) C++17
100 / 100
350 ms 12624 KB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k;
    cin>>n>>k;
    vector<int>l(2*n),r(2*n),s(2*n);
    set<int>v[2*n];
    for(int i=0;i<2*n;i++){
        cin>>l[i]>>r[i]>>s[i];
        l[i]--;
        r[i]--;
        r[i]+=n;
        v[l[i]].insert(i);
        v[r[i]].insert(i);
    }
    queue<int>q;
    bool ok=true;
    for(int i=0;i<2*n;i++){
        if(v[i].size()==0){
            cout<<"NO";
            return 0;
        }
        if(v[i].size()==1){
            q.push(i);
        }
    }
    vector<int>w(2*n,-1);
    vector<bool>used(2*n,0);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        if(v[x].size()==0) continue;
        int y=*v[x].begin();
        used[y]=true;
        w[x]=s[y];
        v[l[y]].erase(y);
        v[r[y]].erase(y);
        if(v[l[y]].size()==1){
            q.push(l[y]);
        }
        if(v[r[y]].size()==1){
            q.push(r[y]);
        }
    }
    vector<int>adj[2*n];
    vector<int>num;
    function <void(int)> dfs=[&](int x){
        if(used[x]==true) return;
        num.pb(s[x]);
        used[x]=true;
        for(int y:adj[x]){
            dfs(y);
        }
    };
    for(int i=0;i<2*n;i++){
        if(w[i]==-1){
            int x=*v[i].begin();
            v[i].erase(v[i].begin());
            int y=*v[i].begin();
            v[i].erase(y);
            adj[x].pb(y);
            adj[y].pb(x);
        }
    }
    vector<int>dif;
    for(int i=0;i<2*n;i++){
        num.clear();
        dfs(i);
        if(!num.empty()){
            int cur=0;
            for(int j=0;j<(int)num.size();j++){
                if(j%2==0) cur+=num[j];
                else cur-=num[j];
            }
            dif.pb(abs(cur));
        }
    }
    int cur=0;
    for(int i=0;i<n;i++){
        if(w[i]>-1) cur+=w[i];
    }
    for(int i=n;i<2*n;i++){
        if(w[i]>-1) cur-=w[i];
    }
    dif.pb(abs(cur));
    sort(all(dif));
    bitset<300001>can;
    can[0]=1;
    int tot=0;
    for(int x:dif){
        tot+=x;
        can=can|(can<<x);
    }
    int mn=1e9;
    for(int i=0;i<=300000;i++){
        if(can[i]==1){
            mn=min(mn,abs(tot-2*i));
        }
    }
    if(mn<=k){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:27:10: warning: unused variable 'ok' [-Wunused-variable]
   27 |     bool ok=true;
      |          ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 564 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 568 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 564 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 560 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 564 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 568 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 564 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 560 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 21 ms 1368 KB Output is correct
27 Correct 6 ms 1372 KB Output is correct
28 Correct 23 ms 1628 KB Output is correct
29 Correct 5 ms 1372 KB Output is correct
30 Correct 23 ms 1400 KB Output is correct
31 Correct 5 ms 1232 KB Output is correct
32 Correct 21 ms 1624 KB Output is correct
33 Correct 5 ms 1372 KB Output is correct
34 Correct 4 ms 1372 KB Output is correct
35 Correct 5 ms 1372 KB Output is correct
36 Correct 20 ms 1408 KB Output is correct
37 Correct 5 ms 1376 KB Output is correct
38 Correct 20 ms 1372 KB Output is correct
39 Correct 5 ms 1240 KB Output is correct
40 Correct 21 ms 1372 KB Output is correct
41 Correct 5 ms 1376 KB Output is correct
42 Correct 21 ms 1396 KB Output is correct
43 Correct 7 ms 1884 KB Output is correct
44 Correct 23 ms 1624 KB Output is correct
45 Correct 6 ms 1368 KB Output is correct
46 Correct 21 ms 1376 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 12 ms 1384 KB Output is correct
49 Correct 12 ms 1624 KB Output is correct
50 Correct 22 ms 1340 KB Output is correct
51 Correct 21 ms 1404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 4440 KB Output is correct
2 Correct 8 ms 3672 KB Output is correct
3 Correct 58 ms 4608 KB Output is correct
4 Correct 8 ms 3672 KB Output is correct
5 Correct 59 ms 4612 KB Output is correct
6 Correct 8 ms 3676 KB Output is correct
7 Correct 65 ms 4604 KB Output is correct
8 Correct 8 ms 3680 KB Output is correct
9 Correct 57 ms 4440 KB Output is correct
10 Correct 8 ms 3684 KB Output is correct
11 Correct 65 ms 4532 KB Output is correct
12 Correct 8 ms 3676 KB Output is correct
13 Correct 58 ms 4432 KB Output is correct
14 Correct 58 ms 4444 KB Output is correct
15 Correct 10 ms 3676 KB Output is correct
16 Correct 62 ms 4444 KB Output is correct
17 Correct 10 ms 3672 KB Output is correct
18 Correct 76 ms 4700 KB Output is correct
19 Correct 8 ms 3672 KB Output is correct
20 Correct 60 ms 4444 KB Output is correct
21 Correct 8 ms 3672 KB Output is correct
22 Correct 35 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 564 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 568 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 564 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 560 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 21 ms 1368 KB Output is correct
27 Correct 6 ms 1372 KB Output is correct
28 Correct 23 ms 1628 KB Output is correct
29 Correct 5 ms 1372 KB Output is correct
30 Correct 23 ms 1400 KB Output is correct
31 Correct 5 ms 1232 KB Output is correct
32 Correct 21 ms 1624 KB Output is correct
33 Correct 5 ms 1372 KB Output is correct
34 Correct 4 ms 1372 KB Output is correct
35 Correct 5 ms 1372 KB Output is correct
36 Correct 20 ms 1408 KB Output is correct
37 Correct 5 ms 1376 KB Output is correct
38 Correct 20 ms 1372 KB Output is correct
39 Correct 5 ms 1240 KB Output is correct
40 Correct 21 ms 1372 KB Output is correct
41 Correct 5 ms 1376 KB Output is correct
42 Correct 21 ms 1396 KB Output is correct
43 Correct 7 ms 1884 KB Output is correct
44 Correct 23 ms 1624 KB Output is correct
45 Correct 6 ms 1368 KB Output is correct
46 Correct 21 ms 1376 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 12 ms 1384 KB Output is correct
49 Correct 12 ms 1624 KB Output is correct
50 Correct 22 ms 1340 KB Output is correct
51 Correct 21 ms 1404 KB Output is correct
52 Correct 59 ms 4440 KB Output is correct
53 Correct 8 ms 3672 KB Output is correct
54 Correct 58 ms 4608 KB Output is correct
55 Correct 8 ms 3672 KB Output is correct
56 Correct 59 ms 4612 KB Output is correct
57 Correct 8 ms 3676 KB Output is correct
58 Correct 65 ms 4604 KB Output is correct
59 Correct 8 ms 3680 KB Output is correct
60 Correct 57 ms 4440 KB Output is correct
61 Correct 8 ms 3684 KB Output is correct
62 Correct 65 ms 4532 KB Output is correct
63 Correct 8 ms 3676 KB Output is correct
64 Correct 58 ms 4432 KB Output is correct
65 Correct 58 ms 4444 KB Output is correct
66 Correct 10 ms 3676 KB Output is correct
67 Correct 62 ms 4444 KB Output is correct
68 Correct 10 ms 3672 KB Output is correct
69 Correct 76 ms 4700 KB Output is correct
70 Correct 8 ms 3672 KB Output is correct
71 Correct 60 ms 4444 KB Output is correct
72 Correct 8 ms 3672 KB Output is correct
73 Correct 35 ms 4444 KB Output is correct
74 Correct 350 ms 12388 KB Output is correct
75 Correct 55 ms 12116 KB Output is correct
76 Correct 347 ms 12440 KB Output is correct
77 Correct 77 ms 12184 KB Output is correct
78 Correct 310 ms 12444 KB Output is correct
79 Correct 339 ms 12316 KB Output is correct
80 Correct 64 ms 12456 KB Output is correct
81 Correct 56 ms 12192 KB Output is correct
82 Correct 299 ms 12124 KB Output is correct
83 Correct 315 ms 12364 KB Output is correct
84 Correct 52 ms 12060 KB Output is correct
85 Correct 318 ms 12372 KB Output is correct
86 Correct 47 ms 12116 KB Output is correct
87 Correct 324 ms 12496 KB Output is correct
88 Correct 48 ms 12028 KB Output is correct
89 Correct 327 ms 12320 KB Output is correct
90 Correct 67 ms 12176 KB Output is correct
91 Correct 331 ms 12624 KB Output is correct
92 Correct 63 ms 12068 KB Output is correct
93 Correct 323 ms 12452 KB Output is correct
94 Correct 30 ms 10324 KB Output is correct
95 Correct 192 ms 12312 KB Output is correct
96 Correct 178 ms 12624 KB Output is correct
97 Correct 318 ms 12432 KB Output is correct
98 Correct 311 ms 12440 KB Output is correct