Submission #104619

# Submission time Handle Problem Language Result Execution time Memory
104619 2019-04-08T12:02:58 Z alexpetrescu Tug of War (BOI15_tug) C++14
100 / 100
2295 ms 5368 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 1170009

std::bitset < MAXS > d;
bool 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);
        for (int i = 1; i <= add[0]; i++)
            add[i] /= 2;
        left = (left + 1) / 2;
        right = right / 2;
        d[0] = 1;
        for (int i = 1; i <= add[0]; i++)
            d |= d << 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:42: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:45: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 4 ms 2176 KB Output is correct
2 Correct 5 ms 2176 KB Output is correct
3 Correct 4 ms 2176 KB Output is correct
4 Correct 5 ms 2176 KB Output is correct
5 Correct 6 ms 2176 KB Output is correct
6 Correct 4 ms 2176 KB Output is correct
7 Correct 5 ms 2176 KB Output is correct
8 Correct 5 ms 2176 KB Output is correct
9 Correct 4 ms 2176 KB Output is correct
10 Correct 5 ms 2176 KB Output is correct
11 Correct 5 ms 2176 KB Output is correct
12 Correct 5 ms 2304 KB Output is correct
13 Correct 5 ms 2176 KB Output is correct
14 Correct 7 ms 2304 KB Output is correct
15 Correct 7 ms 2176 KB Output is correct
16 Correct 5 ms 2176 KB Output is correct
17 Correct 4 ms 2176 KB Output is correct
18 Correct 6 ms 2176 KB Output is correct
19 Correct 4 ms 2176 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 2176 KB Output is correct
24 Correct 5 ms 2176 KB Output is correct
25 Correct 5 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2176 KB Output is correct
2 Correct 5 ms 2176 KB Output is correct
3 Correct 4 ms 2176 KB Output is correct
4 Correct 5 ms 2176 KB Output is correct
5 Correct 6 ms 2176 KB Output is correct
6 Correct 4 ms 2176 KB Output is correct
7 Correct 5 ms 2176 KB Output is correct
8 Correct 5 ms 2176 KB Output is correct
9 Correct 4 ms 2176 KB Output is correct
10 Correct 5 ms 2176 KB Output is correct
11 Correct 5 ms 2176 KB Output is correct
12 Correct 5 ms 2304 KB Output is correct
13 Correct 5 ms 2176 KB Output is correct
14 Correct 7 ms 2304 KB Output is correct
15 Correct 7 ms 2176 KB Output is correct
16 Correct 5 ms 2176 KB Output is correct
17 Correct 4 ms 2176 KB Output is correct
18 Correct 6 ms 2176 KB Output is correct
19 Correct 4 ms 2176 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 2176 KB Output is correct
24 Correct 5 ms 2176 KB Output is correct
25 Correct 5 ms 2176 KB Output is correct
26 Correct 147 ms 2424 KB Output is correct
27 Correct 27 ms 2304 KB Output is correct
28 Correct 138 ms 2304 KB Output is correct
29 Correct 32 ms 2304 KB Output is correct
30 Correct 175 ms 2424 KB Output is correct
31 Correct 30 ms 2432 KB Output is correct
32 Correct 154 ms 2440 KB Output is correct
33 Correct 28 ms 2424 KB Output is correct
34 Correct 17 ms 2424 KB Output is correct
35 Correct 23 ms 2304 KB Output is correct
36 Correct 155 ms 2408 KB Output is correct
37 Correct 29 ms 2432 KB Output is correct
38 Correct 136 ms 2524 KB Output is correct
39 Correct 24 ms 2432 KB Output is correct
40 Correct 146 ms 2432 KB Output is correct
41 Correct 27 ms 2432 KB Output is correct
42 Correct 141 ms 2304 KB Output is correct
43 Correct 27 ms 2432 KB Output is correct
44 Correct 158 ms 2432 KB Output is correct
45 Correct 24 ms 2432 KB Output is correct
46 Correct 142 ms 2444 KB Output is correct
47 Correct 6 ms 1920 KB Output is correct
48 Correct 5 ms 1920 KB Output is correct
49 Correct 6 ms 1920 KB Output is correct
50 Correct 134 ms 2424 KB Output is correct
51 Correct 145 ms 2408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2788 KB Output is correct
2 Correct 13 ms 2688 KB Output is correct
3 Correct 15 ms 2688 KB Output is correct
4 Correct 16 ms 2688 KB Output is correct
5 Correct 24 ms 2680 KB Output is correct
6 Correct 13 ms 2688 KB Output is correct
7 Correct 15 ms 2688 KB Output is correct
8 Correct 16 ms 2688 KB Output is correct
9 Correct 16 ms 2816 KB Output is correct
10 Correct 14 ms 2688 KB Output is correct
11 Correct 14 ms 2788 KB Output is correct
12 Correct 14 ms 2688 KB Output is correct
13 Correct 19 ms 2816 KB Output is correct
14 Correct 16 ms 2864 KB Output is correct
15 Correct 14 ms 2688 KB Output is correct
16 Correct 20 ms 2816 KB Output is correct
17 Correct 17 ms 2680 KB Output is correct
18 Correct 13 ms 2816 KB Output is correct
19 Correct 22 ms 2688 KB Output is correct
20 Correct 17 ms 2688 KB Output is correct
21 Correct 14 ms 2688 KB Output is correct
22 Correct 13 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2176 KB Output is correct
2 Correct 5 ms 2176 KB Output is correct
3 Correct 4 ms 2176 KB Output is correct
4 Correct 5 ms 2176 KB Output is correct
5 Correct 6 ms 2176 KB Output is correct
6 Correct 4 ms 2176 KB Output is correct
7 Correct 5 ms 2176 KB Output is correct
8 Correct 5 ms 2176 KB Output is correct
9 Correct 4 ms 2176 KB Output is correct
10 Correct 5 ms 2176 KB Output is correct
11 Correct 5 ms 2176 KB Output is correct
12 Correct 5 ms 2304 KB Output is correct
13 Correct 5 ms 2176 KB Output is correct
14 Correct 7 ms 2304 KB Output is correct
15 Correct 7 ms 2176 KB Output is correct
16 Correct 5 ms 2176 KB Output is correct
17 Correct 4 ms 2176 KB Output is correct
18 Correct 6 ms 2176 KB Output is correct
19 Correct 4 ms 2176 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 2176 KB Output is correct
24 Correct 5 ms 2176 KB Output is correct
25 Correct 5 ms 2176 KB Output is correct
26 Correct 147 ms 2424 KB Output is correct
27 Correct 27 ms 2304 KB Output is correct
28 Correct 138 ms 2304 KB Output is correct
29 Correct 32 ms 2304 KB Output is correct
30 Correct 175 ms 2424 KB Output is correct
31 Correct 30 ms 2432 KB Output is correct
32 Correct 154 ms 2440 KB Output is correct
33 Correct 28 ms 2424 KB Output is correct
34 Correct 17 ms 2424 KB Output is correct
35 Correct 23 ms 2304 KB Output is correct
36 Correct 155 ms 2408 KB Output is correct
37 Correct 29 ms 2432 KB Output is correct
38 Correct 136 ms 2524 KB Output is correct
39 Correct 24 ms 2432 KB Output is correct
40 Correct 146 ms 2432 KB Output is correct
41 Correct 27 ms 2432 KB Output is correct
42 Correct 141 ms 2304 KB Output is correct
43 Correct 27 ms 2432 KB Output is correct
44 Correct 158 ms 2432 KB Output is correct
45 Correct 24 ms 2432 KB Output is correct
46 Correct 142 ms 2444 KB Output is correct
47 Correct 6 ms 1920 KB Output is correct
48 Correct 5 ms 1920 KB Output is correct
49 Correct 6 ms 1920 KB Output is correct
50 Correct 134 ms 2424 KB Output is correct
51 Correct 145 ms 2408 KB Output is correct
52 Correct 22 ms 2788 KB Output is correct
53 Correct 13 ms 2688 KB Output is correct
54 Correct 15 ms 2688 KB Output is correct
55 Correct 16 ms 2688 KB Output is correct
56 Correct 24 ms 2680 KB Output is correct
57 Correct 13 ms 2688 KB Output is correct
58 Correct 15 ms 2688 KB Output is correct
59 Correct 16 ms 2688 KB Output is correct
60 Correct 16 ms 2816 KB Output is correct
61 Correct 14 ms 2688 KB Output is correct
62 Correct 14 ms 2788 KB Output is correct
63 Correct 14 ms 2688 KB Output is correct
64 Correct 19 ms 2816 KB Output is correct
65 Correct 16 ms 2864 KB Output is correct
66 Correct 14 ms 2688 KB Output is correct
67 Correct 20 ms 2816 KB Output is correct
68 Correct 17 ms 2680 KB Output is correct
69 Correct 13 ms 2816 KB Output is correct
70 Correct 22 ms 2688 KB Output is correct
71 Correct 17 ms 2688 KB Output is correct
72 Correct 14 ms 2688 KB Output is correct
73 Correct 13 ms 2816 KB Output is correct
74 Correct 2012 ms 5236 KB Output is correct
75 Correct 130 ms 5240 KB Output is correct
76 Correct 2105 ms 5260 KB Output is correct
77 Correct 116 ms 5112 KB Output is correct
78 Correct 2164 ms 5240 KB Output is correct
79 Correct 2040 ms 5112 KB Output is correct
80 Correct 137 ms 5120 KB Output is correct
81 Correct 141 ms 5152 KB Output is correct
82 Correct 2088 ms 5368 KB Output is correct
83 Correct 1940 ms 5268 KB Output is correct
84 Correct 157 ms 5112 KB Output is correct
85 Correct 2019 ms 5240 KB Output is correct
86 Correct 159 ms 5112 KB Output is correct
87 Correct 1872 ms 5368 KB Output is correct
88 Correct 183 ms 5112 KB Output is correct
89 Correct 2044 ms 5328 KB Output is correct
90 Correct 149 ms 5268 KB Output is correct
91 Correct 1978 ms 5212 KB Output is correct
92 Correct 143 ms 5124 KB Output is correct
93 Correct 2062 ms 5240 KB Output is correct
94 Correct 44 ms 4472 KB Output is correct
95 Correct 76 ms 4956 KB Output is correct
96 Correct 51 ms 4856 KB Output is correct
97 Correct 2113 ms 5368 KB Output is correct
98 Correct 2295 ms 5368 KB Output is correct