답안 #404477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404477 2021-05-14T13:12:58 Z fvogel499 Tug of War (BOI15_tug) C++14
71 / 100
3000 ms 12980 KB
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <cstring>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cassert>

using namespace std;

#define pii pair<int, int>
#define f first
#define s second

#define ll long long
#define rint int32_t

const int siz = 6000000;

bitset<siz> bit;

// ifstream cin("tug.in");
// ofstream cout("tug.out");

struct Edge {
    Edge(int lNA, int lNB, int lStrength) {
        nA = lNA;
        nB = lNB;
        strength = lStrength;
    }
    int nA, nB, strength;
};

rint main() {
    int n, tolerance;
    cin >> n >> tolerance;

    set<Edge*> graph [2*n];
    for (int i = 0; i < 2*n; i++) {
        int l, r, ls;
        cin >> l >> r >> ls;
        l--;
        r--;
        r += n;
        Edge* e = new Edge(l, r, ls);
        graph[l].insert(e);
        graph[r].insert(e);
    }

    int desequilibrium = 0;
    bool proc [2*n];
    for (int i = 0; i < 2*n; i++) proc[i] = false;
    queue<int> q;
    for (int i = 0; i < 2*n; i++) {
        if (graph[i].size() == 1) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int curNode = q.front();
        q.pop();
        if (proc[curNode] or graph[curNode].empty()) continue;
        proc[curNode] = true;
        if (curNode < n) {
            desequilibrium += (*(graph[curNode].begin()))->strength; 
        }
        else {
            desequilibrium -= (*(graph[curNode].begin()))->strength;
        }
        int newNode = (*(graph[curNode].begin()))->nA;
        if (newNode == curNode) newNode = (*(graph[curNode].begin()))->nB;
        graph[newNode].erase(graph[newNode].find(*(graph[curNode].begin())));
        graph[curNode].clear();
        if (graph[newNode].size() == 1) {
            q.push(newNode);
        }
    }
    desequilibrium = max(desequilibrium, -desequilibrium);

    for (int i = 0; i < 2*n; i++) {
        if (!proc[i] and graph[i].size() == 0) {
            cout << "NO";
            return 0;
        }
    }

    vector<int> desequilibriums;
    desequilibriums.push_back(desequilibrium);
    int desequilibriumsSum = desequilibrium;
    for (int i = 0; i < n; i++) {
        if (proc[i]) continue;
        int locDesiquilibrium = 0;
        int curNode = i;
        int parentNode = -1;
        int step = 1;
        while (!proc[curNode]) {
            proc[curNode] = true;
            int newNode = (*(graph[curNode].begin()))->nA;
            if (newNode == curNode) newNode = (*(graph[curNode].begin()))->nB;
            if (newNode == parentNode) {
                newNode = (*(next(graph[curNode].begin())))->nA;
                if (newNode == curNode) newNode = (*(next(graph[curNode].begin())))->nB;
                locDesiquilibrium += step*(*(next(graph[curNode].begin())))->strength;
            }
            else {
                locDesiquilibrium += step*(*(graph[curNode].begin()))->strength;
            }
            if (step == 1) step = -1;
            else step = 1;
            parentNode = curNode;
            curNode = newNode;
        }
        locDesiquilibrium = max(locDesiquilibrium, -locDesiquilibrium);
        desequilibriums.push_back(locDesiquilibrium);
        desequilibriumsSum += locDesiquilibrium;
    }

    bit = bitset<siz>(0);
    bit[0] = 1;
    for (int i : desequilibriums) {
        bit |= bit<<i;
    }

    for (int i = 0; i < siz; i++) if (bit[i] and abs(i-(desequilibriumsSum-i)) <= tolerance) {
        cout << "YES";
        return 0;
    }

    cout << "NO";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2380 KB Output is correct
2 Correct 5 ms 2380 KB Output is correct
3 Correct 10 ms 2496 KB Output is correct
4 Correct 5 ms 2380 KB Output is correct
5 Correct 10 ms 2404 KB Output is correct
6 Correct 11 ms 2380 KB Output is correct
7 Correct 5 ms 2380 KB Output is correct
8 Correct 5 ms 2380 KB Output is correct
9 Correct 5 ms 2380 KB Output is correct
10 Correct 10 ms 2492 KB Output is correct
11 Correct 5 ms 2380 KB Output is correct
12 Correct 5 ms 2380 KB Output is correct
13 Correct 5 ms 2380 KB Output is correct
14 Correct 5 ms 2380 KB Output is correct
15 Correct 5 ms 2496 KB Output is correct
16 Correct 10 ms 2496 KB Output is correct
17 Correct 5 ms 2380 KB Output is correct
18 Correct 5 ms 2380 KB Output is correct
19 Correct 5 ms 2380 KB Output is correct
20 Correct 4 ms 2380 KB Output is correct
21 Correct 2 ms 1740 KB Output is correct
22 Correct 4 ms 2380 KB Output is correct
23 Correct 10 ms 2488 KB Output is correct
24 Correct 5 ms 2380 KB Output is correct
25 Correct 11 ms 2492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2380 KB Output is correct
2 Correct 5 ms 2380 KB Output is correct
3 Correct 10 ms 2496 KB Output is correct
4 Correct 5 ms 2380 KB Output is correct
5 Correct 10 ms 2404 KB Output is correct
6 Correct 11 ms 2380 KB Output is correct
7 Correct 5 ms 2380 KB Output is correct
8 Correct 5 ms 2380 KB Output is correct
9 Correct 5 ms 2380 KB Output is correct
10 Correct 10 ms 2492 KB Output is correct
11 Correct 5 ms 2380 KB Output is correct
12 Correct 5 ms 2380 KB Output is correct
13 Correct 5 ms 2380 KB Output is correct
14 Correct 5 ms 2380 KB Output is correct
15 Correct 5 ms 2496 KB Output is correct
16 Correct 10 ms 2496 KB Output is correct
17 Correct 5 ms 2380 KB Output is correct
18 Correct 5 ms 2380 KB Output is correct
19 Correct 5 ms 2380 KB Output is correct
20 Correct 4 ms 2380 KB Output is correct
21 Correct 2 ms 1740 KB Output is correct
22 Correct 4 ms 2380 KB Output is correct
23 Correct 10 ms 2488 KB Output is correct
24 Correct 5 ms 2380 KB Output is correct
25 Correct 11 ms 2492 KB Output is correct
26 Correct 508 ms 3172 KB Output is correct
27 Correct 80 ms 3160 KB Output is correct
28 Correct 511 ms 3180 KB Output is correct
29 Correct 81 ms 3148 KB Output is correct
30 Correct 522 ms 3184 KB Output is correct
31 Correct 81 ms 3148 KB Output is correct
32 Correct 510 ms 3188 KB Output is correct
33 Correct 82 ms 3148 KB Output is correct
34 Correct 43 ms 3148 KB Output is correct
35 Correct 80 ms 3172 KB Output is correct
36 Correct 509 ms 3184 KB Output is correct
37 Correct 80 ms 3172 KB Output is correct
38 Correct 508 ms 3180 KB Output is correct
39 Correct 81 ms 3148 KB Output is correct
40 Correct 512 ms 3176 KB Output is correct
41 Correct 79 ms 3148 KB Output is correct
42 Correct 514 ms 3268 KB Output is correct
43 Correct 84 ms 3180 KB Output is correct
44 Correct 507 ms 3176 KB Output is correct
45 Correct 80 ms 3148 KB Output is correct
46 Correct 508 ms 3172 KB Output is correct
47 Correct 6 ms 2380 KB Output is correct
48 Correct 256 ms 3180 KB Output is correct
49 Correct 268 ms 3180 KB Output is correct
50 Correct 509 ms 3172 KB Output is correct
51 Correct 530 ms 3176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1667 ms 6036 KB Output is correct
2 Correct 24 ms 5188 KB Output is correct
3 Correct 1648 ms 6084 KB Output is correct
4 Correct 27 ms 5188 KB Output is correct
5 Correct 1680 ms 6032 KB Output is correct
6 Correct 29 ms 5196 KB Output is correct
7 Correct 1657 ms 6084 KB Output is correct
8 Correct 25 ms 5144 KB Output is correct
9 Correct 1682 ms 6040 KB Output is correct
10 Correct 25 ms 5216 KB Output is correct
11 Correct 1686 ms 6044 KB Output is correct
12 Correct 24 ms 5196 KB Output is correct
13 Correct 1657 ms 6040 KB Output is correct
14 Correct 1688 ms 6040 KB Output is correct
15 Correct 25 ms 5144 KB Output is correct
16 Correct 1646 ms 6036 KB Output is correct
17 Correct 25 ms 5156 KB Output is correct
18 Correct 1671 ms 6084 KB Output is correct
19 Correct 27 ms 5196 KB Output is correct
20 Correct 1659 ms 6068 KB Output is correct
21 Correct 25 ms 5196 KB Output is correct
22 Correct 847 ms 6008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2380 KB Output is correct
2 Correct 5 ms 2380 KB Output is correct
3 Correct 10 ms 2496 KB Output is correct
4 Correct 5 ms 2380 KB Output is correct
5 Correct 10 ms 2404 KB Output is correct
6 Correct 11 ms 2380 KB Output is correct
7 Correct 5 ms 2380 KB Output is correct
8 Correct 5 ms 2380 KB Output is correct
9 Correct 5 ms 2380 KB Output is correct
10 Correct 10 ms 2492 KB Output is correct
11 Correct 5 ms 2380 KB Output is correct
12 Correct 5 ms 2380 KB Output is correct
13 Correct 5 ms 2380 KB Output is correct
14 Correct 5 ms 2380 KB Output is correct
15 Correct 5 ms 2496 KB Output is correct
16 Correct 10 ms 2496 KB Output is correct
17 Correct 5 ms 2380 KB Output is correct
18 Correct 5 ms 2380 KB Output is correct
19 Correct 5 ms 2380 KB Output is correct
20 Correct 4 ms 2380 KB Output is correct
21 Correct 2 ms 1740 KB Output is correct
22 Correct 4 ms 2380 KB Output is correct
23 Correct 10 ms 2488 KB Output is correct
24 Correct 5 ms 2380 KB Output is correct
25 Correct 11 ms 2492 KB Output is correct
26 Correct 508 ms 3172 KB Output is correct
27 Correct 80 ms 3160 KB Output is correct
28 Correct 511 ms 3180 KB Output is correct
29 Correct 81 ms 3148 KB Output is correct
30 Correct 522 ms 3184 KB Output is correct
31 Correct 81 ms 3148 KB Output is correct
32 Correct 510 ms 3188 KB Output is correct
33 Correct 82 ms 3148 KB Output is correct
34 Correct 43 ms 3148 KB Output is correct
35 Correct 80 ms 3172 KB Output is correct
36 Correct 509 ms 3184 KB Output is correct
37 Correct 80 ms 3172 KB Output is correct
38 Correct 508 ms 3180 KB Output is correct
39 Correct 81 ms 3148 KB Output is correct
40 Correct 512 ms 3176 KB Output is correct
41 Correct 79 ms 3148 KB Output is correct
42 Correct 514 ms 3268 KB Output is correct
43 Correct 84 ms 3180 KB Output is correct
44 Correct 507 ms 3176 KB Output is correct
45 Correct 80 ms 3148 KB Output is correct
46 Correct 508 ms 3172 KB Output is correct
47 Correct 6 ms 2380 KB Output is correct
48 Correct 256 ms 3180 KB Output is correct
49 Correct 268 ms 3180 KB Output is correct
50 Correct 509 ms 3172 KB Output is correct
51 Correct 530 ms 3176 KB Output is correct
52 Correct 1667 ms 6036 KB Output is correct
53 Correct 24 ms 5188 KB Output is correct
54 Correct 1648 ms 6084 KB Output is correct
55 Correct 27 ms 5188 KB Output is correct
56 Correct 1680 ms 6032 KB Output is correct
57 Correct 29 ms 5196 KB Output is correct
58 Correct 1657 ms 6084 KB Output is correct
59 Correct 25 ms 5144 KB Output is correct
60 Correct 1682 ms 6040 KB Output is correct
61 Correct 25 ms 5216 KB Output is correct
62 Correct 1686 ms 6044 KB Output is correct
63 Correct 24 ms 5196 KB Output is correct
64 Correct 1657 ms 6040 KB Output is correct
65 Correct 1688 ms 6040 KB Output is correct
66 Correct 25 ms 5144 KB Output is correct
67 Correct 1646 ms 6036 KB Output is correct
68 Correct 25 ms 5156 KB Output is correct
69 Correct 1671 ms 6084 KB Output is correct
70 Correct 27 ms 5196 KB Output is correct
71 Correct 1659 ms 6068 KB Output is correct
72 Correct 25 ms 5196 KB Output is correct
73 Correct 847 ms 6008 KB Output is correct
74 Execution timed out 3071 ms 12980 KB Time limit exceeded
75 Halted 0 ms 0 KB -