#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;
DFS(i.st);
}
}
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 |
3420 KB |
Output is correct |
2 |
Incorrect |
3 ms |
3420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3420 KB |
Output is correct |
2 |
Incorrect |
3 ms |
3420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
725 ms |
4188 KB |
Output is correct |
2 |
Correct |
5 ms |
3928 KB |
Output is correct |
3 |
Correct |
707 ms |
4188 KB |
Output is correct |
4 |
Correct |
6 ms |
3928 KB |
Output is correct |
5 |
Correct |
746 ms |
4300 KB |
Output is correct |
6 |
Correct |
7 ms |
3932 KB |
Output is correct |
7 |
Correct |
697 ms |
4304 KB |
Output is correct |
8 |
Correct |
5 ms |
3928 KB |
Output is correct |
9 |
Correct |
704 ms |
4188 KB |
Output is correct |
10 |
Correct |
6 ms |
3932 KB |
Output is correct |
11 |
Correct |
686 ms |
4084 KB |
Output is correct |
12 |
Correct |
5 ms |
3932 KB |
Output is correct |
13 |
Correct |
684 ms |
4188 KB |
Output is correct |
14 |
Correct |
698 ms |
4184 KB |
Output is correct |
15 |
Correct |
5 ms |
3928 KB |
Output is correct |
16 |
Correct |
685 ms |
4188 KB |
Output is correct |
17 |
Correct |
5 ms |
3928 KB |
Output is correct |
18 |
Correct |
684 ms |
4296 KB |
Output is correct |
19 |
Correct |
6 ms |
3928 KB |
Output is correct |
20 |
Correct |
698 ms |
4300 KB |
Output is correct |
21 |
Correct |
6 ms |
3676 KB |
Output is correct |
22 |
Correct |
341 ms |
4384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3420 KB |
Output is correct |
2 |
Incorrect |
3 ms |
3420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |