#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n,k;
cin>>n>>k;
vector<int>l(2*n),r(2*n),s(2*n);
set<int>v[2*n];
for(int i=0;i<2*n;i++){
cin>>l[i]>>r[i]>>s[i];
l[i]--;
r[i]--;
r[i]+=n;
v[l[i]].insert(i);
v[r[i]].insert(i);
}
queue<int>q;
bool ok=true;
for(int i=0;i<2*n;i++){
if(v[i].size()==0){
cout<<"NO";
return 0;
}
if(v[i].size()==1){
q.push(i);
}
}
vector<int>w(2*n,-1);
vector<bool>used(2*n,0);
while(!q.empty()){
int x=q.front();
q.pop();
if(v[x].size()==0) continue;
int y=*v[x].begin();
used[y]=true;
w[x]=s[y];
v[l[y]].erase(y);
v[r[y]].erase(y);
if(v[l[y]].size()==1){
q.push(l[y]);
}
if(v[r[y]].size()==1){
q.push(r[y]);
}
}
vector<int>adj[2*n];
vector<int>num;
function <void(int)> dfs=[&](int x){
if(used[x]==true) return;
num.pb(s[x]);
used[x]=true;
for(int y:adj[x]){
dfs(y);
}
};
for(int i=0;i<2*n;i++){
if(w[i]==-1){
int x=*v[i].begin();
v[i].erase(v[i].begin());
int y=*v[i].begin();
v[i].erase(y);
adj[x].pb(y);
adj[y].pb(x);
}
}
vector<int>dif;
for(int i=0;i<2*n;i++){
num.clear();
dfs(i);
if(!num.empty()){
int cur=0;
for(int j=0;j<(int)num.size();j++){
if(j%2==0) cur+=num[j];
else cur-=num[j];
}
dif.pb(abs(cur));
}
}
int cur=0;
for(int i=0;i<n;i++){
if(w[i]>-1) cur+=w[i];
}
for(int i=n;i<2*n;i++){
if(w[i]>-1) cur-=w[i];
}
dif.pb(abs(cur));
sort(all(dif));
vector<bool>can(20*n,0);
int tot=0;
can[0]=true;
for(int x:dif){
for(int j=tot;j>=0;j--){
can[j+x]=(can[j]|can[j+x]);
}
can[x]=true;
tot+=x;
}
int mn=1e9;
for(int i=0;i<=20*n;i++){
if(can[i]){
mn=min(mn,abs(tot-2*i));
}
}
if(mn<=k){
cout<<"YES";
}
else{
cout<<"NO";
}
}
Compilation message
tug.cpp: In function 'int main()':
tug.cpp:27:10: warning: unused variable 'ok' [-Wunused-variable]
27 | bool ok=true;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
28 ms |
1116 KB |
Output is correct |
27 |
Correct |
12 ms |
1112 KB |
Output is correct |
28 |
Correct |
29 ms |
1116 KB |
Output is correct |
29 |
Correct |
14 ms |
1116 KB |
Output is correct |
30 |
Correct |
28 ms |
1116 KB |
Output is correct |
31 |
Correct |
12 ms |
1116 KB |
Output is correct |
32 |
Correct |
28 ms |
1120 KB |
Output is correct |
33 |
Correct |
12 ms |
1116 KB |
Output is correct |
34 |
Correct |
7 ms |
1116 KB |
Output is correct |
35 |
Correct |
12 ms |
1112 KB |
Output is correct |
36 |
Correct |
29 ms |
1112 KB |
Output is correct |
37 |
Correct |
12 ms |
1116 KB |
Output is correct |
38 |
Correct |
29 ms |
1116 KB |
Output is correct |
39 |
Correct |
12 ms |
1116 KB |
Output is correct |
40 |
Correct |
28 ms |
1124 KB |
Output is correct |
41 |
Correct |
12 ms |
1116 KB |
Output is correct |
42 |
Correct |
29 ms |
1116 KB |
Output is correct |
43 |
Correct |
12 ms |
1288 KB |
Output is correct |
44 |
Correct |
30 ms |
1372 KB |
Output is correct |
45 |
Correct |
12 ms |
1368 KB |
Output is correct |
46 |
Correct |
31 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1116 KB |
Output is correct |
48 |
Correct |
9 ms |
1276 KB |
Output is correct |
49 |
Correct |
9 ms |
1116 KB |
Output is correct |
50 |
Correct |
119 ms |
1268 KB |
Output is correct |
51 |
Correct |
117 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4184 KB |
Output is correct |
2 |
Correct |
9 ms |
3416 KB |
Output is correct |
3 |
Correct |
12 ms |
4188 KB |
Output is correct |
4 |
Correct |
8 ms |
3420 KB |
Output is correct |
5 |
Correct |
12 ms |
4188 KB |
Output is correct |
6 |
Correct |
8 ms |
3420 KB |
Output is correct |
7 |
Correct |
12 ms |
4276 KB |
Output is correct |
8 |
Correct |
8 ms |
3428 KB |
Output is correct |
9 |
Correct |
11 ms |
4188 KB |
Output is correct |
10 |
Correct |
8 ms |
3320 KB |
Output is correct |
11 |
Correct |
11 ms |
4192 KB |
Output is correct |
12 |
Correct |
8 ms |
3428 KB |
Output is correct |
13 |
Correct |
16 ms |
4192 KB |
Output is correct |
14 |
Correct |
12 ms |
4196 KB |
Output is correct |
15 |
Correct |
8 ms |
3420 KB |
Output is correct |
16 |
Correct |
12 ms |
4188 KB |
Output is correct |
17 |
Correct |
8 ms |
3420 KB |
Output is correct |
18 |
Correct |
11 ms |
4216 KB |
Output is correct |
19 |
Correct |
8 ms |
3672 KB |
Output is correct |
20 |
Correct |
11 ms |
4184 KB |
Output is correct |
21 |
Correct |
8 ms |
3420 KB |
Output is correct |
22 |
Correct |
11 ms |
4056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
28 ms |
1116 KB |
Output is correct |
27 |
Correct |
12 ms |
1112 KB |
Output is correct |
28 |
Correct |
29 ms |
1116 KB |
Output is correct |
29 |
Correct |
14 ms |
1116 KB |
Output is correct |
30 |
Correct |
28 ms |
1116 KB |
Output is correct |
31 |
Correct |
12 ms |
1116 KB |
Output is correct |
32 |
Correct |
28 ms |
1120 KB |
Output is correct |
33 |
Correct |
12 ms |
1116 KB |
Output is correct |
34 |
Correct |
7 ms |
1116 KB |
Output is correct |
35 |
Correct |
12 ms |
1112 KB |
Output is correct |
36 |
Correct |
29 ms |
1112 KB |
Output is correct |
37 |
Correct |
12 ms |
1116 KB |
Output is correct |
38 |
Correct |
29 ms |
1116 KB |
Output is correct |
39 |
Correct |
12 ms |
1116 KB |
Output is correct |
40 |
Correct |
28 ms |
1124 KB |
Output is correct |
41 |
Correct |
12 ms |
1116 KB |
Output is correct |
42 |
Correct |
29 ms |
1116 KB |
Output is correct |
43 |
Correct |
12 ms |
1288 KB |
Output is correct |
44 |
Correct |
30 ms |
1372 KB |
Output is correct |
45 |
Correct |
12 ms |
1368 KB |
Output is correct |
46 |
Correct |
31 ms |
1116 KB |
Output is correct |
47 |
Correct |
2 ms |
1116 KB |
Output is correct |
48 |
Correct |
9 ms |
1276 KB |
Output is correct |
49 |
Correct |
9 ms |
1116 KB |
Output is correct |
50 |
Correct |
119 ms |
1268 KB |
Output is correct |
51 |
Correct |
117 ms |
1112 KB |
Output is correct |
52 |
Correct |
15 ms |
4184 KB |
Output is correct |
53 |
Correct |
9 ms |
3416 KB |
Output is correct |
54 |
Correct |
12 ms |
4188 KB |
Output is correct |
55 |
Correct |
8 ms |
3420 KB |
Output is correct |
56 |
Correct |
12 ms |
4188 KB |
Output is correct |
57 |
Correct |
8 ms |
3420 KB |
Output is correct |
58 |
Correct |
12 ms |
4276 KB |
Output is correct |
59 |
Correct |
8 ms |
3428 KB |
Output is correct |
60 |
Correct |
11 ms |
4188 KB |
Output is correct |
61 |
Correct |
8 ms |
3320 KB |
Output is correct |
62 |
Correct |
11 ms |
4192 KB |
Output is correct |
63 |
Correct |
8 ms |
3428 KB |
Output is correct |
64 |
Correct |
16 ms |
4192 KB |
Output is correct |
65 |
Correct |
12 ms |
4196 KB |
Output is correct |
66 |
Correct |
8 ms |
3420 KB |
Output is correct |
67 |
Correct |
12 ms |
4188 KB |
Output is correct |
68 |
Correct |
8 ms |
3420 KB |
Output is correct |
69 |
Correct |
11 ms |
4216 KB |
Output is correct |
70 |
Correct |
8 ms |
3672 KB |
Output is correct |
71 |
Correct |
11 ms |
4184 KB |
Output is correct |
72 |
Correct |
8 ms |
3420 KB |
Output is correct |
73 |
Correct |
11 ms |
4056 KB |
Output is correct |
74 |
Execution timed out |
3042 ms |
12076 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |