Submission #403194

# Submission time Handle Problem Language Result Execution time Memory
403194 2021-05-12T22:28:35 Z gevacrt Tug of War (BOI15_tug) C++17
100 / 100
1659 ms 29756 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

#define INF INT_MAX
#define MOD 1000000007
#define all(x) x.begin(), x.end()

int N, K;
vector<unordered_set<int>> adj; vi S;

int TOT = 0, CNT = 0;
void remove_singular(int x){
    auto u = *adj[x].begin(); adj[x].clear();
    adj[u].erase(x);

    if(x < 3*N) TOT += S[u], CNT++;
    else CNT--;

    auto v = *adj[u].begin(); adj[u].clear();
    adj[v].erase(u);

    if(adj[v].size() == 1) remove_singular(v);    
}

vi vis;
void dfs(int u, int c, int h[]){
    h[c] += S[u]; vis[u] = 1;

    for(auto v : adj[u]){
        if(vis[v]) continue;
        dfs(v, (c+1)%4, h);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N >> K;

    adj.resize(4*N); S.resize(4*N);
    for(int x = 0; x < 2*N; x++){
        int l, r; cin >> l >> r >> S[x];
        l--, r--; r += N;

        adj[x].insert(2*N+l);
        adj[2*N+l].insert(x);
    
        adj[x].insert(2*N+r);
        adj[2*N+r].insert(x);
    }

    // find single nodes
    for(int x = 2*N; x < 4*N; x++){
        if(adj[x].size() == 1)
            remove_singular(x);
    }

    for(int x = 2*N; x < 4*N; x++){
        if(adj[x].size() != 2 and adj[x].size() != 0){
            cout << "NO" << endl; return 0;
        }
    }
    assert(CNT == 0);

    bitset<600010> KP;
    KP[TOT] = 1;

    vis.resize(4*N);
    for(int x = 0; x < 2*N; x++){
        if(adj[x].empty() or vis[x]) continue;
        int h[4] = {}; dfs(x, 0, h);

        KP = (KP<<h[0]) | (KP<<h[2]);
    }
    
    int SUN = accumulate(all(S), 0);
    for(int i = 0; i < 600010; i++){
        if(KP[i]&1 and abs(SUN-2*i) <= K){
            cout << "YES\n";
            return 0;
        }
    }

    cout << "NO\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
26 Correct 104 ms 2548 KB Output is correct
27 Correct 24 ms 2508 KB Output is correct
28 Correct 105 ms 2508 KB Output is correct
29 Correct 20 ms 2572 KB Output is correct
30 Correct 101 ms 2556 KB Output is correct
31 Correct 20 ms 2568 KB Output is correct
32 Correct 100 ms 2616 KB Output is correct
33 Correct 20 ms 2568 KB Output is correct
34 Correct 13 ms 2508 KB Output is correct
35 Correct 25 ms 2504 KB Output is correct
36 Correct 99 ms 2508 KB Output is correct
37 Correct 20 ms 2564 KB Output is correct
38 Correct 101 ms 2540 KB Output is correct
39 Correct 22 ms 2568 KB Output is correct
40 Correct 118 ms 2548 KB Output is correct
41 Correct 20 ms 2508 KB Output is correct
42 Correct 101 ms 2544 KB Output is correct
43 Correct 20 ms 2564 KB Output is correct
44 Correct 120 ms 2508 KB Output is correct
45 Correct 20 ms 2564 KB Output is correct
46 Correct 111 ms 2588 KB Output is correct
47 Correct 6 ms 2392 KB Output is correct
48 Correct 53 ms 2508 KB Output is correct
49 Correct 54 ms 2508 KB Output is correct
50 Correct 99 ms 2548 KB Output is correct
51 Correct 100 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 503 ms 10184 KB Output is correct
2 Correct 32 ms 9668 KB Output is correct
3 Correct 514 ms 10060 KB Output is correct
4 Correct 33 ms 9680 KB Output is correct
5 Correct 519 ms 10072 KB Output is correct
6 Correct 35 ms 9676 KB Output is correct
7 Correct 507 ms 10176 KB Output is correct
8 Correct 33 ms 9716 KB Output is correct
9 Correct 508 ms 10068 KB Output is correct
10 Correct 33 ms 9660 KB Output is correct
11 Correct 503 ms 10072 KB Output is correct
12 Correct 33 ms 9668 KB Output is correct
13 Correct 495 ms 10068 KB Output is correct
14 Correct 521 ms 10072 KB Output is correct
15 Correct 32 ms 9668 KB Output is correct
16 Correct 508 ms 10132 KB Output is correct
17 Correct 32 ms 9676 KB Output is correct
18 Correct 502 ms 10012 KB Output is correct
19 Correct 33 ms 9668 KB Output is correct
20 Correct 520 ms 10100 KB Output is correct
21 Correct 35 ms 9540 KB Output is correct
22 Correct 300 ms 10068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 2 ms 588 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 2 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 3 ms 588 KB Output is correct
26 Correct 104 ms 2548 KB Output is correct
27 Correct 24 ms 2508 KB Output is correct
28 Correct 105 ms 2508 KB Output is correct
29 Correct 20 ms 2572 KB Output is correct
30 Correct 101 ms 2556 KB Output is correct
31 Correct 20 ms 2568 KB Output is correct
32 Correct 100 ms 2616 KB Output is correct
33 Correct 20 ms 2568 KB Output is correct
34 Correct 13 ms 2508 KB Output is correct
35 Correct 25 ms 2504 KB Output is correct
36 Correct 99 ms 2508 KB Output is correct
37 Correct 20 ms 2564 KB Output is correct
38 Correct 101 ms 2540 KB Output is correct
39 Correct 22 ms 2568 KB Output is correct
40 Correct 118 ms 2548 KB Output is correct
41 Correct 20 ms 2508 KB Output is correct
42 Correct 101 ms 2544 KB Output is correct
43 Correct 20 ms 2564 KB Output is correct
44 Correct 120 ms 2508 KB Output is correct
45 Correct 20 ms 2564 KB Output is correct
46 Correct 111 ms 2588 KB Output is correct
47 Correct 6 ms 2392 KB Output is correct
48 Correct 53 ms 2508 KB Output is correct
49 Correct 54 ms 2508 KB Output is correct
50 Correct 99 ms 2548 KB Output is correct
51 Correct 100 ms 2552 KB Output is correct
52 Correct 503 ms 10184 KB Output is correct
53 Correct 32 ms 9668 KB Output is correct
54 Correct 514 ms 10060 KB Output is correct
55 Correct 33 ms 9680 KB Output is correct
56 Correct 519 ms 10072 KB Output is correct
57 Correct 35 ms 9676 KB Output is correct
58 Correct 507 ms 10176 KB Output is correct
59 Correct 33 ms 9716 KB Output is correct
60 Correct 508 ms 10068 KB Output is correct
61 Correct 33 ms 9660 KB Output is correct
62 Correct 503 ms 10072 KB Output is correct
63 Correct 33 ms 9668 KB Output is correct
64 Correct 495 ms 10068 KB Output is correct
65 Correct 521 ms 10072 KB Output is correct
66 Correct 32 ms 9668 KB Output is correct
67 Correct 508 ms 10132 KB Output is correct
68 Correct 32 ms 9676 KB Output is correct
69 Correct 502 ms 10012 KB Output is correct
70 Correct 33 ms 9668 KB Output is correct
71 Correct 520 ms 10100 KB Output is correct
72 Correct 35 ms 9540 KB Output is correct
73 Correct 300 ms 10068 KB Output is correct
74 Correct 1549 ms 28860 KB Output is correct
75 Correct 212 ms 28872 KB Output is correct
76 Correct 1609 ms 28856 KB Output is correct
77 Correct 186 ms 28816 KB Output is correct
78 Correct 1558 ms 28852 KB Output is correct
79 Correct 1622 ms 29668 KB Output is correct
80 Correct 198 ms 29576 KB Output is correct
81 Correct 212 ms 29540 KB Output is correct
82 Correct 1659 ms 29668 KB Output is correct
83 Correct 1658 ms 29672 KB Output is correct
84 Correct 193 ms 29664 KB Output is correct
85 Correct 1552 ms 29664 KB Output is correct
86 Correct 184 ms 29756 KB Output is correct
87 Correct 1568 ms 29664 KB Output is correct
88 Correct 188 ms 29716 KB Output is correct
89 Correct 1564 ms 29676 KB Output is correct
90 Correct 198 ms 29668 KB Output is correct
91 Correct 1571 ms 29664 KB Output is correct
92 Correct 210 ms 29572 KB Output is correct
93 Correct 1528 ms 29752 KB Output is correct
94 Correct 127 ms 28484 KB Output is correct
95 Correct 867 ms 29668 KB Output is correct
96 Correct 843 ms 29660 KB Output is correct
97 Correct 1589 ms 29668 KB Output is correct
98 Correct 1559 ms 29664 KB Output is correct