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>
#define pb push_back
#define fi first
#define se second
using namespace std;
int n, maxdif, skill[60002];
set<int> v[60002];
pair<int, int> vals[60002];
bool viz[60002];
int dif;
bitset<20 * 60002> dp;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> maxdif;
for(int i = 1; i <= n+n; ++i)
{
int a, b;
cin >> a >> b >> skill[i];
b += n;
v[a].insert(i);
v[b].insert(i);
vals[i] = {a, b};
}
deque<int> d;
for(int i = 1; i <= n+n; ++i)
if(v[i].size() < 2)
d.pb(i), viz[i] = 1;
while(!d.empty())
{
int poz = d[0];
d.pop_front();
if(v[poz].size() == 0)
{
cout << "NO\n";
return 0;
}
int nr = *v[poz].begin();
v[vals[nr].fi].erase(nr);
v[vals[nr].se].erase(nr);
if(v[vals[nr].fi].size() < 2 && !viz[vals[nr].fi])
{
viz[vals[nr].fi] = 1;
d.pb(vals[nr].fi);
}
if(v[vals[nr].se].size() < 2 && !viz[vals[nr].se])
{
viz[vals[nr].se] = 1;
d.pb(vals[nr].se);
}
if(poz <= n)
dif += skill[nr];
else
dif -= skill[nr];
}
vector<int> ruk;
for(int i = 1; i <= n+n; ++i)
{
if(v[i].size() < 2)
continue;
int valas = 0;
int j = i;
while(v[j].size())
{
int val = *v[j].begin();
if(j <= n)
valas += skill[val];
else
valas -= skill[val];
v[vals[val].fi].erase(val);
v[vals[val].se].erase(val);
if(j == vals[val].fi)
j = vals[val].se;
else
j = vals[val].fi;
}
valas = abs(valas);
dif -= valas;
ruk.pb(2 * valas);
}
dif += 20 * n;
dp[dif] = 1;
for(int i = 0; i < ruk.size(); ++i)
dp = dp | (dp << ruk[i]);
bool ok = 0;
for(int i = 20 * n - maxdif; i <= 20 * n + maxdif; ++i)
ok |= dp[i];
if(ok)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
Compilation message (stderr)
tug.cpp: In function 'int main()':
tug.cpp:88:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ruk.size(); ++i)
~~^~~~~~~~~~~~
# | 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... |