#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=3e4+7;
vector<int>P;
bitset<LIM*80>B;
vector<pair<int,int>>V[2*LIM];
int F[2*LIM], ile[2*LIM], usun[2*LIM], odw[2*LIM], n, sum, akt;
int fnd(int x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
void uni(int a, int b) {
a=fnd(a); b=fnd(b);
if(a!=b) {
ile[a]+=ile[b];
F[b]=a;
}
--ile[a];
}
void DFS(int x) {
odw[x]=1;
for(auto i : V[x]) if(!odw[i.st] && !usun[i.st]) {
if(x<n) akt+=i.nd; else akt-=i.nd;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int k;
cin >> n >> k;
rep(i, 2*n) {
F[i]=i;
ile[i]=1;
}
rep(i, 2*n) {
int a, b, c;
cin >> a >> b >> c; --a; --b; b+=n;
V[a].pb({b, c});
V[b].pb({a, c});
uni(a, b);
}
rep(i, 2*n) if(ile[fnd(i)]<0) {
cout << "NO\n";
return 0;
}
rep(i, 2*n) ile[i]=V[i].size();
queue<int>q;
rep(i, 2*n) if(ile[i]==1) q.push(i);
while(!q.empty()) {
int p=q.front(); q.pop();
usun[p]=1;
for(auto i : V[p]) if(!usun[i.st]) {
--ile[i.st];
if(ile[i.st]==1) q.push(i.st);
if(p<n) sum+=i.nd; else sum-=i.nd;
}
}
rep(i, 2*n) if(!usun[i] && !odw[i]) {
akt=0;
DFS(i);
P.pb(abs(akt));
}
sum=abs(sum);
int S=0;
B[0]=1;
for(auto i : P) {
S+=i;
B|=B<<2*i;
}
rep(i, 80*LIM) if(B[i] && i>=S) {
int a=sum+i-S, b=2*S-i;
if(abs(a-b)<=k) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3416 KB |
Output is correct |
2 |
Correct |
3 ms |
3420 KB |
Output is correct |
3 |
Correct |
4 ms |
3420 KB |
Output is correct |
4 |
Correct |
3 ms |
3420 KB |
Output is correct |
5 |
Correct |
4 ms |
3420 KB |
Output is correct |
6 |
Correct |
8 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
3672 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
4 ms |
3488 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Incorrect |
4 ms |
3420 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3416 KB |
Output is correct |
2 |
Correct |
3 ms |
3420 KB |
Output is correct |
3 |
Correct |
4 ms |
3420 KB |
Output is correct |
4 |
Correct |
3 ms |
3420 KB |
Output is correct |
5 |
Correct |
4 ms |
3420 KB |
Output is correct |
6 |
Correct |
8 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
3672 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
4 ms |
3488 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Incorrect |
4 ms |
3420 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1210 ms |
4564 KB |
Output is correct |
2 |
Correct |
5 ms |
4004 KB |
Output is correct |
3 |
Correct |
1280 ms |
4568 KB |
Output is correct |
4 |
Correct |
6 ms |
4192 KB |
Output is correct |
5 |
Correct |
1276 ms |
4568 KB |
Output is correct |
6 |
Correct |
7 ms |
4184 KB |
Output is correct |
7 |
Correct |
1271 ms |
4556 KB |
Output is correct |
8 |
Correct |
6 ms |
4184 KB |
Output is correct |
9 |
Correct |
1232 ms |
4568 KB |
Output is correct |
10 |
Correct |
6 ms |
4188 KB |
Output is correct |
11 |
Correct |
1282 ms |
4444 KB |
Output is correct |
12 |
Correct |
6 ms |
4184 KB |
Output is correct |
13 |
Correct |
1249 ms |
4568 KB |
Output is correct |
14 |
Correct |
1217 ms |
4564 KB |
Output is correct |
15 |
Correct |
6 ms |
4188 KB |
Output is correct |
16 |
Correct |
1326 ms |
4564 KB |
Output is correct |
17 |
Correct |
10 ms |
4188 KB |
Output is correct |
18 |
Correct |
1279 ms |
4560 KB |
Output is correct |
19 |
Correct |
8 ms |
4188 KB |
Output is correct |
20 |
Correct |
1241 ms |
4496 KB |
Output is correct |
21 |
Correct |
8 ms |
3928 KB |
Output is correct |
22 |
Correct |
614 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3416 KB |
Output is correct |
2 |
Correct |
3 ms |
3420 KB |
Output is correct |
3 |
Correct |
4 ms |
3420 KB |
Output is correct |
4 |
Correct |
3 ms |
3420 KB |
Output is correct |
5 |
Correct |
4 ms |
3420 KB |
Output is correct |
6 |
Correct |
8 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
3672 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
4 ms |
3488 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Incorrect |
4 ms |
3420 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |