#pragma GCC target("avx2,bmi,bmi2,popcnt")
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;
const ll mxN=6e4+5;
const ll MAX=1200005;
ll l[mxN];
ll r[mxN];
ll s[mxN];
ll lg[mxN];
ll rg[mxN];
ll lsum=0;
ll rsum=0;
set<pll> adj[mxN][2];
vll in[mxN];
ll cnt=0;
pll val[mxN];
bool visited[mxN][2];
bool done[mxN][2];
vector<pll> stk;
ll dt;
bitset<2*MAX> dp;
bitset<2*MAX> new_dp;
ll dfs(ll cur, ll idx, ll type){
if(visited[cur][type]){
return 0;
}
ll re=s[idx];
if(type==0) re*=-1;
//cout<<cur<<' '<<type<<' '<<re<<'\n';
visited[cur][type]=1;
done[cur][type]=1;
for(auto &[x, y]:adj[cur][type]){
if(y==dt) continue;
re+=dfs(x, y, (type+1)%2);
}
stk.pb({cur, type});
return re;
}
ll getidx(ll a){
return a+MAX;
}
void solve(){
memset(lg, 0, sizeof(lg));
memset(rg, 0, sizeof(rg));
memset(visited, 0, sizeof(visited));
memset(done, 0, sizeof(done));
ll n, k;
cin>>n>>k;
bool good=1;
for(ll i=0;i<2*n;i++){
cin>>l[i]>>r[i]>>s[i];
l[i]--; r[i]--;
if(s[i]!=1){
good=0;
}
adj[l[i]][0].insert({r[i], i});
adj[r[i]][1].insert({l[i], i});
}
set<pair<ll, pll>> st;
for(ll i=0;i<n;i++){
for(ll j=0;j<2;j++){
st.insert({adj[i][j].size(), {i, j}});
}
}
while(!st.empty()){
pair<ll, pll> tep=*st.begin();
st.erase(tep);
if(tep.F<=0){
cout<<"NO\n";
return;
}
if(tep.F>=2){
break;
}
pll cur=*adj[tep.S.F][tep.S.S].begin();
//adj[tep.S.F][tep.S.S].erase(cur);
if(tep.S.S==0){
lsum+=s[cur.S];
done[tep.S.F][0]=1;
st.erase({adj[cur.F][1].size(), {cur.F, 1}});
adj[cur.F][1].erase({tep.S.F, cur.S});
adj[tep.S.F][0].erase({cur.F, cur.S});
st.insert({adj[cur.F][1].size(), {cur.F, 1}});
}
else{
rsum+=s[cur.S];
done[tep.S.F][1]=1;
st.erase({adj[cur.F][0].size(), {cur.F, 0}});
adj[cur.F][0].erase({tep.S.F, cur.S});
adj[tep.S.F][1].erase({cur.F, cur.S});
st.insert({adj[cur.F][0].size(), {cur.F, 0}});
}
}
if(good){
cout<<"YES\n";
return;
}
for(ll i=0;i<n;i++){
if(!done[i][0]){
dt=(*adj[i][0].begin()).S;
val[cnt].F=dfs(i, dt, 0);
for(auto &[x, y]:stk){
visited[x][y]=0;
}
stk.clear();
val[cnt].S=dfs(r[dt], dt, 1);
stk.clear();
//cout<<val[cnt].F<<' '<<val[cnt].S<<'\n';
cnt++;
}
}
dp.reset();
dp[getidx(rsum-lsum)]=1;
for(ll i=0;i<cnt;i++){
new_dp.reset();
if(val[i].F>0){
new_dp=(new_dp | (dp<<val[i].F));
}
else{
new_dp=(new_dp | (dp>>(-val[i].F)));
}
if(val[i].S>0){
new_dp=(new_dp | (dp<<val[i].S));
}
else{
new_dp=(new_dp | (dp>>(-val[i].S)));
}
dp=new_dp;
}
for(ll i=0;i<=k;i++){
if(dp[getidx(i)]){
//cout<<i<<' ';
cout<<"YES\n";
return;
}
if(dp[getidx(-i)]){
//cout<<-i<<' ';
cout<<"YES\n";
return;
}
}
cout<<"NO\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11352 KB |
Output is correct |
2 |
Correct |
5 ms |
11356 KB |
Output is correct |
3 |
Correct |
6 ms |
11464 KB |
Output is correct |
4 |
Correct |
5 ms |
11356 KB |
Output is correct |
5 |
Correct |
4 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11356 KB |
Output is correct |
7 |
Correct |
4 ms |
11608 KB |
Output is correct |
8 |
Correct |
4 ms |
11356 KB |
Output is correct |
9 |
Correct |
5 ms |
11448 KB |
Output is correct |
10 |
Correct |
5 ms |
11356 KB |
Output is correct |
11 |
Correct |
4 ms |
11356 KB |
Output is correct |
12 |
Correct |
4 ms |
11356 KB |
Output is correct |
13 |
Correct |
4 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11356 KB |
Output is correct |
15 |
Correct |
4 ms |
11480 KB |
Output is correct |
16 |
Correct |
4 ms |
11492 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11440 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11100 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
25 |
Correct |
4 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11352 KB |
Output is correct |
2 |
Correct |
5 ms |
11356 KB |
Output is correct |
3 |
Correct |
6 ms |
11464 KB |
Output is correct |
4 |
Correct |
5 ms |
11356 KB |
Output is correct |
5 |
Correct |
4 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11356 KB |
Output is correct |
7 |
Correct |
4 ms |
11608 KB |
Output is correct |
8 |
Correct |
4 ms |
11356 KB |
Output is correct |
9 |
Correct |
5 ms |
11448 KB |
Output is correct |
10 |
Correct |
5 ms |
11356 KB |
Output is correct |
11 |
Correct |
4 ms |
11356 KB |
Output is correct |
12 |
Correct |
4 ms |
11356 KB |
Output is correct |
13 |
Correct |
4 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11356 KB |
Output is correct |
15 |
Correct |
4 ms |
11480 KB |
Output is correct |
16 |
Correct |
4 ms |
11492 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11440 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11100 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
25 |
Correct |
4 ms |
11352 KB |
Output is correct |
26 |
Correct |
335 ms |
12248 KB |
Output is correct |
27 |
Correct |
48 ms |
12124 KB |
Output is correct |
28 |
Correct |
327 ms |
12244 KB |
Output is correct |
29 |
Correct |
48 ms |
12124 KB |
Output is correct |
30 |
Correct |
327 ms |
12236 KB |
Output is correct |
31 |
Correct |
49 ms |
12124 KB |
Output is correct |
32 |
Correct |
326 ms |
12120 KB |
Output is correct |
33 |
Correct |
49 ms |
12120 KB |
Output is correct |
34 |
Correct |
28 ms |
12244 KB |
Output is correct |
35 |
Correct |
49 ms |
12244 KB |
Output is correct |
36 |
Correct |
337 ms |
12244 KB |
Output is correct |
37 |
Correct |
48 ms |
12124 KB |
Output is correct |
38 |
Correct |
333 ms |
12252 KB |
Output is correct |
39 |
Correct |
48 ms |
12124 KB |
Output is correct |
40 |
Correct |
326 ms |
12240 KB |
Output is correct |
41 |
Correct |
57 ms |
12120 KB |
Output is correct |
42 |
Correct |
337 ms |
12244 KB |
Output is correct |
43 |
Correct |
49 ms |
12120 KB |
Output is correct |
44 |
Correct |
327 ms |
12248 KB |
Output is correct |
45 |
Correct |
49 ms |
12124 KB |
Output is correct |
46 |
Correct |
325 ms |
12248 KB |
Output is correct |
47 |
Correct |
4 ms |
11868 KB |
Output is correct |
48 |
Correct |
167 ms |
12248 KB |
Output is correct |
49 |
Correct |
168 ms |
12376 KB |
Output is correct |
50 |
Correct |
330 ms |
12120 KB |
Output is correct |
51 |
Correct |
336 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
15196 KB |
Output is correct |
2 |
Correct |
12 ms |
15196 KB |
Output is correct |
3 |
Correct |
11 ms |
15196 KB |
Output is correct |
4 |
Correct |
21 ms |
15092 KB |
Output is correct |
5 |
Correct |
17 ms |
15192 KB |
Output is correct |
6 |
Correct |
22 ms |
15232 KB |
Output is correct |
7 |
Correct |
13 ms |
15360 KB |
Output is correct |
8 |
Correct |
20 ms |
15036 KB |
Output is correct |
9 |
Correct |
11 ms |
15196 KB |
Output is correct |
10 |
Correct |
12 ms |
15008 KB |
Output is correct |
11 |
Correct |
12 ms |
15192 KB |
Output is correct |
12 |
Correct |
12 ms |
15196 KB |
Output is correct |
13 |
Correct |
13 ms |
15176 KB |
Output is correct |
14 |
Correct |
15 ms |
15192 KB |
Output is correct |
15 |
Correct |
12 ms |
15192 KB |
Output is correct |
16 |
Correct |
12 ms |
15180 KB |
Output is correct |
17 |
Correct |
12 ms |
15088 KB |
Output is correct |
18 |
Correct |
11 ms |
15196 KB |
Output is correct |
19 |
Correct |
12 ms |
15192 KB |
Output is correct |
20 |
Correct |
11 ms |
15192 KB |
Output is correct |
21 |
Correct |
14 ms |
15196 KB |
Output is correct |
22 |
Correct |
16 ms |
15120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11352 KB |
Output is correct |
2 |
Correct |
5 ms |
11356 KB |
Output is correct |
3 |
Correct |
6 ms |
11464 KB |
Output is correct |
4 |
Correct |
5 ms |
11356 KB |
Output is correct |
5 |
Correct |
4 ms |
11356 KB |
Output is correct |
6 |
Correct |
4 ms |
11356 KB |
Output is correct |
7 |
Correct |
4 ms |
11608 KB |
Output is correct |
8 |
Correct |
4 ms |
11356 KB |
Output is correct |
9 |
Correct |
5 ms |
11448 KB |
Output is correct |
10 |
Correct |
5 ms |
11356 KB |
Output is correct |
11 |
Correct |
4 ms |
11356 KB |
Output is correct |
12 |
Correct |
4 ms |
11356 KB |
Output is correct |
13 |
Correct |
4 ms |
11356 KB |
Output is correct |
14 |
Correct |
4 ms |
11356 KB |
Output is correct |
15 |
Correct |
4 ms |
11480 KB |
Output is correct |
16 |
Correct |
4 ms |
11492 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11440 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11100 KB |
Output is correct |
22 |
Correct |
3 ms |
11356 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
25 |
Correct |
4 ms |
11352 KB |
Output is correct |
26 |
Correct |
335 ms |
12248 KB |
Output is correct |
27 |
Correct |
48 ms |
12124 KB |
Output is correct |
28 |
Correct |
327 ms |
12244 KB |
Output is correct |
29 |
Correct |
48 ms |
12124 KB |
Output is correct |
30 |
Correct |
327 ms |
12236 KB |
Output is correct |
31 |
Correct |
49 ms |
12124 KB |
Output is correct |
32 |
Correct |
326 ms |
12120 KB |
Output is correct |
33 |
Correct |
49 ms |
12120 KB |
Output is correct |
34 |
Correct |
28 ms |
12244 KB |
Output is correct |
35 |
Correct |
49 ms |
12244 KB |
Output is correct |
36 |
Correct |
337 ms |
12244 KB |
Output is correct |
37 |
Correct |
48 ms |
12124 KB |
Output is correct |
38 |
Correct |
333 ms |
12252 KB |
Output is correct |
39 |
Correct |
48 ms |
12124 KB |
Output is correct |
40 |
Correct |
326 ms |
12240 KB |
Output is correct |
41 |
Correct |
57 ms |
12120 KB |
Output is correct |
42 |
Correct |
337 ms |
12244 KB |
Output is correct |
43 |
Correct |
49 ms |
12120 KB |
Output is correct |
44 |
Correct |
327 ms |
12248 KB |
Output is correct |
45 |
Correct |
49 ms |
12124 KB |
Output is correct |
46 |
Correct |
325 ms |
12248 KB |
Output is correct |
47 |
Correct |
4 ms |
11868 KB |
Output is correct |
48 |
Correct |
167 ms |
12248 KB |
Output is correct |
49 |
Correct |
168 ms |
12376 KB |
Output is correct |
50 |
Correct |
330 ms |
12120 KB |
Output is correct |
51 |
Correct |
336 ms |
12124 KB |
Output is correct |
52 |
Correct |
18 ms |
15196 KB |
Output is correct |
53 |
Correct |
12 ms |
15196 KB |
Output is correct |
54 |
Correct |
11 ms |
15196 KB |
Output is correct |
55 |
Correct |
21 ms |
15092 KB |
Output is correct |
56 |
Correct |
17 ms |
15192 KB |
Output is correct |
57 |
Correct |
22 ms |
15232 KB |
Output is correct |
58 |
Correct |
13 ms |
15360 KB |
Output is correct |
59 |
Correct |
20 ms |
15036 KB |
Output is correct |
60 |
Correct |
11 ms |
15196 KB |
Output is correct |
61 |
Correct |
12 ms |
15008 KB |
Output is correct |
62 |
Correct |
12 ms |
15192 KB |
Output is correct |
63 |
Correct |
12 ms |
15196 KB |
Output is correct |
64 |
Correct |
13 ms |
15176 KB |
Output is correct |
65 |
Correct |
15 ms |
15192 KB |
Output is correct |
66 |
Correct |
12 ms |
15192 KB |
Output is correct |
67 |
Correct |
12 ms |
15180 KB |
Output is correct |
68 |
Correct |
12 ms |
15088 KB |
Output is correct |
69 |
Correct |
11 ms |
15196 KB |
Output is correct |
70 |
Correct |
12 ms |
15192 KB |
Output is correct |
71 |
Correct |
11 ms |
15192 KB |
Output is correct |
72 |
Correct |
14 ms |
15196 KB |
Output is correct |
73 |
Correct |
16 ms |
15120 KB |
Output is correct |
74 |
Execution timed out |
3027 ms |
23388 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |