/*The best way to get something done is to begin*/
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define frd(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define fi first
#define se second
#define el '\n'
using namespace std;
template <class X, class Y> bool minimize(X &x, const Y &y) {if (x > y) {x = y;return true;} else return false;}
template <class X, class Y> bool maximize(X &x, const Y &y) {if (x < y) {x = y;return true;} else return false;}
/* Author: Huynh Quoc Luat */
/*Khai Bao*/
const long long inf=1e18;
const int oo=1e9;
const int LO=21;
const int CH=27;
const int N=6e5+5;
const int sm=1e9+7;
int n,k;
multiset <pair <int,int>> g[N];
queue <int> q;
//void add(int &x,const int &y){x+=y;if(x>=sm)x-=sm;}
//void sub(int &x,const int &y){x-=y;if(x<0)x+=sm;}
void read()
{
cin >> n >> k;
fr(i,1,2*n){
int l,r,s;
cin >> l >> r >> s;
g[l].insert({n+r,s});
g[n+r].insert({l,-s});
}
}
namespace sub1
{
int tot;
bool check[N];
bitset <N> possible;
void dfs(int u){
check[u]=1;
if(g[u].empty()) return;
int nxt=g[u].begin()->fi;
int cost=g[u].begin()->se;
tot+=cost;
if(!check[nxt]){
g[nxt].erase(g[nxt].find({u,-cost}));
g[u].clear();
dfs(nxt);
}
}
void process()
{
fr(i,1,2*n){
if(g[i].size()==1) q.push(i);
if(g[i].size()==0){
cout << "NO";
return;
}
}
while(!q.empty()){
int cur=q.front();
q.pop();
if(!g[cur].size()){
cout << "NO";
return;
}
int nxt=g[cur].begin()->fi;
int cost=g[cur].begin()->se;
tot+=cost;
g[cur].clear();
g[nxt].erase(g[nxt].find({cur,-cost}));
if(g[nxt].size()==1) q.push(nxt);
}
vector <int> items;
if(tot) items.pb(abs(tot));
fr(i,1,2*n){
if(!check[i] and !g[i].empty()){
tot=0;
g[i].erase(g[i].begin());
dfs(i);
if(tot) items.pb(abs(tot));
}
}
int sum=0;
for(auto v:items) sum+=v;
possible[0]=1;
fr(i,1,sum) possible |= possible << i;
fr(i,0,sum){
if(possible[i] and abs(2*i-sum)<=k){
cout << "YES";
return;
}
}
}
}
void time() {cerr<< endl<<"Luattapcode: "<<clock()<<"ms"<<endl;}
main()
{
ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
#define TASK "qn"
if(fopen(TASK".INP","r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
int mtt=0;
int test=1;
if(mtt) cin>>test;
while(test--)
{
read();
sub1::process();
}
time();
return 0;
}
Compilation message (stderr)
tug.cpp:102:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
102 | main()
| ^~~~
tug.cpp: In function 'int main()':
tug.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tug.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |