Submission #887615

#TimeUsernameProblemLanguageResultExecution timeMemory
887615Shayan86Tug of War (BOI15_tug)C++17
18 / 100
358 ms4912 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; #define Mp make_pair #define sep ' ' #define endl '\n' #define F first #define S second #define pb push_back #define all(x) (x).begin(),(x).end() #define kill(res) cout << res << '\n', exit(0); #define set_dec(x) cout << fixed << setprecision(x); #define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll N = 6e4 + 50; const ll Mod = 1e9 + 7; ll n, k, s[N], par[N], h[N], f[N], sum, cnt; pair<pii, int> edge; bool mark[N]; vector<ll> vec; vector<pii> adj[N]; void dfs(int v){ mark[v] = 1; for(auto [u, w]: adj[v]){ if(u == par[v]) continue; else if(mark[u]){ edge = Mp(Mp(v, u), w); continue; } par[u] = v; h[u] = h[v] + 1; f[u] = w - f[v]; dfs(u); if(h[v] % 2) sum += w; else sum -= w; } } const ll M = 10 * N; bitset<2*M> diff; void check(int v){ mark[v] = 1; for(auto [u, w]: adj[v]){ if(!mark[u]) check(u); } cnt++; sum += adj[v].size(); } int main(){ fast_io; cin >> n >> k; int l, r; for(int i = 1; i <= 2*n; i++){ cin >> l >> r >> s[i]; r += n; adj[l].pb(Mp(r, s[i])); adj[r].pb(Mp(l, s[i])); } for(int i = 1; i <= 2*n; i++){ if(!mark[i]){ sum = 0; cnt = 0; check(i); if(sum != cnt * 2) kill("NO"); } } fill(mark, mark + N, 0); diff[M] = 1; for(int i = 1; i <= 2*n; i++){ if(!mark[i]){ sum = 0; dfs(i); int v = edge.F.F; int u = edge.F.S; int w = edge.S; if(h[v] % 2) sum -= w; else sum += w; ll sum2 = sum; // f[u] + f[v] - w if(h[v] % 2) sum2 -= (f[u] + f[v] - w); else sum2 += (f[u] + f[v] - w); if(sum2 >= 0) diff = diff << sum2; else diff = diff >> (-sum2); vec.pb(abs(sum - sum2)); //cout << sum2 << sep << sum - sum2 << endl; } } for(auto x: vec){ diff = (diff >> x) | (diff << x); } ll res = Mod; for(int i = 0; i < 2*M; i++) if(diff[i]) res = min(res, abs(M-i)); //cout << res << endl; if(res <= k) cout << "YES"; else cout << "NO"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...