#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=6e4+50;
const int mmax=1e6+2e5+50;
const int mod=1e9+7;
using namespace std;
int n,k,l[nmax],r[nmax],s[nmax],i,vl[nmax],cur,x,y,sa,sb,nx[nmax],j;
vector<int>g[nmax],vc,vec;
bitset<nmax>b;
bitset<mmax>f;
int dfs(int x)
{
for(int i=0;i<(int)g[x].size();i++)
{
int y=g[x][i];
if(b[y])continue;
b[y]=1;
if(!vl[y] || dfs(vl[y]))
{
vl[y]=x;
return 1;
}
}
return 0;
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>k;
for(i=1;i<=2*n;i++)
{
cin>>l[i]>>r[i]>>s[i];
r[i]+=n;
g[i].pb(l[i]);
g[i].pb(r[i]);
}
for(i=1;i<=2*n;i++)
{
b.reset();
if(!dfs(i))rc("NO");
}
for(i=1;i<=n;i++)nx[i]=r[vl[i]],sa+=s[vl[i]];
for(i=n+1;i<=2*n;i++)nx[i]=l[vl[i]],sb+=s[vl[i]];
cur=sa-sb;
b.reset();
for(i=1;i<=2*n;i++)
{
if(b[i])continue;
x=i,sa=sb=0,vec.clear();
while(!b[x])
{
vec.pb(x);
b[x]=1;
x=nx[x];
}
for(j=(int)vec.size()-1;j>=0;j--)
{
if(vec[j]<=n)sa+=s[vl[vec[j]]];
else sb+=s[vl[vec[j]]];
if(vec[j]==x)break;
}
if(j==-1)continue;
if(2*(sb-sa)>0)
{
cur+=2*(sb-sa);
vc.pb(2*(sb-sa));
}
else vc.pb(-2*(sb-sa));
}
if(cur<-k)rc("NO");
if(cur<=k)rc("YES");
f[cur+k]=1;
for(i=0;i<(int)vc.size();i++)
{
f|=(f>>vc[i]);
}
for(i=0;i<=2*k;i++)if(f[i])rc("YES");
cout<<"NO"<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2168 KB |
Output is correct |
3 |
Correct |
4 ms |
2168 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
4 ms |
2168 KB |
Output is correct |
6 |
Correct |
4 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
2168 KB |
Output is correct |
8 |
Correct |
4 ms |
2168 KB |
Output is correct |
9 |
Correct |
4 ms |
2168 KB |
Output is correct |
10 |
Correct |
5 ms |
2168 KB |
Output is correct |
11 |
Correct |
4 ms |
2168 KB |
Output is correct |
12 |
Correct |
5 ms |
2168 KB |
Output is correct |
13 |
Correct |
13 ms |
2168 KB |
Output is correct |
14 |
Correct |
4 ms |
2296 KB |
Output is correct |
15 |
Correct |
4 ms |
2168 KB |
Output is correct |
16 |
Correct |
4 ms |
2296 KB |
Output is correct |
17 |
Correct |
4 ms |
2168 KB |
Output is correct |
18 |
Correct |
4 ms |
2168 KB |
Output is correct |
19 |
Correct |
4 ms |
2296 KB |
Output is correct |
20 |
Correct |
3 ms |
1784 KB |
Output is correct |
21 |
Correct |
3 ms |
1784 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
2172 KB |
Output is correct |
25 |
Correct |
4 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2168 KB |
Output is correct |
3 |
Correct |
4 ms |
2168 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
4 ms |
2168 KB |
Output is correct |
6 |
Correct |
4 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
2168 KB |
Output is correct |
8 |
Correct |
4 ms |
2168 KB |
Output is correct |
9 |
Correct |
4 ms |
2168 KB |
Output is correct |
10 |
Correct |
5 ms |
2168 KB |
Output is correct |
11 |
Correct |
4 ms |
2168 KB |
Output is correct |
12 |
Correct |
5 ms |
2168 KB |
Output is correct |
13 |
Correct |
13 ms |
2168 KB |
Output is correct |
14 |
Correct |
4 ms |
2296 KB |
Output is correct |
15 |
Correct |
4 ms |
2168 KB |
Output is correct |
16 |
Correct |
4 ms |
2296 KB |
Output is correct |
17 |
Correct |
4 ms |
2168 KB |
Output is correct |
18 |
Correct |
4 ms |
2168 KB |
Output is correct |
19 |
Correct |
4 ms |
2296 KB |
Output is correct |
20 |
Correct |
3 ms |
1784 KB |
Output is correct |
21 |
Correct |
3 ms |
1784 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
2172 KB |
Output is correct |
25 |
Correct |
4 ms |
2168 KB |
Output is correct |
26 |
Correct |
138 ms |
2528 KB |
Output is correct |
27 |
Correct |
20 ms |
2424 KB |
Output is correct |
28 |
Correct |
135 ms |
2552 KB |
Output is correct |
29 |
Correct |
21 ms |
2552 KB |
Output is correct |
30 |
Correct |
134 ms |
2520 KB |
Output is correct |
31 |
Correct |
21 ms |
2424 KB |
Output is correct |
32 |
Correct |
136 ms |
2552 KB |
Output is correct |
33 |
Correct |
21 ms |
2516 KB |
Output is correct |
34 |
Correct |
14 ms |
2424 KB |
Output is correct |
35 |
Correct |
21 ms |
2552 KB |
Output is correct |
36 |
Correct |
134 ms |
2520 KB |
Output is correct |
37 |
Correct |
21 ms |
2424 KB |
Output is correct |
38 |
Correct |
137 ms |
2520 KB |
Output is correct |
39 |
Correct |
21 ms |
2424 KB |
Output is correct |
40 |
Correct |
136 ms |
2552 KB |
Output is correct |
41 |
Correct |
20 ms |
2552 KB |
Output is correct |
42 |
Correct |
136 ms |
2560 KB |
Output is correct |
43 |
Correct |
21 ms |
2424 KB |
Output is correct |
44 |
Correct |
132 ms |
2552 KB |
Output is correct |
45 |
Correct |
20 ms |
2424 KB |
Output is correct |
46 |
Correct |
137 ms |
2552 KB |
Output is correct |
47 |
Correct |
5 ms |
2040 KB |
Output is correct |
48 |
Correct |
6 ms |
2040 KB |
Output is correct |
49 |
Correct |
6 ms |
2044 KB |
Output is correct |
50 |
Correct |
139 ms |
2552 KB |
Output is correct |
51 |
Correct |
139 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3192 KB |
Output is correct |
2 |
Correct |
13 ms |
2936 KB |
Output is correct |
3 |
Correct |
15 ms |
3064 KB |
Output is correct |
4 |
Correct |
13 ms |
3004 KB |
Output is correct |
5 |
Correct |
16 ms |
3192 KB |
Output is correct |
6 |
Correct |
13 ms |
2936 KB |
Output is correct |
7 |
Correct |
15 ms |
3136 KB |
Output is correct |
8 |
Correct |
13 ms |
2936 KB |
Output is correct |
9 |
Correct |
15 ms |
3156 KB |
Output is correct |
10 |
Correct |
13 ms |
3064 KB |
Output is correct |
11 |
Correct |
15 ms |
3064 KB |
Output is correct |
12 |
Correct |
13 ms |
2936 KB |
Output is correct |
13 |
Correct |
15 ms |
3064 KB |
Output is correct |
14 |
Correct |
16 ms |
3064 KB |
Output is correct |
15 |
Correct |
13 ms |
2936 KB |
Output is correct |
16 |
Correct |
15 ms |
3064 KB |
Output is correct |
17 |
Correct |
13 ms |
3064 KB |
Output is correct |
18 |
Correct |
15 ms |
3192 KB |
Output is correct |
19 |
Correct |
12 ms |
2936 KB |
Output is correct |
20 |
Correct |
15 ms |
3196 KB |
Output is correct |
21 |
Correct |
13 ms |
3064 KB |
Output is correct |
22 |
Correct |
16 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2168 KB |
Output is correct |
3 |
Correct |
4 ms |
2168 KB |
Output is correct |
4 |
Correct |
4 ms |
2296 KB |
Output is correct |
5 |
Correct |
4 ms |
2168 KB |
Output is correct |
6 |
Correct |
4 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
2168 KB |
Output is correct |
8 |
Correct |
4 ms |
2168 KB |
Output is correct |
9 |
Correct |
4 ms |
2168 KB |
Output is correct |
10 |
Correct |
5 ms |
2168 KB |
Output is correct |
11 |
Correct |
4 ms |
2168 KB |
Output is correct |
12 |
Correct |
5 ms |
2168 KB |
Output is correct |
13 |
Correct |
13 ms |
2168 KB |
Output is correct |
14 |
Correct |
4 ms |
2296 KB |
Output is correct |
15 |
Correct |
4 ms |
2168 KB |
Output is correct |
16 |
Correct |
4 ms |
2296 KB |
Output is correct |
17 |
Correct |
4 ms |
2168 KB |
Output is correct |
18 |
Correct |
4 ms |
2168 KB |
Output is correct |
19 |
Correct |
4 ms |
2296 KB |
Output is correct |
20 |
Correct |
3 ms |
1784 KB |
Output is correct |
21 |
Correct |
3 ms |
1784 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
2172 KB |
Output is correct |
25 |
Correct |
4 ms |
2168 KB |
Output is correct |
26 |
Correct |
138 ms |
2528 KB |
Output is correct |
27 |
Correct |
20 ms |
2424 KB |
Output is correct |
28 |
Correct |
135 ms |
2552 KB |
Output is correct |
29 |
Correct |
21 ms |
2552 KB |
Output is correct |
30 |
Correct |
134 ms |
2520 KB |
Output is correct |
31 |
Correct |
21 ms |
2424 KB |
Output is correct |
32 |
Correct |
136 ms |
2552 KB |
Output is correct |
33 |
Correct |
21 ms |
2516 KB |
Output is correct |
34 |
Correct |
14 ms |
2424 KB |
Output is correct |
35 |
Correct |
21 ms |
2552 KB |
Output is correct |
36 |
Correct |
134 ms |
2520 KB |
Output is correct |
37 |
Correct |
21 ms |
2424 KB |
Output is correct |
38 |
Correct |
137 ms |
2520 KB |
Output is correct |
39 |
Correct |
21 ms |
2424 KB |
Output is correct |
40 |
Correct |
136 ms |
2552 KB |
Output is correct |
41 |
Correct |
20 ms |
2552 KB |
Output is correct |
42 |
Correct |
136 ms |
2560 KB |
Output is correct |
43 |
Correct |
21 ms |
2424 KB |
Output is correct |
44 |
Correct |
132 ms |
2552 KB |
Output is correct |
45 |
Correct |
20 ms |
2424 KB |
Output is correct |
46 |
Correct |
137 ms |
2552 KB |
Output is correct |
47 |
Correct |
5 ms |
2040 KB |
Output is correct |
48 |
Correct |
6 ms |
2040 KB |
Output is correct |
49 |
Correct |
6 ms |
2044 KB |
Output is correct |
50 |
Correct |
139 ms |
2552 KB |
Output is correct |
51 |
Correct |
139 ms |
2424 KB |
Output is correct |
52 |
Correct |
15 ms |
3192 KB |
Output is correct |
53 |
Correct |
13 ms |
2936 KB |
Output is correct |
54 |
Correct |
15 ms |
3064 KB |
Output is correct |
55 |
Correct |
13 ms |
3004 KB |
Output is correct |
56 |
Correct |
16 ms |
3192 KB |
Output is correct |
57 |
Correct |
13 ms |
2936 KB |
Output is correct |
58 |
Correct |
15 ms |
3136 KB |
Output is correct |
59 |
Correct |
13 ms |
2936 KB |
Output is correct |
60 |
Correct |
15 ms |
3156 KB |
Output is correct |
61 |
Correct |
13 ms |
3064 KB |
Output is correct |
62 |
Correct |
15 ms |
3064 KB |
Output is correct |
63 |
Correct |
13 ms |
2936 KB |
Output is correct |
64 |
Correct |
15 ms |
3064 KB |
Output is correct |
65 |
Correct |
16 ms |
3064 KB |
Output is correct |
66 |
Correct |
13 ms |
2936 KB |
Output is correct |
67 |
Correct |
15 ms |
3064 KB |
Output is correct |
68 |
Correct |
13 ms |
3064 KB |
Output is correct |
69 |
Correct |
15 ms |
3192 KB |
Output is correct |
70 |
Correct |
12 ms |
2936 KB |
Output is correct |
71 |
Correct |
15 ms |
3196 KB |
Output is correct |
72 |
Correct |
13 ms |
3064 KB |
Output is correct |
73 |
Correct |
16 ms |
3064 KB |
Output is correct |
74 |
Correct |
2000 ms |
6352 KB |
Output is correct |
75 |
Correct |
108 ms |
6136 KB |
Output is correct |
76 |
Correct |
1999 ms |
6424 KB |
Output is correct |
77 |
Correct |
104 ms |
6136 KB |
Output is correct |
78 |
Correct |
2015 ms |
6348 KB |
Output is correct |
79 |
Correct |
1977 ms |
6332 KB |
Output is correct |
80 |
Correct |
109 ms |
6136 KB |
Output is correct |
81 |
Correct |
113 ms |
6136 KB |
Output is correct |
82 |
Correct |
2025 ms |
6392 KB |
Output is correct |
83 |
Correct |
1977 ms |
6348 KB |
Output is correct |
84 |
Correct |
108 ms |
6136 KB |
Output is correct |
85 |
Correct |
1979 ms |
6392 KB |
Output is correct |
86 |
Correct |
107 ms |
6136 KB |
Output is correct |
87 |
Correct |
1968 ms |
6348 KB |
Output is correct |
88 |
Correct |
106 ms |
6136 KB |
Output is correct |
89 |
Correct |
1982 ms |
6344 KB |
Output is correct |
90 |
Correct |
110 ms |
6136 KB |
Output is correct |
91 |
Correct |
2012 ms |
6352 KB |
Output is correct |
92 |
Correct |
108 ms |
6140 KB |
Output is correct |
93 |
Correct |
2004 ms |
6388 KB |
Output is correct |
94 |
Correct |
36 ms |
5368 KB |
Output is correct |
95 |
Correct |
45 ms |
5752 KB |
Output is correct |
96 |
Correct |
45 ms |
5752 KB |
Output is correct |
97 |
Correct |
2016 ms |
6264 KB |
Output is correct |
98 |
Correct |
2048 ms |
6392 KB |
Output is correct |