Submission #252910

#TimeUsernameProblemLanguageResultExecution timeMemory
252910BlagojceTug of War (BOI15_tug)C++11
41 / 100
3070 ms59648 KiB
#include <bits/stdc++.h> #define fr(i, n, m) for(int i = (n); i < (m); i ++) #define pb push_back #define st first #define nd second #define pq priority_queue #define all(x) begin(x), end(x) #include <time.h> #include <cmath> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; const int i_inf = 1e9; const ll inf = 1e18; const ll mod = 1000000007; const ld eps = 1e-13; const ld pi = 3.14159265359; mt19937 _rand(time(NULL)); clock_t timer = clock(); const int mxn = 5e5; int l[mxn], r[mxn], s[mxn]; int n, k; set <int> g[mxn]; int used[mxn]; int vis[mxn]; int tempa, tempb; int trans(int u, int ed){ if(u == l[ed]){ tempb += s[ed]; return r[ed]; } else{ tempa += s[ed]; return l[ed]; } } bool OK; void dfs(int u, int flag){ for(auto e : g[u]){ if(used[e] == flag) continue; used[e] = flag; int nx = trans(u, e); if(vis[nx] == flag){ OK = false; } vis[nx] = flag; dfs(nx, flag); } } bool dp[6000][6000]; int suma, sumb; int deg[mxn]; void pruning(){ queue<int> Q; fr(i, 0, n){ if(deg[i] == 1) Q.push(i); } while(!Q.empty()){ int c = Q.front(); Q.pop(); int ed = *g[c].begin(); int nx = trans(c, ed); g[c].erase(ed); g[nx].erase(ed); --deg[c]; --deg[nx]; used[ed] = 2; if(deg[nx] == 1) Q.push(nx); if(c == l[ed]){ suma += s[ed]; } else{ sumb += s[ed]; } } } void solve(){ cin >> n>> k; n *= 2; bool sub3 = true; fr(i, 0, n){ cin >> l[i] >> r[i] >> s[i]; r[i] += n/2; --l[i], --r[i]; if(s[i] != 1) sub3 = false; deg[l[i]]++; deg[r[i]]++; g[l[i]].insert(i); g[r[i]].insert(i); } pruning(); vector<pii> diff; fr(i, 0, n){ if(used[i] == 2) continue; vis[l[i]] = 1; used[i] = 1; OK = true; tempa = s[i], tempb = 0; dfs(l[i], 1); bool ok1 = OK; int val1 = tempa-tempb; vis[r[i]] = 2; used[i] = 2; OK = true; tempa = 0, tempb = s[i]; dfs(r[i], 2); bool ok2 = OK; int val2 = tempa-tempb; diff.pb({val1, val2}); if(!ok1 && !ok2){ cout<<"NO"<<endl; return; } } if(sub3){ cout<<"YES"<<endl; return; } int base = 50*n; int len = diff.size(); memset(dp, false, sizeof(dp)); dp[0][base + suma - sumb] = true; fr(i, 1, len+1){ fr(pr, 0, 100*n){ if(!dp[i-1][pr]) continue; dp[i][pr+diff[i-1].st] = true; dp[i][pr+diff[i-1].nd] = true; } } fr(i, 0, k+1){ if(dp[len][base+i]||dp[len][base-i]){ cout<<"YES"<<endl; return; } } cout<<"NO"<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...