Submission #1005776

#TimeUsernameProblemLanguageResultExecution timeMemory
1005776TrinhKhanhDungTug of War (BOI15_tug)C++14
100 / 100
1717 ms12928 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1e9 + 7) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} const int MAX = 1e5 + 10; int N, K; set<pair<int, int>> adj[MAX]; int s[MAX], L, R; bool vis[MAX]; void dfs(int u, int p, vector<int> &val){ // cout << u << ' '; vis[u] = true; for(auto it: adj[u]){ int v = it.fi; int cost = s[it.se]; if(it.se != p) val.push_back(cost); if(vis[v]) continue; dfs(v, it.se, val); } } void solve(){ //input cin >> N >> K; for(int i=1; i<=N*2; i++){ int l, r; cin >> l >> r >> s[i]; adj[l].insert(make_pair(r + N, i)); adj[r + N].insert(make_pair(l, i)); } queue<int> q; for(int i=1; i<=N*2; i++){ if(sz(adj[i]) == 0){ cout << "NO\n"; return; } if(sz(adj[i]) == 1){ q.push(i); } } while(!q.empty()){ int u = q.front(); // cout << u << ' '; q.pop(); if(sz(adj[u]) == 0) continue; auto it = adj[u].begin(); int v = (*it).fi, cost = s[(*it).se]; if(u <= N){ L += cost; } else{ R += cost; } adj[u].clear(); adj[v].erase(make_pair(u, (*it).se)); if(sz(adj[v]) == 1){ q.push(v); } } bitset<1200003> bs; bs[0] = true; int S = 0; for(int i=1; i<=N*2; i++){ // cout << i << ' ' << sz(adj[i]) << '\n'; if(sz(adj[i]) > 2){ cout << "NO\n"; return; } if(sz(adj[i]) == 2 && !vis[i]){ vector<int> val; dfs(i, 0, val); int c1 = 0, c2 = 0; for(int i=0; i+1<sz(val); i++){ if(i & 1) c2 += val[i]; else c1 += val[i]; } bs = (bs << c1) | (bs << c2); S += c1 + c2; } } for(int i=0; i<1200001; i++){ if(bs[i]){ int j = S - i; if(abs(L + i - (R + j)) <= K){ cout << "YES\n"; return; } } } cout << "NO\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("netw.inp", "r", stdin); // freopen("netw.out", "w", stdout); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...