# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30932 | Navick | Tug of War (BOI15_tug) | C++14 | 33 ms | 8708 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>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 3e4 + 10, K = 30;
int n, k, deg[2 * N], suml, sumr, sum, p[N];
vector<int> adj[4 * N];
bool mark[4 * N];
set<pii> s;
pii vl[4 * N];
bitset<N * K> dp, foo;
inline int pe(int i){
return 2 * n + i;
}
inline int rg(int i){
return i + n;
}
inline int lf(int i){
return i;
}
void dfs(int v, int c, int h = 0){
mark[v] = true;
if(v>=2*n){
if(h % 4 == 1)
vl[c].F += p[v];
else
vl[c].S += p[v];
}
for(auto u : adj[v]){
if(mark[u])continue;
dfs(u, c, h + 1);
}
}
int main(){
scanf("%d %d", &n, &k);
for(int i=0; i<2*n; i++){
int l, r; scanf("%d %d", &l, &r);
--l; --r;
scanf("%d", &p[pe(i)]); sum += p[pe(i)];
adj[pe(i)].pb(lf(l));
adj[pe(i)].pb(rg(r));
adj[rg(r)].pb(pe(i));
adj[lf(l)].pb(pe(i));
}
for(int i=0; i<2*n; i++){
deg[i] = (int)adj[i].size();
s.insert({deg[i], i});
}
while(!s.empty()){
int v = (*s.begin()).S;
s.erase(s.begin());
if(deg[v] <= 0)
return printf("NO\n"), 0;
if(deg[v] == 1){
mark[v] = true;
for(auto u : adj[v]){
if(mark[u])continue;
mark[u] = true;
for(auto x : adj[u]){
if(mark[x])continue;
s.erase({deg[x], x});
deg[x]--;
s.insert({deg[x], x});
}
if(v < n)
suml += p[u];
else sumr += p[u];
}
}
else break;
}
return printf("YES\n"), 0;
int cnt = 0;
for(int i=0; i<2*n; i++){
if(mark[i])continue;
dfs(i, cnt);
cnt++;
}
dp[suml] = 1;
for(int i=0; i<cnt; i++){
foo = dp;
dp.reset();
dp |= foo << (vl[i].F);
dp |= foo << (vl[i].S);
}
for(int i=(sum-k+1)/2; i<=(sum+k)/2; i++)
if(dp[i])
return printf("YES\n"), 0;
return printf("NO\n"), 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... |