Submission #1022741

# Submission time Handle Problem Language Result Execution time Memory
1022741 2024-07-14T04:25:15 Z guagua0407 Tug of War (BOI15_tug) C++17
100 / 100
966 ms 6160 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=6e4+5;
int l[mxn],r[mxn],S[mxn];
bool vis[mxn];
bool usedl[mxn],usedr[mxn];

int main() {_
    int n,k;
    cin>>n>>k;
    vector<vector<int>> ls(n),rs(n);
    int all=0;
    for(int i=0;i<2*n;i++){
        cin>>l[i]>>r[i]>>S[i];
        all+=S[i];
        l[i]--;
        r[i]--;
        ls[l[i]].push_back(i);
        rs[r[i]].push_back(i);
    }
    for(int i=0;i<n;i++){
        if(ls[i].empty() or rs[i].empty()){
            cout<<"NO"<<'\n';
            return 0;
        }
    }
    int ans=0;
    for(int i=0;i<2*n;i++){
        if(vis[i]) continue;
        int state=-1;
        {
            int cnt=0;
            for(auto v:ls[l[i]]){
                if(!vis[v]) cnt++;
            }
            if(cnt==1){
                state=0;
            }
        }
        {
            int cnt=0;
            for(auto v:rs[r[i]]){
                if(!vis[v]) cnt++;
            }
            if(cnt==1){
                state=1;
            }
        }
        if(state==-1) continue;
        int x=i;
        while(x!=-1){
            vis[x]=true;
            int cnt=0;
            int nxt;
            if(state==0){
                usedl[l[x]]=true;
                state=1;
                ans+=S[x];
                for(auto v:rs[r[x]]){
                    if(!vis[v]){
                        cnt++;
                        nxt=v;
                    }
                }
                if(cnt==1) x=nxt;
                else x=-1;
            }
            else{
                usedr[r[x]]=true;
                state=0;
                for(auto v:ls[l[x]]){
                    if(!vis[v]){
                        cnt++;
                        nxt=v;
                    }
                }
                if(cnt==1) x=nxt;
                else x=-1;
            }
        }
    }
    for(int i=0;i<n;i++){
        {
            int cnt=0;
            for(auto v:ls[i]){
                if(!vis[v]) cnt++;
            }
            if(!usedl[i] and cnt!=2){
                cout<<"NO"<<'\n';
                return 0;
            }
        }
        {
            int cnt=0;
            for(auto v:rs[i]){
                if(!vis[v]) cnt++;
            }
            if(!usedr[i] and cnt!=2){
                cout<<"NO"<<'\n';
                return 0;
            }
        }
    }
    vector<int> cand;
    for(int i=0;i<2*n;i++){
        if(vis[i]) continue;
        int x=i;
        int a=0;
        int b=0;
        int state=0;
        while(x!=-1){
            vis[x]=true;
            if(state==0){
                state=1;
                a+=S[x];
                bool tf=false;
                for(auto v:ls[l[x]]){
                    if(!vis[v]){
                        x=v;
                        tf=true;
                        break;
                    }
                }
                if(!tf) x=-1;
            }
            else{
                state=0;
                b+=S[x];
                bool tf=false;
                for(auto v:rs[r[x]]){
                    if(!vis[v]){
                        x=v;
                        tf=true;
                        break;
                    }
                }
                if(!tf) x=-1;
            }
        }
        ans+=a;
        cand.push_back(b-a);
    }
    bitset<mxn*20> b;
    b[ans]=1;
    for(auto v:cand){
        if(v>=0) b|=(b<<v);
        else b|=(b>>(-v));
    }
    int cur=all/2;
    while((all-cur)-cur<=k){
        if(b[cur] or b[all-cur]){
            cout<<"YES"<<'\n';
            return 0;
        }
        cur--;
    }
    cout<<"NO"<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

tug.cpp: In function 'void setIO(std::string)':
tug.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tug.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:68:19: warning: 'nxt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   68 |             vis[x]=true;
      |             ~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 900 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 940 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 0 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 900 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 940 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 0 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 58 ms 1372 KB Output is correct
27 Correct 9 ms 1372 KB Output is correct
28 Correct 60 ms 1372 KB Output is correct
29 Correct 14 ms 1156 KB Output is correct
30 Correct 58 ms 1372 KB Output is correct
31 Correct 10 ms 1136 KB Output is correct
32 Correct 59 ms 1372 KB Output is correct
33 Correct 10 ms 1368 KB Output is correct
34 Correct 7 ms 1372 KB Output is correct
35 Correct 15 ms 1384 KB Output is correct
36 Correct 69 ms 1372 KB Output is correct
37 Correct 13 ms 1376 KB Output is correct
38 Correct 71 ms 1380 KB Output is correct
39 Correct 13 ms 1372 KB Output is correct
40 Correct 64 ms 1372 KB Output is correct
41 Correct 10 ms 1116 KB Output is correct
42 Correct 89 ms 1168 KB Output is correct
43 Correct 11 ms 1372 KB Output is correct
44 Correct 61 ms 1372 KB Output is correct
45 Correct 10 ms 1372 KB Output is correct
46 Correct 64 ms 1388 KB Output is correct
47 Correct 2 ms 1120 KB Output is correct
48 Correct 30 ms 1388 KB Output is correct
49 Correct 30 ms 1376 KB Output is correct
50 Correct 84 ms 1376 KB Output is correct
51 Correct 61 ms 1376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 2396 KB Output is correct
2 Correct 6 ms 2392 KB Output is correct
3 Correct 149 ms 2584 KB Output is correct
4 Correct 7 ms 2392 KB Output is correct
5 Correct 145 ms 2396 KB Output is correct
6 Correct 5 ms 2392 KB Output is correct
7 Correct 153 ms 2576 KB Output is correct
8 Correct 6 ms 2392 KB Output is correct
9 Correct 150 ms 2584 KB Output is correct
10 Correct 5 ms 2220 KB Output is correct
11 Correct 146 ms 2400 KB Output is correct
12 Correct 5 ms 2392 KB Output is correct
13 Correct 148 ms 2648 KB Output is correct
14 Correct 150 ms 2392 KB Output is correct
15 Correct 5 ms 2272 KB Output is correct
16 Correct 149 ms 2588 KB Output is correct
17 Correct 5 ms 2392 KB Output is correct
18 Correct 151 ms 2480 KB Output is correct
19 Correct 5 ms 2392 KB Output is correct
20 Correct 149 ms 2396 KB Output is correct
21 Correct 6 ms 2272 KB Output is correct
22 Correct 84 ms 2556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 900 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 860 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 940 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 0 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 58 ms 1372 KB Output is correct
27 Correct 9 ms 1372 KB Output is correct
28 Correct 60 ms 1372 KB Output is correct
29 Correct 14 ms 1156 KB Output is correct
30 Correct 58 ms 1372 KB Output is correct
31 Correct 10 ms 1136 KB Output is correct
32 Correct 59 ms 1372 KB Output is correct
33 Correct 10 ms 1368 KB Output is correct
34 Correct 7 ms 1372 KB Output is correct
35 Correct 15 ms 1384 KB Output is correct
36 Correct 69 ms 1372 KB Output is correct
37 Correct 13 ms 1376 KB Output is correct
38 Correct 71 ms 1380 KB Output is correct
39 Correct 13 ms 1372 KB Output is correct
40 Correct 64 ms 1372 KB Output is correct
41 Correct 10 ms 1116 KB Output is correct
42 Correct 89 ms 1168 KB Output is correct
43 Correct 11 ms 1372 KB Output is correct
44 Correct 61 ms 1372 KB Output is correct
45 Correct 10 ms 1372 KB Output is correct
46 Correct 64 ms 1388 KB Output is correct
47 Correct 2 ms 1120 KB Output is correct
48 Correct 30 ms 1388 KB Output is correct
49 Correct 30 ms 1376 KB Output is correct
50 Correct 84 ms 1376 KB Output is correct
51 Correct 61 ms 1376 KB Output is correct
52 Correct 159 ms 2396 KB Output is correct
53 Correct 6 ms 2392 KB Output is correct
54 Correct 149 ms 2584 KB Output is correct
55 Correct 7 ms 2392 KB Output is correct
56 Correct 145 ms 2396 KB Output is correct
57 Correct 5 ms 2392 KB Output is correct
58 Correct 153 ms 2576 KB Output is correct
59 Correct 6 ms 2392 KB Output is correct
60 Correct 150 ms 2584 KB Output is correct
61 Correct 5 ms 2220 KB Output is correct
62 Correct 146 ms 2400 KB Output is correct
63 Correct 5 ms 2392 KB Output is correct
64 Correct 148 ms 2648 KB Output is correct
65 Correct 150 ms 2392 KB Output is correct
66 Correct 5 ms 2272 KB Output is correct
67 Correct 149 ms 2588 KB Output is correct
68 Correct 5 ms 2392 KB Output is correct
69 Correct 151 ms 2480 KB Output is correct
70 Correct 5 ms 2392 KB Output is correct
71 Correct 149 ms 2396 KB Output is correct
72 Correct 6 ms 2272 KB Output is correct
73 Correct 84 ms 2556 KB Output is correct
74 Correct 877 ms 5980 KB Output is correct
75 Correct 51 ms 5720 KB Output is correct
76 Correct 957 ms 5976 KB Output is correct
77 Correct 52 ms 5724 KB Output is correct
78 Correct 966 ms 5960 KB Output is correct
79 Correct 913 ms 5980 KB Output is correct
80 Correct 56 ms 5976 KB Output is correct
81 Correct 53 ms 5712 KB Output is correct
82 Correct 835 ms 5988 KB Output is correct
83 Correct 953 ms 6152 KB Output is correct
84 Correct 50 ms 5720 KB Output is correct
85 Correct 878 ms 5976 KB Output is correct
86 Correct 53 ms 5720 KB Output is correct
87 Correct 922 ms 6144 KB Output is correct
88 Correct 50 ms 5724 KB Output is correct
89 Correct 867 ms 6160 KB Output is correct
90 Correct 65 ms 5912 KB Output is correct
91 Correct 894 ms 5936 KB Output is correct
92 Correct 55 ms 5724 KB Output is correct
93 Correct 883 ms 5976 KB Output is correct
94 Correct 16 ms 5468 KB Output is correct
95 Correct 533 ms 6080 KB Output is correct
96 Correct 469 ms 5972 KB Output is correct
97 Correct 951 ms 6128 KB Output is correct
98 Correct 916 ms 6144 KB Output is correct