Submission #887625

# Submission time Handle Problem Language Result Execution time Memory
887625 2023-12-14T20:54:12 Z Shayan86 Tug of War (BOI15_tug) C++14
100 / 100
1571 ms 7952 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll N = 6e4 + 50;
const ll Mod = 1e9 + 7;

ll n, k, s[N], par[N], h[N], f[N], sum, cnt;
pair<pii, int> edge;

bool mark[N];
vector<ll> vec;
vector<pii> adj[N];

void dfs(int v){
    mark[v] = 1;
    for(auto [u, w]: adj[v]){
        if(u == par[v]) continue;
        else if(mark[u]){
            edge = Mp(Mp(v, u), w);
            continue;
        }
        par[u] = v; h[u] = h[v] + 1; f[u] = w - f[v]; dfs(u);

        if(h[v] % 2) sum += w;
        else sum -= w;
    }
}

const ll M = 10 * N;

bitset<2*M> diff;

void check(int v){
    mark[v] = 1;
    for(auto [u, w]: adj[v]){
        if(!mark[u]) check(u);
    }
    cnt++; sum += adj[v].size();
}

int main(){
    fast_io;

    cin >> n >> k;

    int l, r;
    for(int i = 1; i <= 2*n; i++){
        cin >> l >> r >> s[i]; r += n;
        adj[l].pb(Mp(r, s[i]));
        adj[r].pb(Mp(l, s[i]));
    }

    for(int i = 1; i <= 2*n; i++){
        if(!mark[i]){
            sum = 0; cnt = 0; check(i);
            if(sum != cnt * 2) kill("NO");
        }
    }
    fill(mark, mark + N, 0);

    diff[M] = 1;
    for(int i = 1; i <= 2*n; i++){
        if(!mark[i]){
            sum = 0; dfs(i);
            
            int v = edge.F.F;
            int u = edge.F.S;
            int w = edge.S;

            if(h[v] % 2) sum -= w;
            else sum += w;

            ll sum2 = sum; // f[u] + f[v] - w
            if(h[v] % 2) sum2 -= (f[u] + f[v] - w);
            else sum2 += (f[u] + f[v] - w);

            ll sum3 = sum2;
            if(h[v] % 2) sum3 += 2 * f[v];
            else sum3 -= 2  * f[v];
            
            if(sum3 >= 0) diff = diff << sum3;
            else diff = diff >> (-sum3);

            vec.pb(abs(sum - sum2));
            //cout << sum2 << sep << sum - sum2 << endl;
        }
    }

    for(auto x: vec){
        diff = (diff >> x) | (diff << x);
    }

    ll res = Mod;
    for(int i = 0; i < 2*M; i++) if(diff[i]) res = min(res, abs(M-i));

    //cout << res << endl;
    if(res <= k) cout << "YES";
    else cout << "NO";

}

Compilation message

