This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update>
using ll = long long;
using ld = long double;
const ll MOD = 998244353;
const ll root = 62;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 30001;
//remove 0/1 spot stuff
//everything left has 2 possible people
//decompose into graph and do knapsack
vector<ttgl> adj[2*mxN];
int deg[mxN];
array<int, 3> edges[mxN];
bool flag[2*mxN];
bitset<20*2*mxN> dp;
set<ttgl> vis;
int n, k;
int dfs(int u) {
int res = 0;
for(auto v: adj[u]) {
if(flag[v.first])continue;
if(vis.count({v.first, u})) continue;
flag[v.first] = true;
vis.insert({u, v.first});
res+=dfs(v.first);
res+=(v.second * (u<n ? 1 : -1));
}
return res;
}
int main() {
//freopen("file.in", "r", stdin);
//freopen("file.out", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin.tie(0)->sync_with_stdio(0);
cin>>n>>k;
int a, b, c;
owo(i, 0, 2*n) {
cin>>a>>b>>c;
a--; b--;
b+=n;
adj[a].senpai({b, c});
adj[b].senpai({a, c});
deg[a]++; deg[b]++;
edges[i] = {a, b, c};
}
int mid = 20*n;
queue<int> Q;
owo(i, 0, 2*n) {
if(deg[i]==0) {
cout<<"NO\n";
exit(0);
}
if(deg[i]==1) {
Q.push(i);
flag[i] = true;
}
}
while(!Q.empty()) {
int u = Q.front();
Q.pop();
for(auto v: adj[u]) {
deg[v.first]--;
if(deg[v.first]==0) {
cout<<"NO\n";
exit(0);
}
if(flag[v.first])continue;
mid+=(v.second * (u<n ? 1 : -1));
if(deg[v.first]==1) {
flag[v.first] = true;
Q.push(v.first);
}
}
}
vector<int> kk;
owo(i, 0, 2*n) {
if(deg[i]<=1||flag[i])continue;
int nxt = dfs(i);
if(nxt<0)nxt*=-1;
mid-=nxt;
//cout<<nxt<<"\n";
kk.senpai(2*nxt);
}
//cout<<mid<<"\n";
dp[mid] = 1;
for(int p: kk) {
dp|=dp<<p;
}
bool ok = false;
owo(i, 20*n-k, 20*n+k+1) {
ok|=dp[i];
}
cout<<(ok ? "YES\n" : "NO\n");
return 0;
}
Compilation message (stderr)
tug.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
tug.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
# | 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... |