답안 #559388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559388 2022-05-09T17:21:14 Z Olympia Tug of War (BOI15_tug) C++17
41 / 100
70 ms 5348 KB
#include <bits/stdc++.h>
using namespace std;
vector<multiset<pair<int,int> > > adj;
vector<bool> hasVisited;
int ans = 0;
void dfs (int curNode, int prevNode) {
    if (hasVisited[curNode]) {
        return;
    }
    hasVisited[curNode] = true;
    for (pair<int,int> p: adj[curNode]) {
        if (p.first != prevNode) {
            ans += p.second;
            dfs (p.first, curNode);
        }
    }
}
int c = 0;
void apply () {
    queue<int> q;
    for (int i = 0; i < adj.size(); i++) {
        if (adj[i].size() == 1) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        pair<int,int> p = *adj[x].begin();
        c += p.second;
        adj[x].clear();
        assert(adj[p.first].count(make_pair(x, -p.second)));
        adj[p.first].erase(make_pair(x, -p.second));
        if (adj[p.first].size() == 1) {
            q.push(p.first);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    adj.resize(2 * n);
    hasVisited.assign(2 * n, false);
    map<pair<int,int>,vector<int> > myMap;
    for (int i = 0; i < 2 * n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        v += n;
        myMap[make_pair(u, v)].push_back(w);
        adj[u].insert(make_pair(v, w));
        adj[v].insert(make_pair(u, -w));
    }
    for (int i = 0; i < 2 * n; i++) {
        if (adj[i].empty()) {
            cout << "NO\n";
            exit(0);
        }
    }
    apply();
    vector<int> tot;
    for (int i = 0; i < n; i++) {
        if (!hasVisited[i] && !adj[i].empty()) {
            ans = 0;
            dfs (i, (*adj[i].rbegin()).first);
            tot.push_back(abs(ans));
        }
    }
    for (auto& p: myMap) {
        if (p.second.size() <= 1) {
            continue;
        }
        int a = 0;
        for (int j = 0; j < p.second.size(); j++) {
            if (j % 2) a += p.second[j];
            else a -= p.second[j];
        }
        tot.push_back(abs(a));
    }
    bitset<60000> bs;
    bs.set(c + 30000);
    for (int j: tot) {
        bs = (bs >> j) | (bs << j);
    }
    for (int i = 0; i <= k; i++) {
        if (bs[30000 + i]) {
            cout << "YES\n";
            return 0;
        }
    }
    cout << "NO\n";
}

Compilation message

tug.cpp: In function 'void apply()':
tug.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::multiset<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for (int i = 0; i < adj.size(); i++) {
      |                     ~~^~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for (int j = 0; j < p.second.size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 15 ms 1132 KB Output is correct
27 Correct 5 ms 1384 KB Output is correct
28 Correct 19 ms 1132 KB Output is correct
29 Correct 6 ms 1364 KB Output is correct
30 Correct 13 ms 1108 KB Output is correct
31 Correct 5 ms 1364 KB Output is correct
32 Correct 13 ms 1108 KB Output is correct
33 Correct 5 ms 1300 KB Output is correct
34 Correct 4 ms 1388 KB Output is correct
35 Correct 5 ms 1364 KB Output is correct
36 Correct 13 ms 1108 KB Output is correct
37 Correct 5 ms 1364 KB Output is correct
38 Correct 14 ms 1124 KB Output is correct
39 Correct 5 ms 1364 KB Output is correct
40 Correct 16 ms 1128 KB Output is correct
41 Correct 5 ms 1364 KB Output is correct
42 Correct 14 ms 1136 KB Output is correct
43 Correct 5 ms 1388 KB Output is correct
44 Correct 14 ms 1108 KB Output is correct
45 Correct 4 ms 1364 KB Output is correct
46 Correct 12 ms 1108 KB Output is correct
47 Correct 3 ms 1260 KB Output is correct
48 Incorrect 10 ms 1236 KB Output isn't correct
49 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 4464 KB Output is correct
2 Correct 18 ms 5256 KB Output is correct
3 Correct 70 ms 4480 KB Output is correct
4 Correct 19 ms 5324 KB Output is correct
5 Correct 57 ms 4468 KB Output is correct
6 Correct 14 ms 5272 KB Output is correct
7 Correct 49 ms 4436 KB Output is correct
8 Correct 18 ms 5316 KB Output is correct
9 Correct 50 ms 4448 KB Output is correct
10 Correct 14 ms 5332 KB Output is correct
11 Correct 49 ms 4384 KB Output is correct
12 Correct 14 ms 5332 KB Output is correct
13 Correct 49 ms 4436 KB Output is correct
14 Correct 49 ms 4356 KB Output is correct
15 Correct 15 ms 5332 KB Output is correct
16 Correct 55 ms 4388 KB Output is correct
17 Correct 21 ms 5332 KB Output is correct
18 Correct 54 ms 4476 KB Output is correct
19 Correct 20 ms 5348 KB Output is correct
20 Correct 64 ms 4436 KB Output is correct
21 Correct 15 ms 5076 KB Output is correct
22 Correct 34 ms 4820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 15 ms 1132 KB Output is correct
27 Correct 5 ms 1384 KB Output is correct
28 Correct 19 ms 1132 KB Output is correct
29 Correct 6 ms 1364 KB Output is correct
30 Correct 13 ms 1108 KB Output is correct
31 Correct 5 ms 1364 KB Output is correct
32 Correct 13 ms 1108 KB Output is correct
33 Correct 5 ms 1300 KB Output is correct
34 Correct 4 ms 1388 KB Output is correct
35 Correct 5 ms 1364 KB Output is correct
36 Correct 13 ms 1108 KB Output is correct
37 Correct 5 ms 1364 KB Output is correct
38 Correct 14 ms 1124 KB Output is correct
39 Correct 5 ms 1364 KB Output is correct
40 Correct 16 ms 1128 KB Output is correct
41 Correct 5 ms 1364 KB Output is correct
42 Correct 14 ms 1136 KB Output is correct
43 Correct 5 ms 1388 KB Output is correct
44 Correct 14 ms 1108 KB Output is correct
45 Correct 4 ms 1364 KB Output is correct
46 Correct 12 ms 1108 KB Output is correct
47 Correct 3 ms 1260 KB Output is correct
48 Incorrect 10 ms 1236 KB Output isn't correct
49 Halted 0 ms 0 KB -