답안 #104618

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104618 2019-04-08T11:57:44 Z alexpetrescu Tug of War (BOI15_tug) C++14
71 / 100
3000 ms 5300 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);
        for (int i = 1; i <= add[0]; i++)
            add[i] /= 2;
        left = (left + 1) / 2;
        right = right / 2;
        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]);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1792 KB Output is correct
2 Correct 3 ms 1792 KB Output is correct
3 Correct 4 ms 1792 KB Output is correct
4 Correct 3 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 3 ms 1792 KB Output is correct
8 Correct 4 ms 1792 KB Output is correct
9 Correct 3 ms 1792 KB Output is correct
10 Correct 4 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 3 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 3 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 3 ms 1792 KB Output is correct
22 Correct 4 ms 1792 KB Output is correct
23 Correct 3 ms 1792 KB Output is correct
24 Correct 4 ms 1792 KB Output is correct
25 Correct 4 ms 1792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1792 KB Output is correct
2 Correct 3 ms 1792 KB Output is correct
3 Correct 4 ms 1792 KB Output is correct
4 Correct 3 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 3 ms 1792 KB Output is correct
8 Correct 4 ms 1792 KB Output is correct
9 Correct 3 ms 1792 KB Output is correct
10 Correct 4 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 3 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 3 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 3 ms 1792 KB Output is correct
22 Correct 4 ms 1792 KB Output is correct
23 Correct 3 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 21 ms 2004 KB Output is correct
27 Correct 10 ms 2048 KB Output is correct
28 Correct 26 ms 2016 KB Output is correct
29 Correct 14 ms 1920 KB Output is correct
30 Correct 27 ms 1920 KB Output is correct
31 Correct 10 ms 1920 KB Output is correct
32 Correct 20 ms 1920 KB Output is correct
33 Correct 10 ms 2048 KB Output is correct
34 Correct 9 ms 1920 KB Output is correct
35 Correct 11 ms 1920 KB Output is correct
36 Correct 22 ms 1920 KB Output is correct
37 Correct 11 ms 2048 KB Output is correct
38 Correct 25 ms 2048 KB Output is correct
39 Correct 11 ms 1920 KB Output is correct
40 Correct 24 ms 2040 KB Output is correct
41 Correct 11 ms 2048 KB Output is correct
42 Correct 21 ms 1920 KB Output is correct
43 Correct 11 ms 2048 KB Output is correct
44 Correct 22 ms 2040 KB Output is correct
45 Correct 12 ms 1920 KB Output is correct
46 Correct 22 ms 1920 KB Output is correct
47 Correct 6 ms 1920 KB Output is correct
48 Correct 6 ms 1920 KB Output is correct
49 Correct 6 ms 1920 KB Output is correct
50 Correct 51 ms 2040 KB Output is correct
51 Correct 62 ms 2040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2688 KB Output is correct
2 Correct 15 ms 2688 KB Output is correct
3 Correct 14 ms 2688 KB Output is correct
4 Correct 15 ms 2688 KB Output is correct
5 Correct 14 ms 2688 KB Output is correct
6 Correct 13 ms 2688 KB Output is correct
7 Correct 15 ms 2816 KB Output is correct
8 Correct 18 ms 2688 KB Output is correct
9 Correct 20 ms 2688 KB Output is correct
10 Correct 19 ms 2688 KB Output is correct
11 Correct 16 ms 2816 KB Output is correct
12 Correct 17 ms 2688 KB Output is correct
13 Correct 13 ms 2688 KB Output is correct
14 Correct 17 ms 2816 KB Output is correct
15 Correct 18 ms 2688 KB Output is correct
16 Correct 16 ms 2816 KB Output is correct
17 Correct 17 ms 2688 KB Output is correct
18 Correct 17 ms 2688 KB Output is correct
19 Correct 15 ms 2688 KB Output is correct
20 Correct 17 ms 2816 KB Output is correct
21 Correct 14 ms 2688 KB Output is correct
22 Correct 15 ms 2844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1792 KB Output is correct
2 Correct 3 ms 1792 KB Output is correct
3 Correct 4 ms 1792 KB Output is correct
4 Correct 3 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 3 ms 1792 KB Output is correct
8 Correct 4 ms 1792 KB Output is correct
9 Correct 3 ms 1792 KB Output is correct
10 Correct 4 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 3 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 3 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 3 ms 1792 KB Output is correct
22 Correct 4 ms 1792 KB Output is correct
23 Correct 3 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 21 ms 2004 KB Output is correct
27 Correct 10 ms 2048 KB Output is correct
28 Correct 26 ms 2016 KB Output is correct
29 Correct 14 ms 1920 KB Output is correct
30 Correct 27 ms 1920 KB Output is correct
31 Correct 10 ms 1920 KB Output is correct
32 Correct 20 ms 1920 KB Output is correct
33 Correct 10 ms 2048 KB Output is correct
34 Correct 9 ms 1920 KB Output is correct
35 Correct 11 ms 1920 KB Output is correct
36 Correct 22 ms 1920 KB Output is correct
37 Correct 11 ms 2048 KB Output is correct
38 Correct 25 ms 2048 KB Output is correct
39 Correct 11 ms 1920 KB Output is correct
40 Correct 24 ms 2040 KB Output is correct
41 Correct 11 ms 2048 KB Output is correct
42 Correct 21 ms 1920 KB Output is correct
43 Correct 11 ms 2048 KB Output is correct
44 Correct 22 ms 2040 KB Output is correct
45 Correct 12 ms 1920 KB Output is correct
46 Correct 22 ms 1920 KB Output is correct
47 Correct 6 ms 1920 KB Output is correct
48 Correct 6 ms 1920 KB Output is correct
49 Correct 6 ms 1920 KB Output is correct
50 Correct 51 ms 2040 KB Output is correct
51 Correct 62 ms 2040 KB Output is correct
52 Correct 15 ms 2688 KB Output is correct
53 Correct 15 ms 2688 KB Output is correct
54 Correct 14 ms 2688 KB Output is correct
55 Correct 15 ms 2688 KB Output is correct
56 Correct 14 ms 2688 KB Output is correct
57 Correct 13 ms 2688 KB Output is correct
58 Correct 15 ms 2816 KB Output is correct
59 Correct 18 ms 2688 KB Output is correct
60 Correct 20 ms 2688 KB Output is correct
61 Correct 19 ms 2688 KB Output is correct
62 Correct 16 ms 2816 KB Output is correct
63 Correct 17 ms 2688 KB Output is correct
64 Correct 13 ms 2688 KB Output is correct
65 Correct 17 ms 2816 KB Output is correct
66 Correct 18 ms 2688 KB Output is correct
67 Correct 16 ms 2816 KB Output is correct
68 Correct 17 ms 2688 KB Output is correct
69 Correct 17 ms 2688 KB Output is correct
70 Correct 15 ms 2688 KB Output is correct
71 Correct 17 ms 2816 KB Output is correct
72 Correct 14 ms 2688 KB Output is correct
73 Correct 15 ms 2844 KB Output is correct
74 Execution timed out 3087 ms 5300 KB Time limit exceeded
75 Halted 0 ms 0 KB -