This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |