This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MAXN 600005
#define FOR(i, a, b) for(int 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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |