답안 #1109109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109109 2024-11-06T03:54:06 Z codexistent Tug of War (BOI15_tug) C++14
23 / 100
24 ms 5456 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define MAXN 60005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)

multiset<pair<int, int>> e[MAXN];
bool v[MAXN];
int n, k, r = 0, r2 = 0;
bitset<MAXN> bs;

void dfs(int node){
    v[node] = true;
    if(e[node].size()) return;
    int nxt, cost;
    tie(nxt, cost) = *e[node].begin();

    r += cost;
    if(!v[nxt]){
        e[nxt].erase(e[nxt].find({node, -cost}));
        e[node].clear();
        dfs(nxt);
    }
}

int main(){
    cin >> n >> k;
    FOR(i, 1, 2 * n){
        int l, r, s;
        cin >> l >> r >> s;

        e[l].insert({n + r, s}), e[n + r].insert({l, -s});
    }

    queue<int> q;
    FOR(i, 1, 2 * n) {
        if(e[i].size() == 1) q.push(i);
        if(e[i].size() == 0) return cout << "NO" << endl, 0;
    }

    while(q.size()){
        int qi = q.front();
        q.pop();

        if(e[qi].size() == 0) return cout << "NO" << endl, 0;
        int nxt, cost;
        tie(nxt, cost) = *e[qi].begin();
        r += cost;

        e[qi].clear();
        e[nxt].erase(e[nxt].find({qi, -cost}));
        if(e[nxt].size() == 1) q.push(nxt);
    }

    vector<int> items;
    if(r) items.push_back(abs(r));
    FOR(i, 1, 2 * n){
        if(!v[i] && e[i].size()){
            r = 0;
            e[i].erase(e[i].begin());

            dfs(i);
            if(r) items.push_back(abs(r));
        }
    }
    r2 = accumulate(items.begin(), items.end(), 0);

    bs[0] = 1;
    for(int i : items) bs |= bs << i;
    FOR(i, 0, r2){
        if(bs[i] && abs(2 * i - r2) <= k) return cout << "YES" << endl, 0; 
    }

    return cout << "NO" << endl, 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 5456 KB Output is correct
2 Correct 13 ms 5200 KB Output is correct
3 Correct 18 ms 5220 KB Output is correct
4 Correct 13 ms 5200 KB Output is correct
5 Correct 13 ms 5336 KB Output is correct
6 Correct 14 ms 5364 KB Output is correct
7 Correct 13 ms 5200 KB Output is correct
8 Correct 14 ms 5364 KB Output is correct
9 Correct 21 ms 5348 KB Output is correct
10 Correct 15 ms 5200 KB Output is correct
11 Correct 19 ms 5336 KB Output is correct
12 Correct 12 ms 5372 KB Output is correct
13 Correct 13 ms 5360 KB Output is correct
14 Correct 13 ms 5200 KB Output is correct
15 Correct 13 ms 5200 KB Output is correct
16 Correct 13 ms 5200 KB Output is correct
17 Correct 13 ms 5200 KB Output is correct
18 Correct 13 ms 5200 KB Output is correct
19 Correct 14 ms 5200 KB Output is correct
20 Correct 14 ms 5200 KB Output is correct
21 Correct 14 ms 5200 KB Output is correct
22 Correct 24 ms 5192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 3152 KB Output isn't correct
2 Halted 0 ms 0 KB -