#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=600005;
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 |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
4 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
3 ms |
11100 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
3 ms |
11096 KB |
Output is correct |
7 |
Correct |
3 ms |
11100 KB |
Output is correct |
8 |
Correct |
4 ms |
11096 KB |
Output is correct |
9 |
Correct |
3 ms |
11100 KB |
Output is correct |
10 |
Correct |
3 ms |
11100 KB |
Output is correct |
11 |
Correct |
3 ms |
11184 KB |
Output is correct |
12 |
Correct |
3 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
11100 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
3 ms |
11184 KB |
Output is correct |
16 |
Correct |
3 ms |
11100 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
3 ms |
11100 KB |
Output is correct |
19 |
Correct |
3 ms |
11100 KB |
Output is correct |
20 |
Correct |
4 ms |
11100 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
3 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11100 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
4 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
3 ms |
11100 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
3 ms |
11096 KB |
Output is correct |
7 |
Correct |
3 ms |
11100 KB |
Output is correct |
8 |
Correct |
4 ms |
11096 KB |
Output is correct |
9 |
Correct |
3 ms |
11100 KB |
Output is correct |
10 |
Correct |
3 ms |
11100 KB |
Output is correct |
11 |
Correct |
3 ms |
11184 KB |
Output is correct |
12 |
Correct |
3 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
11100 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
3 ms |
11184 KB |
Output is correct |
16 |
Correct |
3 ms |
11100 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
3 ms |
11100 KB |
Output is correct |
19 |
Correct |
3 ms |
11100 KB |
Output is correct |
20 |
Correct |
4 ms |
11100 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
3 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11100 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
159 ms |
11868 KB |
Output is correct |
27 |
Correct |
25 ms |
11864 KB |
Output is correct |
28 |
Correct |
156 ms |
11904 KB |
Output is correct |
29 |
Correct |
24 ms |
11868 KB |
Output is correct |
30 |
Correct |
154 ms |
11868 KB |
Output is correct |
31 |
Correct |
25 ms |
11868 KB |
Output is correct |
32 |
Correct |
155 ms |
11868 KB |
Output is correct |
33 |
Correct |
25 ms |
11864 KB |
Output is correct |
34 |
Correct |
17 ms |
11868 KB |
Output is correct |
35 |
Correct |
25 ms |
11864 KB |
Output is correct |
36 |
Correct |
152 ms |
11868 KB |
Output is correct |
37 |
Correct |
24 ms |
11868 KB |
Output is correct |
38 |
Correct |
157 ms |
11908 KB |
Output is correct |
39 |
Correct |
24 ms |
11864 KB |
Output is correct |
40 |
Correct |
158 ms |
11868 KB |
Output is correct |
41 |
Correct |
26 ms |
11864 KB |
Output is correct |
42 |
Correct |
154 ms |
11868 KB |
Output is correct |
43 |
Correct |
24 ms |
11864 KB |
Output is correct |
44 |
Correct |
153 ms |
11916 KB |
Output is correct |
45 |
Correct |
25 ms |
11868 KB |
Output is correct |
46 |
Correct |
155 ms |
11868 KB |
Output is correct |
47 |
Correct |
4 ms |
11608 KB |
Output is correct |
48 |
Correct |
80 ms |
11916 KB |
Output is correct |
49 |
Correct |
89 ms |
11864 KB |
Output is correct |
50 |
Correct |
158 ms |
11868 KB |
Output is correct |
51 |
Correct |
159 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14680 KB |
Output is correct |
2 |
Correct |
13 ms |
14684 KB |
Output is correct |
3 |
Correct |
11 ms |
14820 KB |
Output is correct |
4 |
Correct |
12 ms |
14684 KB |
Output is correct |
5 |
Correct |
11 ms |
14684 KB |
Output is correct |
6 |
Correct |
17 ms |
14816 KB |
Output is correct |
7 |
Correct |
11 ms |
14684 KB |
Output is correct |
8 |
Correct |
14 ms |
14680 KB |
Output is correct |
9 |
Correct |
11 ms |
14684 KB |
Output is correct |
10 |
Correct |
13 ms |
14684 KB |
Output is correct |
11 |
Correct |
12 ms |
14716 KB |
Output is correct |
12 |
Correct |
13 ms |
14888 KB |
Output is correct |
13 |
Correct |
11 ms |
14684 KB |
Output is correct |
14 |
Correct |
12 ms |
14768 KB |
Output is correct |
15 |
Correct |
12 ms |
14780 KB |
Output is correct |
16 |
Correct |
13 ms |
14684 KB |
Output is correct |
17 |
Correct |
12 ms |
14684 KB |
Output is correct |
18 |
Correct |
11 ms |
14684 KB |
Output is correct |
19 |
Correct |
12 ms |
14684 KB |
Output is correct |
20 |
Correct |
12 ms |
14680 KB |
Output is correct |
21 |
Correct |
13 ms |
14684 KB |
Output is correct |
22 |
Correct |
22 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
4 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
3 ms |
11100 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
3 ms |
11096 KB |
Output is correct |
7 |
Correct |
3 ms |
11100 KB |
Output is correct |
8 |
Correct |
4 ms |
11096 KB |
Output is correct |
9 |
Correct |
3 ms |
11100 KB |
Output is correct |
10 |
Correct |
3 ms |
11100 KB |
Output is correct |
11 |
Correct |
3 ms |
11184 KB |
Output is correct |
12 |
Correct |
3 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
11100 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
3 ms |
11184 KB |
Output is correct |
16 |
Correct |
3 ms |
11100 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
3 ms |
11100 KB |
Output is correct |
19 |
Correct |
3 ms |
11100 KB |
Output is correct |
20 |
Correct |
4 ms |
11100 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
3 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11100 KB |
Output is correct |
24 |
Correct |
3 ms |
11352 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
159 ms |
11868 KB |
Output is correct |
27 |
Correct |
25 ms |
11864 KB |
Output is correct |
28 |
Correct |
156 ms |
11904 KB |
Output is correct |
29 |
Correct |
24 ms |
11868 KB |
Output is correct |
30 |
Correct |
154 ms |
11868 KB |
Output is correct |
31 |
Correct |
25 ms |
11868 KB |
Output is correct |
32 |
Correct |
155 ms |
11868 KB |
Output is correct |
33 |
Correct |
25 ms |
11864 KB |
Output is correct |
34 |
Correct |
17 ms |
11868 KB |
Output is correct |
35 |
Correct |
25 ms |
11864 KB |
Output is correct |
36 |
Correct |
152 ms |
11868 KB |
Output is correct |
37 |
Correct |
24 ms |
11868 KB |
Output is correct |
38 |
Correct |
157 ms |
11908 KB |
Output is correct |
39 |
Correct |
24 ms |
11864 KB |
Output is correct |
40 |
Correct |
158 ms |
11868 KB |
Output is correct |
41 |
Correct |
26 ms |
11864 KB |
Output is correct |
42 |
Correct |
154 ms |
11868 KB |
Output is correct |
43 |
Correct |
24 ms |
11864 KB |
Output is correct |
44 |
Correct |
153 ms |
11916 KB |
Output is correct |
45 |
Correct |
25 ms |
11868 KB |
Output is correct |
46 |
Correct |
155 ms |
11868 KB |
Output is correct |
47 |
Correct |
4 ms |
11608 KB |
Output is correct |
48 |
Correct |
80 ms |
11916 KB |
Output is correct |
49 |
Correct |
89 ms |
11864 KB |
Output is correct |
50 |
Correct |
158 ms |
11868 KB |
Output is correct |
51 |
Correct |
159 ms |
11864 KB |
Output is correct |
52 |
Correct |
11 ms |
14680 KB |
Output is correct |
53 |
Correct |
13 ms |
14684 KB |
Output is correct |
54 |
Correct |
11 ms |
14820 KB |
Output is correct |
55 |
Correct |
12 ms |
14684 KB |
Output is correct |
56 |
Correct |
11 ms |
14684 KB |
Output is correct |
57 |
Correct |
17 ms |
14816 KB |
Output is correct |
58 |
Correct |
11 ms |
14684 KB |
Output is correct |
59 |
Correct |
14 ms |
14680 KB |
Output is correct |
60 |
Correct |
11 ms |
14684 KB |
Output is correct |
61 |
Correct |
13 ms |
14684 KB |
Output is correct |
62 |
Correct |
12 ms |
14716 KB |
Output is correct |
63 |
Correct |
13 ms |
14888 KB |
Output is correct |
64 |
Correct |
11 ms |
14684 KB |
Output is correct |
65 |
Correct |
12 ms |
14768 KB |
Output is correct |
66 |
Correct |
12 ms |
14780 KB |
Output is correct |
67 |
Correct |
13 ms |
14684 KB |
Output is correct |
68 |
Correct |
12 ms |
14684 KB |
Output is correct |
69 |
Correct |
11 ms |
14684 KB |
Output is correct |
70 |
Correct |
12 ms |
14684 KB |
Output is correct |
71 |
Correct |
12 ms |
14680 KB |
Output is correct |
72 |
Correct |
13 ms |
14684 KB |
Output is correct |
73 |
Correct |
22 ms |
14680 KB |
Output is correct |
74 |
Correct |
2310 ms |
22872 KB |
Output is correct |
75 |
Correct |
142 ms |
22880 KB |
Output is correct |
76 |
Correct |
2356 ms |
22864 KB |
Output is correct |
77 |
Correct |
150 ms |
22876 KB |
Output is correct |
78 |
Correct |
2297 ms |
22860 KB |
Output is correct |
79 |
Correct |
2330 ms |
22856 KB |
Output is correct |
80 |
Correct |
131 ms |
22864 KB |
Output is correct |
81 |
Correct |
134 ms |
22612 KB |
Output is correct |
82 |
Correct |
2347 ms |
22612 KB |
Output is correct |
83 |
Correct |
2283 ms |
22856 KB |
Output is correct |
84 |
Correct |
130 ms |
22720 KB |
Output is correct |
85 |
Correct |
2237 ms |
22864 KB |
Output is correct |
86 |
Correct |
127 ms |
22880 KB |
Output is correct |
87 |
Correct |
2293 ms |
22856 KB |
Output is correct |
88 |
Correct |
128 ms |
22864 KB |
Output is correct |
89 |
Correct |
2323 ms |
22856 KB |
Output is correct |
90 |
Correct |
140 ms |
23124 KB |
Output is correct |
91 |
Correct |
2301 ms |
22864 KB |
Output is correct |
92 |
Correct |
132 ms |
22864 KB |
Output is correct |
93 |
Correct |
2301 ms |
23100 KB |
Output is correct |
94 |
Correct |
41 ms |
22808 KB |
Output is correct |
95 |
Correct |
1231 ms |
22868 KB |
Output is correct |
96 |
Correct |
1214 ms |
22852 KB |
Output is correct |
97 |
Correct |
2349 ms |
22852 KB |
Output is correct |
98 |
Correct |
2392 ms |
22852 KB |
Output is correct |