Submission #762444

# Submission time Handle Problem Language Result Execution time Memory
762444 2023-06-21T12:00:50 Z MilosMilutinovic Tug of War (BOI15_tug) C++14
100 / 100
1144 ms 18292 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 4 * N;
int n, k, l[N], r[N], s[N], up[N], cur;
vector<int> who[N], cyc;
vector<pair<int, int>> g[N];
set<int> pos, idx;
bool was[N], seen[N], on_cyc[N], gg[N];
void dfs(int x) {
    if (pos.find(x) != pos.end()) {
        return;
    }
    pos.insert(x);
    for (int i : who[x]) {
        idx.insert(i);
        dfs(l[i] ^ (n + r[i]) ^ x);
    }
}
void dfs_cyc(int x, int fa) {
    if (!cyc.empty()) {
        return;
    }
    seen[x] = true;
    for (auto& p : g[x]) {
        if (fa == p.second) {
            continue;
        }
        int y = p.first;
        if (seen[y]) {
            int ver = x;
            while (ver != y) {
                cyc.push_back(ver);
                ver = up[ver];
            }
            cyc.push_back(y);
            return;
        }
        up[y] = x;
        dfs_cyc(y, p.second);
        if (!cyc.empty()) {
            return;
        }
    }
}
void Go(int x, int p) {
    if (gg[x]) return;
    gg[x] = true;
    cur += (p <= n ? s[x] : -s[x]);
    for (int i : who[p]) {
        Go(i, l[i] ^ (r[i] + n) ^ p);
    }
}
int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= 2 * n; i++) {
        scanf("%d%d%d", &l[i], &r[i], &s[i]);
        who[l[i]].push_back(i);
        who[n + r[i]].push_back(i);
    }
    vector<pair<int, int>> vec;
    for (int i = 1; i <= 2 * n; i++) {
        if (was[i]) {
            continue;
        }
        pos.clear();
        idx.clear();
        dfs(l[i]);
        if (pos.size() != idx.size()) {
            printf("NO\n");
            exit(0);
        }
        for (int i : idx) {
            g[l[i]].emplace_back(n + r[i], i);
            g[n + r[i]].emplace_back(l[i], i);
        }
        cyc.clear();
        dfs_cyc(l[i], -1);
        for (int i : cyc)
            on_cyc[i] = true;
        int f = -1;
        for (int i : idx) {
            if (on_cyc[l[i]] && on_cyc[n + r[i]])
                f = i;
            was[i] = true;
        }
        cur = 0;
        for (int i : idx)
            gg[i] = false;
        Go(f, l[f]);
        int fir = cur;
        cur = 0;
        for (int i : idx)
            gg[i] = false;
        Go(f, n + r[f]);
        int sec = cur;
        vec.emplace_back(fir, sec);
        //printf("%d je taj\n", f);
    }
    bitset<M> bs;
    bs[2 * N] = 1;
    for (auto& p : vec) {
        bs = (p.first >= 0 ? (bs >> p.first) : (bs << (-p.first))) | (p.second >= 0 ? (bs >> p.second) : (bs << (-p.second)));
    }
    bool ok = false;
    for (int i = 0; i <= k; i++)
        if (bs[2 * N - i] || bs[2 * N + i])
            ok = true;
    if (ok)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

/*
4 1
1 1 1
2 1 2
2 2 8
1 2 2
3 3 5
3 3 2
4 4 1
4 4 2

YES

2 5
1 1 1
1 2 4
2 2 1
2 1 4

NO
*/

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     scanf("%d%d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~
tug.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         scanf("%d%d%d", &l[i], &r[i], &s[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10068 KB Output is correct
2 Correct 5 ms 10068 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 6 ms 10068 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 6 ms 10100 KB Output is correct
7 Correct 6 ms 10096 KB Output is correct
8 Correct 5 ms 10100 KB Output is correct
9 Correct 6 ms 10052 KB Output is correct
10 Correct 5 ms 10068 KB Output is correct
11 Correct 5 ms 10068 KB Output is correct
12 Correct 5 ms 10068 KB Output is correct
13 Correct 5 ms 10096 KB Output is correct
14 Correct 5 ms 10100 KB Output is correct
15 Correct 5 ms 10068 KB Output is correct
16 Correct 5 ms 10068 KB Output is correct
17 Correct 6 ms 10100 KB Output is correct
18 Correct 5 ms 10128 KB Output is correct
19 Correct 5 ms 10068 KB Output is correct
20 Correct 5 ms 10068 KB Output is correct
21 Correct 4 ms 10068 KB Output is correct
22 Correct 5 ms 10108 KB Output is correct
23 Correct 5 ms 10068 KB Output is correct
24 Correct 5 ms 10068 KB Output is correct
25 Correct 5 ms 10068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10068 KB Output is correct
2 Correct 5 ms 10068 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 6 ms 10068 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 6 ms 10100 KB Output is correct
7 Correct 6 ms 10096 KB Output is correct
8 Correct 5 ms 10100 KB Output is correct
9 Correct 6 ms 10052 KB Output is correct
10 Correct 5 ms 10068 KB Output is correct
11 Correct 5 ms 10068 KB Output is correct
12 Correct 5 ms 10068 KB Output is correct
13 Correct 5 ms 10096 KB Output is correct
14 Correct 5 ms 10100 KB Output is correct
15 Correct 5 ms 10068 KB Output is correct
16 Correct 5 ms 10068 KB Output is correct
17 Correct 6 ms 10100 KB Output is correct
18 Correct 5 ms 10128 KB Output is correct
19 Correct 5 ms 10068 KB Output is correct
20 Correct 5 ms 10068 KB Output is correct
21 Correct 4 ms 10068 KB Output is correct
22 Correct 5 ms 10108 KB Output is correct
23 Correct 5 ms 10068 KB Output is correct
24 Correct 5 ms 10068 KB Output is correct
25 Correct 5 ms 10068 KB Output is correct
26 Correct 79 ms 10500 KB Output is correct
27 Correct 17 ms 10444 KB Output is correct
28 Correct 74 ms 10496 KB Output is correct
29 Correct 17 ms 10480 KB Output is correct
30 Correct 78 ms 10496 KB Output is correct
31 Correct 17 ms 10496 KB Output is correct
32 Correct 74 ms 10496 KB Output is correct
33 Correct 18 ms 10452 KB Output is correct
34 Correct 13 ms 10448 KB Output is correct
35 Correct 17 ms 10484 KB Output is correct
36 Correct 79 ms 10492 KB Output is correct
37 Correct 19 ms 10512 KB Output is correct
38 Correct 83 ms 10496 KB Output is correct
39 Correct 17 ms 10452 KB Output is correct
40 Correct 76 ms 10500 KB Output is correct
41 Correct 17 ms 10452 KB Output is correct
42 Correct 77 ms 10508 KB Output is correct
43 Correct 17 ms 10452 KB Output is correct
44 Correct 74 ms 10492 KB Output is correct
45 Correct 18 ms 10508 KB Output is correct
46 Correct 80 ms 10572 KB Output is correct
47 Correct 9 ms 10580 KB Output is correct
48 Correct 43 ms 10516 KB Output is correct
49 Correct 42 ms 10516 KB Output is correct
50 Correct 76 ms 10500 KB Output is correct
51 Correct 81 ms 10500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 195 ms 12044 KB Output is correct
2 Correct 11 ms 11092 KB Output is correct
3 Correct 190 ms 12036 KB Output is correct
4 Correct 14 ms 11148 KB Output is correct
5 Correct 192 ms 12040 KB Output is correct
6 Correct 14 ms 11136 KB Output is correct
7 Correct 194 ms 12044 KB Output is correct
8 Correct 11 ms 11112 KB Output is correct
9 Correct 198 ms 12036 KB Output is correct
10 Correct 12 ms 11140 KB Output is correct
11 Correct 202 ms 12036 KB Output is correct
12 Correct 13 ms 11092 KB Output is correct
13 Correct 195 ms 12044 KB Output is correct
14 Correct 200 ms 12036 KB Output is correct
15 Correct 10 ms 11092 KB Output is correct
16 Correct 201 ms 12040 KB Output is correct
17 Correct 11 ms 11092 KB Output is correct
18 Correct 224 ms 12036 KB Output is correct
19 Correct 10 ms 11144 KB Output is correct
20 Correct 225 ms 11980 KB Output is correct
21 Correct 19 ms 12756 KB Output is correct
22 Correct 161 ms 12072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10068 KB Output is correct
2 Correct 5 ms 10068 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 6 ms 10068 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 6 ms 10100 KB Output is correct
7 Correct 6 ms 10096 KB Output is correct
8 Correct 5 ms 10100 KB Output is correct
9 Correct 6 ms 10052 KB Output is correct
10 Correct 5 ms 10068 KB Output is correct
11 Correct 5 ms 10068 KB Output is correct
12 Correct 5 ms 10068 KB Output is correct
13 Correct 5 ms 10096 KB Output is correct
14 Correct 5 ms 10100 KB Output is correct
15 Correct 5 ms 10068 KB Output is correct
16 Correct 5 ms 10068 KB Output is correct
17 Correct 6 ms 10100 KB Output is correct
18 Correct 5 ms 10128 KB Output is correct
19 Correct 5 ms 10068 KB Output is correct
20 Correct 5 ms 10068 KB Output is correct
21 Correct 4 ms 10068 KB Output is correct
22 Correct 5 ms 10108 KB Output is correct
23 Correct 5 ms 10068 KB Output is correct
24 Correct 5 ms 10068 KB Output is correct
25 Correct 5 ms 10068 KB Output is correct
26 Correct 79 ms 10500 KB Output is correct
27 Correct 17 ms 10444 KB Output is correct
28 Correct 74 ms 10496 KB Output is correct
29 Correct 17 ms 10480 KB Output is correct
30 Correct 78 ms 10496 KB Output is correct
31 Correct 17 ms 10496 KB Output is correct
32 Correct 74 ms 10496 KB Output is correct
33 Correct 18 ms 10452 KB Output is correct
34 Correct 13 ms 10448 KB Output is correct
35 Correct 17 ms 10484 KB Output is correct
36 Correct 79 ms 10492 KB Output is correct
37 Correct 19 ms 10512 KB Output is correct
38 Correct 83 ms 10496 KB Output is correct
39 Correct 17 ms 10452 KB Output is correct
40 Correct 76 ms 10500 KB Output is correct
41 Correct 17 ms 10452 KB Output is correct
42 Correct 77 ms 10508 KB Output is correct
43 Correct 17 ms 10452 KB Output is correct
44 Correct 74 ms 10492 KB Output is correct
45 Correct 18 ms 10508 KB Output is correct
46 Correct 80 ms 10572 KB Output is correct
47 Correct 9 ms 10580 KB Output is correct
48 Correct 43 ms 10516 KB Output is correct
49 Correct 42 ms 10516 KB Output is correct
50 Correct 76 ms 10500 KB Output is correct
51 Correct 81 ms 10500 KB Output is correct
52 Correct 195 ms 12044 KB Output is correct
53 Correct 11 ms 11092 KB Output is correct
54 Correct 190 ms 12036 KB Output is correct
55 Correct 14 ms 11148 KB Output is correct
56 Correct 192 ms 12040 KB Output is correct
57 Correct 14 ms 11136 KB Output is correct
58 Correct 194 ms 12044 KB Output is correct
59 Correct 11 ms 11112 KB Output is correct
60 Correct 198 ms 12036 KB Output is correct
61 Correct 12 ms 11140 KB Output is correct
62 Correct 202 ms 12036 KB Output is correct
63 Correct 13 ms 11092 KB Output is correct
64 Correct 195 ms 12044 KB Output is correct
65 Correct 200 ms 12036 KB Output is correct
66 Correct 10 ms 11092 KB Output is correct
67 Correct 201 ms 12040 KB Output is correct
68 Correct 11 ms 11092 KB Output is correct
69 Correct 224 ms 12036 KB Output is correct
70 Correct 10 ms 11144 KB Output is correct
71 Correct 225 ms 11980 KB Output is correct
72 Correct 19 ms 12756 KB Output is correct
73 Correct 161 ms 12072 KB Output is correct
74 Correct 1139 ms 16000 KB Output is correct
75 Correct 97 ms 15832 KB Output is correct
76 Correct 1093 ms 15988 KB Output is correct
77 Correct 94 ms 15852 KB Output is correct
78 Correct 1104 ms 15988 KB Output is correct
79 Correct 1080 ms 15988 KB Output is correct
80 Correct 91 ms 15820 KB Output is correct
81 Correct 100 ms 15844 KB Output is correct
82 Correct 1086 ms 15988 KB Output is correct
83 Correct 1144 ms 15988 KB Output is correct
84 Correct 92 ms 15780 KB Output is correct
85 Correct 1094 ms 16084 KB Output is correct
86 Correct 110 ms 15900 KB Output is correct
87 Correct 1093 ms 15992 KB Output is correct
88 Correct 99 ms 15812 KB Output is correct
89 Correct 1073 ms 15992 KB Output is correct
90 Correct 91 ms 15832 KB Output is correct
91 Correct 1061 ms 15992 KB Output is correct
92 Correct 93 ms 15856 KB Output is correct
93 Correct 1061 ms 16040 KB Output is correct
94 Correct 59 ms 18292 KB Output is correct
95 Correct 571 ms 16280 KB Output is correct
96 Correct 648 ms 16204 KB Output is correct
97 Correct 1135 ms 15988 KB Output is correct
98 Correct 1101 ms 15984 KB Output is correct