tug.cpp: In function 'void dfs(int)':
tug.cpp:38:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   38 |     for(auto [u, w]: adj[v]){
      |              ^
tug.cpp: In function 'void check(int)':
tug.cpp:57:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   57 |     for(auto [u, w]: adj[v]){
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Correct 2 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 3260 KB Output is correct
5 Correct 2 ms 3424 KB Output is correct
6 Correct 2 ms 3416 KB Output is correct
7 Correct 2 ms 3416 KB Output is correct
8 Correct 2 ms 3420 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3420 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3420 KB Output is correct
13 Correct 2 ms 3420 KB Output is correct
14 Correct 2 ms 3420 KB Output is correct
15 Correct 2 ms 3420 KB Output is correct
16 Correct 2 ms 3248 KB Output is correct
17 Correct 2 ms 3420 KB Output is correct
18 Correct 2 ms 3420 KB Output is correct
19 Correct 2 ms 3420 KB Output is correct
20 Correct 2 ms 3420 KB Output is correct
21 Correct 1 ms 2908 KB Output is correct
22 Correct 2 ms 3420 KB Output is correct
23 Correct 2 ms 3420 KB Output is correct
24 Correct 3 ms 3420 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Correct 2 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 3260 KB Output is correct
5 Correct 2 ms 3424 KB Output is correct
6 Correct 2 ms 3416 KB Output is correct
7 Correct 2 ms 3416 KB Output is correct
8 Correct 2 ms 3420 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3420 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3420 KB Output is correct
13 Correct 2 ms 3420 KB Output is correct
14 Correct 2 ms 3420 KB Output is correct
15 Correct 2 ms 3420 KB Output is correct
16 Correct 2 ms 3248 KB Output is correct
17 Correct 2 ms 3420 KB Output is correct
18 Correct 2 ms 3420 KB Output is correct
19 Correct 2 ms 3420 KB Output is correct
20 Correct 2 ms 3420 KB Output is correct
21 Correct 1 ms 2908 KB Output is correct
22 Correct 2 ms 3420 KB Output is correct
23 Correct 2 ms 3420 KB Output is correct
24 Correct 3 ms 3420 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
26 Correct 106 ms 3420 KB Output is correct
27 Correct 16 ms 3420 KB Output is correct
28 Correct 100 ms 3416 KB Output is correct
29 Correct 16 ms 3416 KB Output is correct
30 Correct 106 ms 3620 KB Output is correct
31 Correct 17 ms 3420 KB Output is correct
32 Correct 102 ms 3608 KB Output is correct
33 Correct 16 ms 3416 KB Output is correct
34 Correct 10 ms 3420 KB Output is correct
35 Correct 16 ms 3620 KB Output is correct
36 Correct 105 ms 3416 KB Output is correct
37 Correct 16 ms 3420 KB Output is correct
38 Correct 105 ms 3608 KB Output is correct
39 Correct 16 ms 3672 KB Output is correct
40 Correct 106 ms 3604 KB Output is correct
41 Correct 17 ms 3416 KB Output is correct
42 Correct 104 ms 3608 KB Output is correct
43 Correct 17 ms 3420 KB Output is correct
44 Correct 103 ms 3616 KB Output is correct
45 Correct 16 ms 3420 KB Output is correct
46 Correct 108 ms 3420 KB Output is correct
47 Correct 2 ms 3164 KB Output is correct
48 Correct 64 ms 3640 KB Output is correct
49 Correct 63 ms 3624 KB Output is correct
50 Correct 109 ms 3676 KB Output is correct
51 Correct 105 ms 3652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 330 ms 4432 KB Output is correct
2 Correct 6 ms 3928 KB Output is correct
3 Correct 351 ms 4448 KB Output is correct
4 Correct 6 ms 3932 KB Output is correct
5 Correct 334 ms 4352 KB Output is correct
6 Correct 6 ms 3932 KB Output is correct
7 Correct 357 ms 4600 KB Output is correct
8 Correct 6 ms 3932 KB Output is correct
9 Correct 379 ms 4500 KB Output is correct
10 Correct 7 ms 3932 KB Output is correct
11 Correct 327 ms 4432 KB Output is correct
12 Correct 6 ms 3928 KB Output is correct
13 Correct 358 ms 4656 KB Output is correct
14 Correct 334 ms 4432 KB Output is correct
15 Correct 6 ms 3928 KB Output is correct
16 Correct 350 ms 4476 KB Output is correct
17 Correct 6 ms 3928 KB Output is correct
18 Correct 353 ms 4436 KB Output is correct
19 Correct 6 ms 3928 KB Output is correct
20 Correct 340 ms 4432 KB Output is correct
21 Correct 6 ms 3932 KB Output is correct
22 Correct 215 ms 4504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3420 KB Output is correct
2 Correct 2 ms 3420 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 3260 KB Output is correct
5 Correct 2 ms 3424 KB Output is correct
6 Correct 2 ms 3416 KB Output is correct
7 Correct 2 ms 3416 KB Output is correct
8 Correct 2 ms 3420 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3420 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3420 KB Output is correct
13 Correct 2 ms 3420 KB Output is correct
14 Correct 2 ms 3420 KB Output is correct
15 Correct 2 ms 3420 KB Output is correct
16 Correct 2 ms 3248 KB Output is correct
17 Correct 2 ms 3420 KB Output is correct
18 Correct 2 ms 3420 KB Output is correct
19 Correct 2 ms 3420 KB Output is correct
20 Correct 2 ms 3420 KB Output is correct
21 Correct 1 ms 2908 KB Output is correct
22 Correct 2 ms 3420 KB Output is correct
23 Correct 2 ms 3420 KB Output is correct
24 Correct 3 ms 3420 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
26 Correct 106 ms 3420 KB Output is correct
27 Correct 16 ms 3420 KB Output is correct
28 Correct 100 ms 3416 KB Output is correct
29 Correct 16 ms 3416 KB Output is correct
30 Correct 106 ms 3620 KB Output is correct
31 Correct 17 ms 3420 KB Output is correct
32 Correct 102 ms 3608 KB Output is correct
33 Correct 16 ms 3416 KB Output is correct
34 Correct 10 ms 3420 KB Output is correct
35 Correct 16 ms 3620 KB Output is correct
36 Correct 105 ms 3416 KB Output is correct
37 Correct 16 ms 3420 KB Output is correct
38 Correct 105 ms 3608 KB Output is correct
39 Correct 16 ms 3672 KB Output is correct
40 Correct 106 ms 3604 KB Output is correct
41 Correct 17 ms 3416 KB Output is correct
42 Correct 104 ms 3608 KB Output is correct
43 Correct 17 ms 3420 KB Output is correct
44 Correct 103 ms 3616 KB Output is correct
45 Correct 16 ms 3420 KB Output is correct
46 Correct 108 ms 3420 KB Output is correct
47 Correct 2 ms 3164 KB Output is correct
48 Correct 64 ms 3640 KB Output is correct
49 Correct 63 ms 3624 KB Output is correct
50 Correct 109 ms 3676 KB Output is correct
51 Correct 105 ms 3652 KB Output is correct
52 Correct 330 ms 4432 KB Output is correct
53 Correct 6 ms 3928 KB Output is correct
54 Correct 351 ms 4448 KB Output is correct
55 Correct 6 ms 3932 KB Output is correct
56 Correct 334 ms 4352 KB Output is correct
57 Correct 6 ms 3932 KB Output is correct
58 Correct 357 ms 4600 KB Output is correct
59 Correct 6 ms 3932 KB Output is correct
60 Correct 379 ms 4500 KB Output is correct
61 Correct 7 ms 3932 KB Output is correct
62 Correct 327 ms 4432 KB Output is correct
63 Correct 6 ms 3928 KB Output is correct
64 Correct 358 ms 4656 KB Output is correct
65 Correct 334 ms 4432 KB Output is correct
66 Correct 6 ms 3928 KB Output is correct
67 Correct 350 ms 4476 KB Output is correct
68 Correct 6 ms 3928 KB Output is correct
69 Correct 353 ms 4436 KB Output is correct
70 Correct 6 ms 3928 KB Output is correct
71 Correct 340 ms 4432 KB Output is correct
72 Correct 6 ms 3932 KB Output is correct
73 Correct 215 ms 4504 KB Output is correct
74 Correct 1514 ms 7404 KB Output is correct
75 Correct 78 ms 7256 KB Output is correct
76 Correct 1510 ms 7484 KB Output is correct
77 Correct 76 ms 7260 KB Output is correct
78 Correct 1494 ms 7380 KB Output is correct
79 Correct 1515 ms 7380 KB Output is correct
80 Correct 81 ms 7388 KB Output is correct
81 Correct 77 ms 7360 KB Output is correct
82 Correct 1544 ms 7400 KB Output is correct
83 Correct 1473 ms 7364 KB Output is correct
84 Correct 80 ms 7372 KB Output is correct
85 Correct 1571 ms 7504 KB Output is correct
86 Correct 76 ms 7248 KB Output is correct
87 Correct 1543 ms 7376 KB Output is correct
88 Correct 82 ms 7380 KB Output is correct
89 Correct 1532 ms 7596 KB Output is correct
90 Correct 78 ms 7248 KB Output is correct
91 Correct 1561 ms 7344 KB Output is correct
92 Correct 81 ms 7256 KB Output is correct
93 Correct 1524 ms 7300 KB Output is correct
94 Correct 18 ms 6404 KB Output is correct
95 Correct 969 ms 7920 KB Output is correct
96 Correct 952 ms 7952 KB Output is correct
97 Correct 1533 ms 7340 KB Output is correct
98 Correct 1527 ms 7296 KB Output is correct