Submission #62802

# Submission time Handle Problem Language Result Execution time Memory
62802 2018-07-30T05:49:43 Z Benq Tug of War (BOI15_tug) C++11
100 / 100
1519 ms 14428 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 60001;

int n, k, l[MX],r[MX],s[MX];
bitset<570001> b;
vi adj[MX];

int pre[MX], depth[MX];
bool visit[MX], cyc[MX];
pi oops;
int yes[2], no[2], mn, sum;
vi v;
vpi V;

void dfs(int x) {
    visit[x] = 1; v.pb(x);
    for (auto a: adj[x]) if (a != pre[x]) {
        int t = l[a]+r[a]+n-x;
        if (visit[t]) {
            if (depth[t] < depth[x]) oops = {x,a};
        } else {
            pre[t] = a; depth[t] = depth[x]+1;
            dfs(t);
        }
    }
}

int getSum(int x) { return l[x]+r[x]+n; }

void genCyc() {
    V = {oops};
    int fin = getSum(oops.s)-oops.f;
    do {
        int nex = getSum(pre[oops.f])-oops.f;
        V.pb({nex,pre[oops.f]});
        oops.f = nex;
    } while (oops.f != fin);
}

void dfs2(int a, int pre) {
    for (int i: adj[a]) if (i != pre) {
        int t = l[i]+r[i]+n-a;
        if (!cyc[t]) {
            yes[t <= n] += s[i];
            dfs2(t,i);
        }
    }
}

void solve(int x) {
    v.clear(), V.clear();
    F0R(i,2) yes[i] = no[i] = 0;
    dfs(x);
    int numEdge = 0;
    for (int i: v) numEdge += sz(adj[i]);
    if (numEdge != 2*sz(v)) {
        cout << "NO";
        exit(0);
    }
    genCyc();
    for (auto i: V) cyc[i.f] = 1;
    for (auto i: V) dfs2(i.f,0);
    F0R(i,sz(V)) no[i&1] += s[V[i].s];
    mn += min(yes[0]+no[0],yes[0]+no[1]);
    b |= b<<(max(yes[0]+no[0],yes[0]+no[1])-min(yes[0]+no[0],yes[0]+no[1]));
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k;
    b[0] = 1;
    FOR(i,1,2*n+1) {
        cin >> l[i] >> r[i] >> s[i];
        adj[l[i]].pb(i), adj[r[i]+n].pb(i);
        sum += s[i];
    }
    FOR(i,1,2*n+1) if (!visit[i]) solve(i);
    F0R(i,570001) if (b[i] == 1 && abs(2*i+2*mn-sum) <= k) {
        cout << "YES";
        exit(0);
    }
    cout << "NO";
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2040 KB Output is correct
2 Correct 4 ms 2156 KB Output is correct
3 Correct 6 ms 2156 KB Output is correct
4 Correct 5 ms 2156 KB Output is correct
5 Correct 5 ms 2156 KB Output is correct
6 Correct 6 ms 2156 KB Output is correct
7 Correct 5 ms 2160 KB Output is correct
8 Correct 5 ms 2288 KB Output is correct
9 Correct 5 ms 2288 KB Output is correct
10 Correct 6 ms 2288 KB Output is correct
11 Correct 5 ms 2288 KB Output is correct
12 Correct 5 ms 2288 KB Output is correct
13 Correct 4 ms 2288 KB Output is correct
14 Correct 5 ms 2288 KB Output is correct
15 Correct 4 ms 2288 KB Output is correct
16 Correct 5 ms 2288 KB Output is correct
17 Correct 4 ms 2288 KB Output is correct
18 Correct 5 ms 2288 KB Output is correct
19 Correct 4 ms 2288 KB Output is correct
20 Correct 5 ms 2288 KB Output is correct
21 Correct 5 ms 2288 KB Output is correct
22 Correct 5 ms 2288 KB Output is correct
23 Correct 6 ms 2288 KB Output is correct
24 Correct 5 ms 2288 KB Output is correct
25 Correct 5 ms 2288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2040 KB Output is correct
2 Correct 4 ms 2156 KB Output is correct
3 Correct 6 ms 2156 KB Output is correct
4 Correct 5 ms 2156 KB Output is correct
5 Correct 5 ms 2156 KB Output is correct
6 Correct 6 ms 2156 KB Output is correct
7 Correct 5 ms 2160 KB Output is correct
8 Correct 5 ms 2288 KB Output is correct
9 Correct 5 ms 2288 KB Output is correct
10 Correct 6 ms 2288 KB Output is correct
11 Correct 5 ms 2288 KB Output is correct
12 Correct 5 ms 2288 KB Output is correct
13 Correct 4 ms 2288 KB Output is correct
14 Correct 5 ms 2288 KB Output is correct
15 Correct 4 ms 2288 KB Output is correct
16 Correct 5 ms 2288 KB Output is correct
17 Correct 4 ms 2288 KB Output is correct
18 Correct 5 ms 2288 KB Output is correct
19 Correct 4 ms 2288 KB Output is correct
20 Correct 5 ms 2288 KB Output is correct
21 Correct 5 ms 2288 KB Output is correct
22 Correct 5 ms 2288 KB Output is correct
23 Correct 6 ms 2288 KB Output is correct
24 Correct 5 ms 2288 KB Output is correct
25 Correct 5 ms 2288 KB Output is correct
26 Correct 76 ms 2468 KB Output is correct
27 Correct 18 ms 2468 KB Output is correct
28 Correct 109 ms 2540 KB Output is correct
29 Correct 27 ms 2540 KB Output is correct
30 Correct 76 ms 2540 KB Output is correct
31 Correct 24 ms 2540 KB Output is correct
32 Correct 114 ms 2540 KB Output is correct
33 Correct 20 ms 2540 KB Output is correct
34 Correct 14 ms 2540 KB Output is correct
35 Correct 18 ms 2540 KB Output is correct
36 Correct 77 ms 2540 KB Output is correct
37 Correct 22 ms 2540 KB Output is correct
38 Correct 85 ms 2540 KB Output is correct
39 Correct 17 ms 2540 KB Output is correct
40 Correct 77 ms 2540 KB Output is correct
41 Correct 17 ms 2540 KB Output is correct
42 Correct 90 ms 2540 KB Output is correct
43 Correct 19 ms 2540 KB Output is correct
44 Correct 84 ms 2540 KB Output is correct
45 Correct 24 ms 2540 KB Output is correct
46 Correct 78 ms 2540 KB Output is correct
47 Correct 8 ms 2540 KB Output is correct
48 Correct 43 ms 2540 KB Output is correct
49 Correct 45 ms 2540 KB Output is correct
50 Correct 95 ms 2540 KB Output is correct
51 Correct 118 ms 2540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 253 ms 3308 KB Output is correct
2 Correct 24 ms 3308 KB Output is correct
3 Correct 218 ms 3308 KB Output is correct
4 Correct 18 ms 3308 KB Output is correct
5 Correct 225 ms 3308 KB Output is correct
6 Correct 15 ms 3308 KB Output is correct
7 Correct 310 ms 3308 KB Output is correct
8 Correct 15 ms 3308 KB Output is correct
9 Correct 217 ms 3308 KB Output is correct
10 Correct 14 ms 3308 KB Output is correct
11 Correct 209 ms 3308 KB Output is correct
12 Correct 17 ms 3308 KB Output is correct
13 Correct 250 ms 3356 KB Output is correct
14 Correct 263 ms 3356 KB Output is correct
15 Correct 29 ms 3356 KB Output is correct
16 Correct 234 ms 3356 KB Output is correct
17 Correct 21 ms 3356 KB Output is correct
18 Correct 211 ms 3356 KB Output is correct
19 Correct 16 ms 3356 KB Output is correct
20 Correct 205 ms 3356 KB Output is correct
21 Correct 19 ms 3356 KB Output is correct
22 Correct 116 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2040 KB Output is correct
2 Correct 4 ms 2156 KB Output is correct
3 Correct 6 ms 2156 KB Output is correct
4 Correct 5 ms 2156 KB Output is correct
5 Correct 5 ms 2156 KB Output is correct
6 Correct 6 ms 2156 KB Output is correct
7 Correct 5 ms 2160 KB Output is correct
8 Correct 5 ms 2288 KB Output is correct
9 Correct 5 ms 2288 KB Output is correct
10 Correct 6 ms 2288 KB Output is correct
11 Correct 5 ms 2288 KB Output is correct
12 Correct 5 ms 2288 KB Output is correct
13 Correct 4 ms 2288 KB Output is correct
14 Correct 5 ms 2288 KB Output is correct
15 Correct 4 ms 2288 KB Output is correct
16 Correct 5 ms 2288 KB Output is correct
17 Correct 4 ms 2288 KB Output is correct
18 Correct 5 ms 2288 KB Output is correct
19 Correct 4 ms 2288 KB Output is correct
20 Correct 5 ms 2288 KB Output is correct
21 Correct 5 ms 2288 KB Output is correct
22 Correct 5 ms 2288 KB Output is correct
23 Correct 6 ms 2288 KB Output is correct
24 Correct 5 ms 2288 KB Output is correct
25 Correct 5 ms 2288 KB Output is correct
26 Correct 76 ms 2468 KB Output is correct
27 Correct 18 ms 2468 KB Output is correct
28 Correct 109 ms 2540 KB Output is correct
29 Correct 27 ms 2540 KB Output is correct
30 Correct 76 ms 2540 KB Output is correct
31 Correct 24 ms 2540 KB Output is correct
32 Correct 114 ms 2540 KB Output is correct
33 Correct 20 ms 2540 KB Output is correct
34 Correct 14 ms 2540 KB Output is correct
35 Correct 18 ms 2540 KB Output is correct
36 Correct 77 ms 2540 KB Output is correct
37 Correct 22 ms 2540 KB Output is correct
38 Correct 85 ms 2540 KB Output is correct
39 Correct 17 ms 2540 KB Output is correct
40 Correct 77 ms 2540 KB Output is correct
41 Correct 17 ms 2540 KB Output is correct
42 Correct 90 ms 2540 KB Output is correct
43 Correct 19 ms 2540 KB Output is correct
44 Correct 84 ms 2540 KB Output is correct
45 Correct 24 ms 2540 KB Output is correct
46 Correct 78 ms 2540 KB Output is correct
47 Correct 8 ms 2540 KB Output is correct
48 Correct 43 ms 2540 KB Output is correct
49 Correct 45 ms 2540 KB Output is correct
50 Correct 95 ms 2540 KB Output is correct
51 Correct 118 ms 2540 KB Output is correct
52 Correct 253 ms 3308 KB Output is correct
53 Correct 24 ms 3308 KB Output is correct
54 Correct 218 ms 3308 KB Output is correct
55 Correct 18 ms 3308 KB Output is correct
56 Correct 225 ms 3308 KB Output is correct
57 Correct 15 ms 3308 KB Output is correct
58 Correct 310 ms 3308 KB Output is correct
59 Correct 15 ms 3308 KB Output is correct
60 Correct 217 ms 3308 KB Output is correct
61 Correct 14 ms 3308 KB Output is correct
62 Correct 209 ms 3308 KB Output is correct
63 Correct 17 ms 3308 KB Output is correct
64 Correct 250 ms 3356 KB Output is correct
65 Correct 263 ms 3356 KB Output is correct
66 Correct 29 ms 3356 KB Output is correct
67 Correct 234 ms 3356 KB Output is correct
68 Correct 21 ms 3356 KB Output is correct
69 Correct 211 ms 3356 KB Output is correct
70 Correct 16 ms 3356 KB Output is correct
71 Correct 205 ms 3356 KB Output is correct
72 Correct 19 ms 3356 KB Output is correct
73 Correct 116 ms 3448 KB Output is correct
74 Correct 1122 ms 5396 KB Output is correct
75 Correct 124 ms 5484 KB Output is correct
76 Correct 1154 ms 5528 KB Output is correct
77 Correct 109 ms 5528 KB Output is correct
78 Correct 1393 ms 5528 KB Output is correct
79 Correct 1169 ms 5528 KB Output is correct
80 Correct 176 ms 5528 KB Output is correct
81 Correct 128 ms 5528 KB Output is correct
82 Correct 1392 ms 5528 KB Output is correct
83 Correct 1351 ms 5528 KB Output is correct
84 Correct 126 ms 5528 KB Output is correct
85 Correct 1136 ms 5528 KB Output is correct
86 Correct 114 ms 5528 KB Output is correct
87 Correct 1162 ms 5528 KB Output is correct
88 Correct 155 ms 6252 KB Output is correct
89 Correct 1378 ms 7120 KB Output is correct
90 Correct 159 ms 7848 KB Output is correct
91 Correct 1220 ms 8668 KB Output is correct
92 Correct 124 ms 9576 KB Output is correct
93 Correct 1304 ms 10412 KB Output is correct
94 Correct 80 ms 10412 KB Output is correct
95 Correct 709 ms 12056 KB Output is correct
96 Correct 718 ms 12840 KB Output is correct
97 Correct 1519 ms 13532 KB Output is correct
98 Correct 1290 ms 14428 KB Output is correct