답안 #404479

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404479 2021-05-14T13:19:04 Z fvogel499 Tug of War (BOI15_tug) C++14
71 / 100
157 ms 11048 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 = 60001;

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 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 10 ms 972 KB Output is correct
27 Correct 6 ms 980 KB Output is correct
28 Correct 9 ms 972 KB Output is correct
29 Correct 6 ms 980 KB Output is correct
30 Correct 9 ms 972 KB Output is correct
31 Correct 5 ms 1004 KB Output is correct
32 Correct 10 ms 972 KB Output is correct
33 Correct 6 ms 992 KB Output is correct
34 Correct 5 ms 972 KB Output is correct
35 Correct 6 ms 972 KB Output is correct
36 Correct 9 ms 1012 KB Output is correct
37 Correct 5 ms 972 KB Output is correct
38 Correct 9 ms 972 KB Output is correct
39 Correct 5 ms 972 KB Output is correct
40 Correct 11 ms 1020 KB Output is correct
41 Correct 6 ms 972 KB Output is correct
42 Correct 9 ms 972 KB Output is correct
43 Correct 6 ms 972 KB Output is correct
44 Correct 11 ms 972 KB Output is correct
45 Correct 6 ms 972 KB Output is correct
46 Correct 12 ms 1016 KB Output is correct
47 Correct 5 ms 972 KB Output is correct
48 Correct 8 ms 944 KB Output is correct
49 Correct 8 ms 1056 KB Output is correct
50 Correct 9 ms 972 KB Output is correct
51 Correct 9 ms 1064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 3752 KB Output is correct
2 Correct 23 ms 3712 KB Output is correct
3 Correct 35 ms 3908 KB Output is correct
4 Correct 26 ms 3920 KB Output is correct
5 Correct 36 ms 3940 KB Output is correct
6 Correct 23 ms 3780 KB Output is correct
7 Correct 34 ms 3992 KB Output is correct
8 Correct 24 ms 3844 KB Output is correct
9 Correct 35 ms 3900 KB Output is correct
10 Correct 23 ms 3856 KB Output is correct
11 Correct 37 ms 3936 KB Output is correct
12 Correct 23 ms 3864 KB Output is correct
13 Correct 37 ms 3920 KB Output is correct
14 Correct 35 ms 3988 KB Output is correct
15 Correct 24 ms 3900 KB Output is correct
16 Correct 35 ms 3908 KB Output is correct
17 Correct 28 ms 3852 KB Output is correct
18 Correct 36 ms 3940 KB Output is correct
19 Correct 25 ms 3856 KB Output is correct
20 Correct 36 ms 3972 KB Output is correct
21 Correct 23 ms 3852 KB Output is correct
22 Correct 29 ms 3892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 10 ms 972 KB Output is correct
27 Correct 6 ms 980 KB Output is correct
28 Correct 9 ms 972 KB Output is correct
29 Correct 6 ms 980 KB Output is correct
30 Correct 9 ms 972 KB Output is correct
31 Correct 5 ms 1004 KB Output is correct
32 Correct 10 ms 972 KB Output is correct
33 Correct 6 ms 992 KB Output is correct
34 Correct 5 ms 972 KB Output is correct
35 Correct 6 ms 972 KB Output is correct
36 Correct 9 ms 1012 KB Output is correct
37 Correct 5 ms 972 KB Output is correct
38 Correct 9 ms 972 KB Output is correct
39 Correct 5 ms 972 KB Output is correct
40 Correct 11 ms 1020 KB Output is correct
41 Correct 6 ms 972 KB Output is correct
42 Correct 9 ms 972 KB Output is correct
43 Correct 6 ms 972 KB Output is correct
44 Correct 11 ms 972 KB Output is correct
45 Correct 6 ms 972 KB Output is correct
46 Correct 12 ms 1016 KB Output is correct
47 Correct 5 ms 972 KB Output is correct
48 Correct 8 ms 944 KB Output is correct
49 Correct 8 ms 1056 KB Output is correct
50 Correct 9 ms 972 KB Output is correct
51 Correct 9 ms 1064 KB Output is correct
52 Correct 34 ms 3752 KB Output is correct
53 Correct 23 ms 3712 KB Output is correct
54 Correct 35 ms 3908 KB Output is correct
55 Correct 26 ms 3920 KB Output is correct
56 Correct 36 ms 3940 KB Output is correct
57 Correct 23 ms 3780 KB Output is correct
58 Correct 34 ms 3992 KB Output is correct
59 Correct 24 ms 3844 KB Output is correct
60 Correct 35 ms 3900 KB Output is correct
61 Correct 23 ms 3856 KB Output is correct
62 Correct 37 ms 3936 KB Output is correct
63 Correct 23 ms 3864 KB Output is correct
64 Correct 37 ms 3920 KB Output is correct
65 Correct 35 ms 3988 KB Output is correct
66 Correct 24 ms 3900 KB Output is correct
67 Correct 35 ms 3908 KB Output is correct
68 Correct 28 ms 3852 KB Output is correct
69 Correct 36 ms 3940 KB Output is correct
70 Correct 25 ms 3856 KB Output is correct
71 Correct 36 ms 3972 KB Output is correct
72 Correct 23 ms 3852 KB Output is correct
73 Correct 29 ms 3892 KB Output is correct
74 Correct 155 ms 11048 KB Output is correct
75 Correct 99 ms 10820 KB Output is correct
76 Correct 155 ms 10960 KB Output is correct
77 Correct 90 ms 10816 KB Output is correct
78 Incorrect 157 ms 10940 KB Output isn't correct
79 Halted 0 ms 0 KB -