Submission #104617

# Submission time Handle Problem Language Result Execution time Memory
104617 2019-04-08T11:55:16 Z alexpetrescu Tug of War (BOI15_tug) C++14
71 / 100
3000 ms 5396 KB
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstdlib>
#include <algorithm>

//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout

#define MAXN 60009
#define MAXS 1200009

bool d[MAXS], cuplat[MAXN], done[MAXN];
int gr[MAXN], D, q[MAXN], add[MAXN];
int n, l[MAXN], r[MAXN], s[MAXN];
std::vector < int > g[MAXN];

inline void go(int x) {
    while (x) {
        int a = l[x];
        if (cuplat[a])
            a = r[x];
        if (cuplat[a])
            exit(1);
        cuplat[a] = 1;
        done[x] = 1;
        if (a <= n)
            D -= s[x];
        else
            D += s[x];
        x = 0;
        for (auto &y : g[a])
            if (done[y] == 0)
                x = y;
    }
}

int main() {
    int k;
    fscanf(fin, "%d%d", &n, &k);

    for (int i = 1; i <= 2 * n; i++) {
        fscanf(fin, "%d%d%d", &l[i], &r[i], &s[i]);
        r[i] += n;
        g[l[i]].push_back(i);
        g[r[i]].push_back(i);
    }

    for (int i = 1; i <= 2 * n; i++)
        gr[i] = g[i].size();

    int st = 0, dr = 0;
    for (int i = 1; i <= 2 * n; i++)
        if (gr[i] <= 1)
            q[dr++] = i;

    int dif = 0;
    while (st < dr) {
        int x = q[st++];
        if (gr[x] == 0) {
            fprintf(fout, "NO");
            return 0;
        }
        cuplat[x] = 1;
        int nod = 0;
        for (auto &y : g[x])
            if (done[y] == 0)
                nod = y;
        if (nod == 0)
            exit(2);
        done[nod] = 1;
        int z = l[nod] ^ r[nod] ^ x;
        gr[z]--;
        if (gr[z] == 1)
            q[dr++] = z;
        if (x <= n)
            dif -= s[nod];
        else
            dif += s[nod];
    }

    for (int i = 1; i <= 2 * n; i++) {
        if (done[i] == 0) {
            D = 0;
            go(i);
            if (D < 0)
                D = -D;
            dif -= D;
            add[++add[0]] = 2 * D;
        }
    }

    int left = -k - dif, right = k - dif;
    if (right < 0)
        fprintf(fout, "NO");
    else if (left <= 0)
        fprintf(fout, "YES");
    else {
        std::sort(add + 1, add + add[0] + 1);
        int lim = 0;
        d[0] = 1;
        for (int i = 1; i <= add[0]; i++) {
            lim = std::max(right, lim + add[i]);
            for (int j = add[i]; j <= lim; j++)
                d[j] |= d[j - add[i]];
        }
        bool ans = 0;
        for (int i = left; i <= right; i++)
            ans |= d[i];
        if (ans)
            fprintf(fout, "YES");
        else
            fprintf(fout, "NO");
    }

    fclose(fin);
    fclose(fout);
    return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:41:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d%d", &n, &k);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~
tug.cpp:44:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fin, "%d%d%d", &l[i], &r[i], &s[i]);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1792 KB Output is correct
2 Correct 4 ms 1792 KB Output is correct
3 Correct 3 ms 1764 KB Output is correct
4 Correct 4 ms 1792 KB Output is correct
5 Correct 3 ms 1792 KB Output is correct
6 Correct 4 ms 1792 KB Output is correct
7 Correct 4 ms 1920 KB Output is correct
8 Correct 3 ms 1792 KB Output is correct
9 Correct 3 ms 1792 KB Output is correct
10 Correct 1 ms 1792 KB Output is correct
11 Correct 3 ms 1792 KB Output is correct
12 Correct 3 ms 1792 KB Output is correct
13 Correct 3 ms 1792 KB Output is correct
14 Correct 4 ms 1792 KB Output is correct
15 Correct 3 ms 1792 KB Output is correct
16 Correct 4 ms 1792 KB Output is correct
17 Correct 4 ms 1792 KB Output is correct
18 Correct 3 ms 1792 KB Output is correct
19 Correct 4 ms 1792 KB Output is correct
20 Correct 4 ms 1792 KB Output is correct
21 Correct 4 ms 1792 KB Output is correct
22 Correct 4 ms 1792 KB Output is correct
23 Correct 4 ms 1792 KB Output is correct
24 Correct 4 ms 1792 KB Output is correct
25 Correct 4 ms 1792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1792 KB Output is correct
2 Correct 4 ms 1792 KB Output is correct
3 Correct 3 ms 1764 KB Output is correct
4 Correct 4 ms 1792 KB Output is correct
5 Correct 3 ms 1792 KB Output is correct
6 Correct 4 ms 1792 KB Output is correct
7 Correct 4 ms 1920 KB Output is correct
8 Correct 3 ms 1792 KB Output is correct
9 Correct 3 ms 1792 KB Output is correct
10 Correct 1 ms 1792 KB Output is correct
11 Correct 3 ms 1792 KB Output is correct
12 Correct 3 ms 1792 KB Output is correct
13 Correct 3 ms 1792 KB Output is correct
14 Correct 4 ms 1792 KB Output is correct
15 Correct 3 ms 1792 KB Output is correct
16 Correct 4 ms 1792 KB Output is correct
17 Correct 4 ms 1792 KB Output is correct
18 Correct 3 ms 1792 KB Output is correct
19 Correct 4 ms 1792 KB Output is correct
20 Correct 4 ms 1792 KB Output is correct
21 Correct 4 ms 1792 KB Output is correct
22 Correct 4 ms 1792 KB Output is correct
23 Correct 4 ms 1792 KB Output is correct
24 Correct 4 ms 1792 KB Output is correct
25 Correct 4 ms 1792 KB Output is correct
26 Correct 36 ms 2036 KB Output is correct
27 Correct 17 ms 2048 KB Output is correct
28 Correct 46 ms 1920 KB Output is correct
29 Correct 16 ms 2048 KB Output is correct
30 Correct 31 ms 1920 KB Output is correct
31 Correct 22 ms 2048 KB Output is correct
32 Correct 33 ms 2048 KB Output is correct
33 Correct 18 ms 2048 KB Output is correct
34 Correct 12 ms 2048 KB Output is correct
35 Correct 16 ms 2048 KB Output is correct
36 Correct 33 ms 1920 KB Output is correct
37 Correct 16 ms 2048 KB Output is correct
38 Correct 37 ms 2044 KB Output is correct
39 Correct 17 ms 2048 KB Output is correct
40 Correct 29 ms 1920 KB Output is correct
41 Correct 14 ms 2048 KB Output is correct
42 Correct 29 ms 1920 KB Output is correct
43 Correct 19 ms 2048 KB Output is correct
44 Correct 40 ms 2048 KB Output is correct
45 Correct 13 ms 2048 KB Output is correct
46 Correct 36 ms 1920 KB Output is correct
47 Correct 5 ms 1920 KB Output is correct
48 Correct 7 ms 1920 KB Output is correct
49 Correct 5 ms 1920 KB Output is correct
50 Correct 132 ms 2168 KB Output is correct
51 Correct 91 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2816 KB Output is correct
2 Correct 14 ms 2688 KB Output is correct
3 Correct 20 ms 2680 KB Output is correct
4 Correct 21 ms 2684 KB Output is correct
5 Correct 18 ms 2816 KB Output is correct
6 Correct 17 ms 2688 KB Output is correct
7 Correct 18 ms 2728 KB Output is correct
8 Correct 15 ms 2688 KB Output is correct
9 Correct 16 ms 2688 KB Output is correct
10 Correct 14 ms 2688 KB Output is correct
11 Correct 15 ms 2688 KB Output is correct
12 Correct 13 ms 2688 KB Output is correct
13 Correct 15 ms 2688 KB Output is correct
14 Correct 14 ms 2816 KB Output is correct
15 Correct 21 ms 2688 KB Output is correct
16 Correct 15 ms 2688 KB Output is correct
17 Correct 17 ms 2688 KB Output is correct
18 Correct 16 ms 2816 KB Output is correct
19 Correct 14 ms 2816 KB Output is correct
20 Correct 13 ms 2816 KB Output is correct
21 Correct 22 ms 2816 KB Output is correct
22 Correct 17 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1792 KB Output is correct
2 Correct 4 ms 1792 KB Output is correct
3 Correct 3 ms 1764 KB Output is correct
4 Correct 4 ms 1792 KB Output is correct
5 Correct 3 ms 1792 KB Output is correct
6 Correct 4 ms 1792 KB Output is correct
7 Correct 4 ms 1920 KB Output is correct
8 Correct 3 ms 1792 KB Output is correct
9 Correct 3 ms 1792 KB Output is correct
10 Correct 1 ms 1792 KB Output is correct
11 Correct 3 ms 1792 KB Output is correct
12 Correct 3 ms 1792 KB Output is correct
13 Correct 3 ms 1792 KB Output is correct
14 Correct 4 ms 1792 KB Output is correct
15 Correct 3 ms 1792 KB Output is correct
16 Correct 4 ms 1792 KB Output is correct
17 Correct 4 ms 1792 KB Output is correct
18 Correct 3 ms 1792 KB Output is correct
19 Correct 4 ms 1792 KB Output is correct
20 Correct 4 ms 1792 KB Output is correct
21 Correct 4 ms 1792 KB Output is correct
22 Correct 4 ms 1792 KB Output is correct
23 Correct 4 ms 1792 KB Output is correct
24 Correct 4 ms 1792 KB Output is correct
25 Correct 4 ms 1792 KB Output is correct
26 Correct 36 ms 2036 KB Output is correct
27 Correct 17 ms 2048 KB Output is correct
28 Correct 46 ms 1920 KB Output is correct
29 Correct 16 ms 2048 KB Output is correct
30 Correct 31 ms 1920 KB Output is correct
31 Correct 22 ms 2048 KB Output is correct
32 Correct 33 ms 2048 KB Output is correct
33 Correct 18 ms 2048 KB Output is correct
34 Correct 12 ms 2048 KB Output is correct
35 Correct 16 ms 2048 KB Output is correct
36 Correct 33 ms 1920 KB Output is correct
37 Correct 16 ms 2048 KB Output is correct
38 Correct 37 ms 2044 KB Output is correct
39 Correct 17 ms 2048 KB Output is correct
40 Correct 29 ms 1920 KB Output is correct
41 Correct 14 ms 2048 KB Output is correct
42 Correct 29 ms 1920 KB Output is correct
43 Correct 19 ms 2048 KB Output is correct
44 Correct 40 ms 2048 KB Output is correct
45 Correct 13 ms 2048 KB Output is correct
46 Correct 36 ms 1920 KB Output is correct
47 Correct 5 ms 1920 KB Output is correct
48 Correct 7 ms 1920 KB Output is correct
49 Correct 5 ms 1920 KB Output is correct
50 Correct 132 ms 2168 KB Output is correct
51 Correct 91 ms 2048 KB Output is correct
52 Correct 14 ms 2816 KB Output is correct
53 Correct 14 ms 2688 KB Output is correct
54 Correct 20 ms 2680 KB Output is correct
55 Correct 21 ms 2684 KB Output is correct
56 Correct 18 ms 2816 KB Output is correct
57 Correct 17 ms 2688 KB Output is correct
58 Correct 18 ms 2728 KB Output is correct
59 Correct 15 ms 2688 KB Output is correct
60 Correct 16 ms 2688 KB Output is correct
61 Correct 14 ms 2688 KB Output is correct
62 Correct 15 ms 2688 KB Output is correct
63 Correct 13 ms 2688 KB Output is correct
64 Correct 15 ms 2688 KB Output is correct
65 Correct 14 ms 2816 KB Output is correct
66 Correct 21 ms 2688 KB Output is correct
67 Correct 15 ms 2688 KB Output is correct
68 Correct 17 ms 2688 KB Output is correct
69 Correct 16 ms 2816 KB Output is correct
70 Correct 14 ms 2816 KB Output is correct
71 Correct 13 ms 2816 KB Output is correct
72 Correct 22 ms 2816 KB Output is correct
73 Correct 17 ms 2816 KB Output is correct
74 Execution timed out 3093 ms 5396 KB Time limit exceeded
75 Halted 0 ms 0 KB -