# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
351990 | minoum | Tug of War (BOI15_tug) | C++17 | 1364 ms | 8812 KiB |
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 int ll;
const int MAXN = 6e4;
int n, k, s[MAXN], l[MAXN], r[MAXN], ss = 0, s1 = 0, dd[MAXN], ww[2];
vector <int> adj[MAXN]; //id,u
bool deadh[MAXN], deadt[MAXN];
inline bool ch(){
set <pair<int,int>> deg;
for(int i = 0; i < 2*n; i++){
dd[i] = (int)adj[i].size();
deg.insert({dd[i], i});
}
while(!deg.empty()){
if(deg.begin()->first > 1) break;
if(deg.begin()->first <= 0) return false;
int t = deg.begin()->second, h = -1, v2; deg.erase(*deg.begin());
for(auto i: adj[t])
if(!deadh[i]){
h = i; v2 = ((t<n)?r[h]:l[h]); break;
}
s1 += (t<n?s[h]:0);
deadt[t] = true; deadh[h] = true;
v2 = (t<n?r[h]:l[h]);
if(!deadt[v2]){
deg.erase({dd[v2],v2});
dd[v2]--;
deg.insert({dd[v2], v2});
}
}
return true;
}
inline void dfs(int v){
int hh = 0, v2;
while(!deadt[v]){
for(auto i: adj[v])
if(!deadh[i]){
int h = i;
ww[hh] += s[h]; deadh[h] = true; deadt[v] = true;
v2 = ((v<n)?r[h]:l[h]);
break;
}
hh = 1-hh; v = v2;
}
return;
}
int main()
{
// ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// cin >> n >> k;
scanf("%d",&n); scanf("%d", &k);
for(int i = 0; i < 2*n; i++){
//cin >> l[i] >> r[i] >> s[i];
scanf("%d", l+i); scanf("%d", r+i); scanf("%d", s+i);
l[i]--; r[i]--; r[i] += n; ss += s[i];
adj[l[i]].push_back(i);
adj[r[i]].push_back(i);
}
// int b = (ss-k+1)/2, e = (ss+k)/2; b = max(b, 0);
bool check = ch();
if(!check){
// cout << "NO" << '\n';
printf("NO");
return 0;
}
if(ss-(2*s1) <= k && -ss+(2*s1) <= k){
//cout << "YES" << '\n';
printf("YES");
return 0;
}
vector <pair<int,int>> w;
for(int i = 0; i < 2*n; i++)
if(!deadt[i]){
ww[0] = ww[1] = 0;
dfs(i);
w.push_back({ww[0],ww[1]});
}
bitset <MAXN*10> bt;
bt[s1] = 1;
for(auto i: w)
bt = (bt<<i.first) | (bt<<i.second);
for(int i = 0; i < ss; i++)
if(bt[i] && ss-(2*i) <= k && -ss+(2*i) <= k){
//cout << "YES" << '\n';
printf("YES");
return 0;
}
// cout << "NO" << '\n';
printf("NO");
return 0;
}
Compilation message (stderr)
# | 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... |