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>
#define ll long long
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 6e4 + 1, M = 10 * N;
const int mod = 1e9 + 7;
int n, k, deg[N], vis[N], w[N];
vector< pair<int, int> > g[N];
bitset<M> dp;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
int sum = 0;
for(int i = 1, x, y;i <= n + n;i++) {
cin >> x >> y >> w[i], y += n;
g[x].push_back(MP(y, i)), deg[x]++;
g[y].push_back(MP(x, i)), deg[y]++;
}
{
set< pair<int, int> > st;
for(int i = 1;i <= n + n;i++)
st.insert(MP(deg[i], i));
while(!st.empty()) {
int pos = st.begin()->Y;
st.erase(st.begin());
if(deg[pos] == 0) return cout << "NO", 0;
else if(deg[pos] != 1) break;
for(auto [to, id]: g[pos]) {
if(vis[id]) continue;
vis[id] = 1;
st.erase(MP(deg[to], to));
if(pos > n) sum -= w[id];
else sum += w[id];
deg[to]--, deg[pos]--;
st.insert(MP(deg[to], to));
break;
}
}
}
dp[0] = 1;
for(int i = 1;i <= n + n;i++) {
int bl = 0;
function<void(int)> dfs;
dfs = [&](int v){
int cnt = 0;
for(auto [to, id]: g[v]) {
if(vis[id]) continue;
vis[id] = 1;
if(v <= n) bl += w[id];
else bl -= w[id];
dfs(to);
cnt++;
}
assert(cnt <= 1);
};
dfs(i);
if(bl == 0) continue;
bl = abs(bl), sum += bl, dp |= dp << bl;
}
string ans = "NO";
for(int i = 0;i < M;i++) {
if(dp[i] && abs(sum - 2 * i) <= k) {
ans = "YES";
}
}
cout << ans;
return 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... |