답안 #337721

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337721 2020-12-21T12:41:05 Z HynDuf Tug of War (BOI15_tug) C++11
23 / 100
247 ms 8172 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define eb emplace_back
#define pb push_back
#define pf push_front
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define Rep(i, r, l) for (int i = (r); i >= (l); i--)
#define DB(X) {cerr << #X << " = " << X << '\n';}
#define DB1(A, _) {cerr << #A << "[" << _ << "] = " << A[_] << '\n';}
#define DB2(A, _, __) {cerr << #A << "[" << _ << "][" << __ << "] = " << A[_][__] << '\n';}
#define DB3(A, _, __, ____) {cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << A[_][__][___] << '\n';}
#define PR(A, l, r) {cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define left ___left
#define right ___right
#define next ___next
#define prev ___prev
#define y1 ___y1
#define y0 ___y0
#define j0 ___j0
#define SZ(x) ((int)(x).size())
using ll = long long;
using ld = long double;
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 6e4 + 2, NN = 600002;
int n, cnt[N], K;
struct Tug
{
    int l, r, s;
} tug[N];
set<int> tu[N];
vii g[N];
vi obj;
bool vis[N];
int cur;
void dfs(int u, int r)
{
    for (const ii &v : g[u]) if (!vis[v.S])
    {
        vis[v.S] = 1;
        if (u > n) cur -= tug[v.S].s;
        else cur += tug[v.S].s;
        if (v.F != r) dfs(v.F, r);
    }
}
bitset<NN> dp;
int main()
{
    //freopen("T.in", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> K;
    rep(i, 1, n << 1)
    {
        int l, r, s;
        cin >> l >> r >> s;
        r += n;
        tug[i] = {l, r, s};
        cnt[l]++, cnt[r]++;
        tu[l].insert(i);
        tu[r].insert(i);
    }
    queue<int> q;
    rep(i, 1, (n << 1)) if (cnt[i] == 0)
    {
        cout << "NO";
        return 0;
    } else if (cnt[i] == 1) q.push(i);
    int ans = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (cnt[u] == 0)
        {
            cout << "NO";
            return 0;
        }
        assert(cnt[u] == 1);
        assert(SZ(tu[u]) == 1);
        cnt[u] = 0;
        int id = *tu[u].begin();
        int ano = (tug[id].l == u ? tug[id].r : tug[id].l);
        if (--cnt[ano] == 1) q.push(ano);
        if (u > n) ans -= tug[id].s;
        else ans += tug[id].s;
        tu[ano].erase(id);
    }
    rep(u, 1, (n << 1)) if (cnt[u])
    {
        assert(cnt[u] == 2);
        assert(SZ(tu[u]) == 2);
        auto it = tu[u].begin();
        int ano = (tug[*it].l == u ? tug[*it].r : tug[*it].l);
        g[u].eb(ano, *it);
        it++;
        ano = (tug[*it].l == u ? tug[*it].r : tug[*it].l);
        g[u].eb(ano, *it);
    }
    rep(i, 1, (n << 1)) if (cnt[i] == 2 && !vis[i])
    {
        cur = 0;
        dfs(i, i);
        obj.eb(abs(cur));
        //DB(cur);
    }
    dp[0] = 1;
    for (int v : obj) dp = (dp << v) | (dp >> v);
    rep(i, 0, K) if (dp[i])
    {
        cout << "YES";
        return 0;
    }
    cout << "NO";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4844 KB Output is correct
2 Correct 4 ms 4844 KB Output is correct
3 Correct 4 ms 4844 KB Output is correct
4 Incorrect 4 ms 4844 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4844 KB Output is correct
2 Correct 4 ms 4844 KB Output is correct
3 Correct 4 ms 4844 KB Output is correct
4 Incorrect 4 ms 4844 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 239 ms 8096 KB Output is correct
2 Correct 15 ms 7020 KB Output is correct
3 Correct 242 ms 8096 KB Output is correct
4 Correct 14 ms 7020 KB Output is correct
5 Correct 241 ms 8172 KB Output is correct
6 Correct 15 ms 7020 KB Output is correct
7 Correct 238 ms 8044 KB Output is correct
8 Correct 14 ms 7020 KB Output is correct
9 Correct 247 ms 8172 KB Output is correct
10 Correct 15 ms 7020 KB Output is correct
11 Correct 245 ms 8172 KB Output is correct
12 Correct 15 ms 7020 KB Output is correct
13 Correct 241 ms 8044 KB Output is correct
14 Correct 239 ms 8044 KB Output is correct
15 Correct 15 ms 7020 KB Output is correct
16 Correct 239 ms 8172 KB Output is correct
17 Correct 14 ms 7020 KB Output is correct
18 Correct 240 ms 8108 KB Output is correct
19 Correct 14 ms 7020 KB Output is correct
20 Correct 246 ms 8044 KB Output is correct
21 Correct 15 ms 7020 KB Output is correct
22 Correct 179 ms 7532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4844 KB Output is correct
2 Correct 4 ms 4844 KB Output is correct
3 Correct 4 ms 4844 KB Output is correct
4 Incorrect 4 ms 4844 KB Output isn't correct
5 Halted 0 ms 0 KB -