#include <bits/stdc++.h>
using namespace std;
int const N=30005;
int const maxx=6e5;
set<int> mem[2*N];
set<pair<int,int>> sz;
int L[2*N],R[2*N],S[2*N];
bool done[2*N];
bool dp[maxx];
bool tmdp[maxx];
bool check(vector<int> ds,int k,int s1,int s2){
dp[0]=1;
for(auto i:ds){
for(int j=0;j<maxx;j++)
tmdp[j]=0;
for(int j=0;j<maxx;j++){
if(dp[j]){
tmdp[j+i]=1;
tmdp[abs(j-i)]=1;
}
}
for(int j=0;j<maxx;j++)
dp[j]=tmdp[j];
}
for(int i=0;i<=maxx;i++)
if(dp[i] && (abs((i+s1)-s2)<=k || abs((i+s2)-s1)<=k))
return 1;
return 0;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=2*n;i++){
int s,l,r;
cin>>l>>r>>s;
r+=n;
mem[l].insert(i);
mem[r].insert(i);
L[i]=l;
R[i]=r;
S[i]=s;
}
for(int i=1;i<=2*n;i++)
sz.insert({mem[i].size(),i});
int sum1=0,sum2=0;
while(sz.size()){
auto p=*(sz.begin());
int job=p.second;
int cons=p.first;
// cout<<job<<' '<<cons<<endl;
if(cons==2)
break;
sz.erase(sz.begin());
if(cons==0){
cout<<"NO"<<endl;
return 0;
}
else if(cons==1){
int per=*(mem[job].begin());
if(job<=n)
sum1+=S[per];
else
sum2+=S[per];
// cout<<per<<' '<<L[per]<<' '<<R[per]<<endl;
sz.erase({mem[L[per]].size(),L[per]});
sz.erase({mem[R[per]].size(),R[per]});
mem[L[per]].erase(per);
mem[R[per]].erase(per);
if(done[L[per]]==0 && L[per]!=job)
sz.insert({mem[L[per]].size(),L[per]});
if(done[R[per]]==0 && R[per]!=job)
sz.insert({mem[R[per]].size(),R[per]});
done[job]=1;
}
else
break;
}
//now all are with degree two.
vector<int> ds;
int temp=0;
while(sz.size()){
auto p=*(sz.begin());
int job=p.second;
int cons=p.first;
// cout<<job<<' '<<cons<<endl;
sz.erase(sz.begin());
if(cons==2 && temp!=0){
ds.push_back(abs(temp));
temp=0;
}
int per=*(mem[job].begin());
if(job<=n)
temp+=S[per];
else
temp-=S[per];
// cout<<per<<' '<<L[per]<<' '<<R[per]<<endl;
sz.erase({mem[L[per]].size(),L[per]});
sz.erase({mem[R[per]].size(),R[per]});
mem[L[per]].erase(per);
mem[R[per]].erase(per);
if(done[L[per]]==0 && L[per]!=job)
sz.insert({mem[L[per]].size(),L[per]});
if(done[R[per]]==0 && R[per]!=job)
sz.insert({mem[R[per]].size(),R[per]});
done[job]=1;
}
if(temp!=0)
ds.push_back(abs(temp));
// make a sum <= k from ds;
if(check(ds,k,sum1,sum2))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
7768 KB |
Output is correct |
2 |
Correct |
17 ms |
7772 KB |
Output is correct |
3 |
Correct |
23 ms |
8016 KB |
Output is correct |
4 |
Correct |
21 ms |
7768 KB |
Output is correct |
5 |
Correct |
19 ms |
7768 KB |
Output is correct |
6 |
Correct |
17 ms |
7772 KB |
Output is correct |
7 |
Correct |
20 ms |
7772 KB |
Output is correct |
8 |
Correct |
16 ms |
7772 KB |
Output is correct |
9 |
Correct |
19 ms |
7644 KB |
Output is correct |
10 |
Correct |
16 ms |
7772 KB |
Output is correct |
11 |
Correct |
19 ms |
7660 KB |
Output is correct |
12 |
Correct |
16 ms |
7696 KB |
Output is correct |
13 |
Correct |
20 ms |
7768 KB |
Output is correct |
14 |
Correct |
23 ms |
7772 KB |
Output is correct |
15 |
Correct |
16 ms |
7768 KB |
Output is correct |
16 |
Correct |
20 ms |
7772 KB |
Output is correct |
17 |
Correct |
15 ms |
7772 KB |
Output is correct |
18 |
Correct |
20 ms |
7844 KB |
Output is correct |
19 |
Correct |
15 ms |
7772 KB |
Output is correct |
20 |
Correct |
20 ms |
7836 KB |
Output is correct |
21 |
Correct |
16 ms |
7768 KB |
Output is correct |
22 |
Correct |
23 ms |
7772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
4952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |