#include <bits/stdc++.h>
using namespace std;
int const N=30005;
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[100000];
bool tmdp[100000];
bool check(vector<int> ds,int k,int s1,int s2){
dp[0]=1;
for(auto i:ds){
for(int j=0;j<40005;j++)
tmdp[j]=0;
for(int j=0;j<40005;j++){
if(dp[j]){
tmdp[j+i]=1;
tmdp[abs(j-i)]=1;
}
}
for(int j=0;j<40005;j++)
dp[j]=tmdp[j];
}
for(int i=0;i<=40005;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(n>2000){
cout<<"YES"<<endl;
return 0;
}
if(check(ds,k,sum1,sum2))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3928 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
4052 KB |
Output is correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4052 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
2 ms |
3932 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
4048 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
3928 KB |
Output is correct |
23 |
Correct |
2 ms |
3932 KB |
Output is correct |
24 |
Correct |
2 ms |
3932 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3928 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
4052 KB |
Output is correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4052 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
2 ms |
3932 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
4048 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
3928 KB |
Output is correct |
23 |
Correct |
2 ms |
3932 KB |
Output is correct |
24 |
Correct |
2 ms |
3932 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
55 ms |
4696 KB |
Output is correct |
27 |
Correct |
13 ms |
4696 KB |
Output is correct |
28 |
Correct |
56 ms |
4720 KB |
Output is correct |
29 |
Correct |
13 ms |
4700 KB |
Output is correct |
30 |
Correct |
63 ms |
4700 KB |
Output is correct |
31 |
Correct |
13 ms |
4696 KB |
Output is correct |
32 |
Correct |
58 ms |
4736 KB |
Output is correct |
33 |
Correct |
16 ms |
4696 KB |
Output is correct |
34 |
Correct |
11 ms |
4700 KB |
Output is correct |
35 |
Correct |
13 ms |
4740 KB |
Output is correct |
36 |
Correct |
56 ms |
4700 KB |
Output is correct |
37 |
Correct |
18 ms |
4696 KB |
Output is correct |
38 |
Correct |
61 ms |
4948 KB |
Output is correct |
39 |
Correct |
13 ms |
4700 KB |
Output is correct |
40 |
Correct |
55 ms |
4652 KB |
Output is correct |
41 |
Correct |
13 ms |
4700 KB |
Output is correct |
42 |
Correct |
57 ms |
4700 KB |
Output is correct |
43 |
Correct |
13 ms |
4700 KB |
Output is correct |
44 |
Correct |
56 ms |
4700 KB |
Output is correct |
45 |
Correct |
13 ms |
4696 KB |
Output is correct |
46 |
Correct |
62 ms |
4700 KB |
Output is correct |
47 |
Correct |
4 ms |
4440 KB |
Output is correct |
48 |
Correct |
31 ms |
4700 KB |
Output is correct |
49 |
Correct |
30 ms |
4676 KB |
Output is correct |
50 |
Correct |
63 ms |
4712 KB |
Output is correct |
51 |
Correct |
63 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
16 ms |
7000 KB |
Output is correct |
3 |
Correct |
20 ms |
7004 KB |
Output is correct |
4 |
Correct |
16 ms |
7004 KB |
Output is correct |
5 |
Correct |
20 ms |
7004 KB |
Output is correct |
6 |
Correct |
16 ms |
6872 KB |
Output is correct |
7 |
Correct |
23 ms |
6872 KB |
Output is correct |
8 |
Correct |
16 ms |
7004 KB |
Output is correct |
9 |
Correct |
20 ms |
7000 KB |
Output is correct |
10 |
Correct |
16 ms |
7004 KB |
Output is correct |
11 |
Correct |
20 ms |
7000 KB |
Output is correct |
12 |
Correct |
16 ms |
7012 KB |
Output is correct |
13 |
Correct |
20 ms |
7000 KB |
Output is correct |
14 |
Correct |
20 ms |
7004 KB |
Output is correct |
15 |
Correct |
16 ms |
6884 KB |
Output is correct |
16 |
Correct |
20 ms |
7004 KB |
Output is correct |
17 |
Correct |
16 ms |
7004 KB |
Output is correct |
18 |
Correct |
20 ms |
7004 KB |
Output is correct |
19 |
Correct |
16 ms |
7004 KB |
Output is correct |
20 |
Correct |
20 ms |
7000 KB |
Output is correct |
21 |
Correct |
17 ms |
7000 KB |
Output is correct |
22 |
Correct |
23 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3928 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
2 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
4052 KB |
Output is correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Correct |
2 ms |
3932 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
4052 KB |
Output is correct |
12 |
Correct |
1 ms |
3932 KB |
Output is correct |
13 |
Correct |
2 ms |
3932 KB |
Output is correct |
14 |
Correct |
2 ms |
3932 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
1 ms |
3932 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
4048 KB |
Output is correct |
20 |
Correct |
1 ms |
3932 KB |
Output is correct |
21 |
Correct |
1 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
3928 KB |
Output is correct |
23 |
Correct |
2 ms |
3932 KB |
Output is correct |
24 |
Correct |
2 ms |
3932 KB |
Output is correct |
25 |
Correct |
2 ms |
3932 KB |
Output is correct |
26 |
Correct |
55 ms |
4696 KB |
Output is correct |
27 |
Correct |
13 ms |
4696 KB |
Output is correct |
28 |
Correct |
56 ms |
4720 KB |
Output is correct |
29 |
Correct |
13 ms |
4700 KB |
Output is correct |
30 |
Correct |
63 ms |
4700 KB |
Output is correct |
31 |
Correct |
13 ms |
4696 KB |
Output is correct |
32 |
Correct |
58 ms |
4736 KB |
Output is correct |
33 |
Correct |
16 ms |
4696 KB |
Output is correct |
34 |
Correct |
11 ms |
4700 KB |
Output is correct |
35 |
Correct |
13 ms |
4740 KB |
Output is correct |
36 |
Correct |
56 ms |
4700 KB |
Output is correct |
37 |
Correct |
18 ms |
4696 KB |
Output is correct |
38 |
Correct |
61 ms |
4948 KB |
Output is correct |
39 |
Correct |
13 ms |
4700 KB |
Output is correct |
40 |
Correct |
55 ms |
4652 KB |
Output is correct |
41 |
Correct |
13 ms |
4700 KB |
Output is correct |
42 |
Correct |
57 ms |
4700 KB |
Output is correct |
43 |
Correct |
13 ms |
4700 KB |
Output is correct |
44 |
Correct |
56 ms |
4700 KB |
Output is correct |
45 |
Correct |
13 ms |
4696 KB |
Output is correct |
46 |
Correct |
62 ms |
4700 KB |
Output is correct |
47 |
Correct |
4 ms |
4440 KB |
Output is correct |
48 |
Correct |
31 ms |
4700 KB |
Output is correct |
49 |
Correct |
30 ms |
4676 KB |
Output is correct |
50 |
Correct |
63 ms |
4712 KB |
Output is correct |
51 |
Correct |
63 ms |
4696 KB |
Output is correct |
52 |
Correct |
20 ms |
7004 KB |
Output is correct |
53 |
Correct |
16 ms |
7000 KB |
Output is correct |
54 |
Correct |
20 ms |
7004 KB |
Output is correct |
55 |
Correct |
16 ms |
7004 KB |
Output is correct |
56 |
Correct |
20 ms |
7004 KB |
Output is correct |
57 |
Correct |
16 ms |
6872 KB |
Output is correct |
58 |
Correct |
23 ms |
6872 KB |
Output is correct |
59 |
Correct |
16 ms |
7004 KB |
Output is correct |
60 |
Correct |
20 ms |
7000 KB |
Output is correct |
61 |
Correct |
16 ms |
7004 KB |
Output is correct |
62 |
Correct |
20 ms |
7000 KB |
Output is correct |
63 |
Correct |
16 ms |
7012 KB |
Output is correct |
64 |
Correct |
20 ms |
7000 KB |
Output is correct |
65 |
Correct |
20 ms |
7004 KB |
Output is correct |
66 |
Correct |
16 ms |
6884 KB |
Output is correct |
67 |
Correct |
20 ms |
7004 KB |
Output is correct |
68 |
Correct |
16 ms |
7004 KB |
Output is correct |
69 |
Correct |
20 ms |
7004 KB |
Output is correct |
70 |
Correct |
16 ms |
7004 KB |
Output is correct |
71 |
Correct |
20 ms |
7000 KB |
Output is correct |
72 |
Correct |
17 ms |
7000 KB |
Output is correct |
73 |
Correct |
23 ms |
7004 KB |
Output is correct |
74 |
Incorrect |
67 ms |
13392 KB |
Output isn't correct |
75 |
Halted |
0 ms |
0 KB |
- |