Submission #957119

#TimeUsernameProblemLanguageResultExecution timeMemory
957119GrindMachineCop and Robber (BOI14_coprobber)C++17
100 / 100
354 ms13848 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x, y) ((x + y - 1) / (y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i, n) for(int i = 0; i < n; ++i) #define rep1(i, n) for(int i = 1; i <= n; ++i) #define rev(i, s, e) for(int i = s; i >= e; --i) #define trav(i, a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a, b); } template<typename T> void amax(T &a, T b) { a = max(a, b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* refs: edi key idea: look at the game on a game state graph n*n*2 nodes (cop,robber,turn) dp[cop][robber][turn] = 1 if it's winning for the curr player, 0 if it's losing for the curr player, -1 if the game is never ending for the curr player initially, all values = -1 we know that dp[i][i][0] = 1, dp[i][i][1] = 0 dp[i][j][k] is winning if it has an edge to at least 1 losing state dp[i][j][k] is losing if all its outgoing edges are to winning states also store par[i][j][k] = next node to choose in the winning strategy run multiple iterations until no dp value changes and then stop to optimize, run a bfs from the known values (dp[i][i][k]) and push neighboring states into the queue when their values are decided */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; #include "coprobber.h" int n; int dp[MAX_N][MAX_N][2]; bool a[MAX_N][MAX_N]; vector<int> adj[MAX_N]; int pos; int par[MAX_N][MAX_N][2]; int start(int n_, bool a_[MAX_N][MAX_N]) { n = n_; rep(i,MAX_N){ adj[i].clear(); } rep(i,MAX_N){ rep(j,MAX_N){ a[i][j] = a_[i][j]; if(a[i][j]){ adj[i].pb(j); } } } memset(dp,-1,sizeof dp); memset(par,-1,sizeof par); queue<array<int,3>> q; rep(i,n){ dp[i][i][0] = 1; dp[i][i][1] = 0; q.push({i,i,0}); q.push({i,i,1}); } int win_cnt[n][n][2], lose_cnt[n][n][2]; memset(win_cnt,0,sizeof win_cnt); memset(lose_cnt,0,sizeof lose_cnt); rep(i,n){ rep(j,n){ if(i == j) conts; rep(k,2){ if(k == 0){ win_cnt[i][j][k] = sz(adj[i])+1; } else{ lose_cnt[i][j][k] = sz(adj[j]); } } } } while(!q.empty()){ auto [i,j,k] = q.front(); q.pop(); if(k == 0){ // police wins, sub 1 from lose_cnt[i][v][k^1] if(dp[i][j][k]){ trav(v,adj[j]){ if(dp[i][v][k^1] == -1){ lose_cnt[i][v][k^1]--; if(!lose_cnt[i][v][k^1]){ q.push({i,v,k^1}); dp[i][v][k^1] = 0; } } } } // police loses, add unvis adj robber states else{ trav(v,adj[j]){ if(dp[i][v][k^1] == -1){ q.push({i,v,k^1}); dp[i][v][k^1] = 1; par[i][v][k^1] = j; } } } } else{ // robber loses, add unvis adj police states if(!dp[i][j][k]){ trav(v,adj[i]){ if(dp[v][j][k^1] == -1){ q.push({v,j,k^1}); dp[v][j][k^1] = 1; par[v][j][k^1] = i; } } { int v = i; if(dp[v][j][k^1] == -1){ q.push({v,j,k^1}); dp[v][j][k^1] = 1; par[v][j][k^1] = i; } } } // robber wins, sub 1 from win_cnt[v][j][k^1] else{ trav(v,adj[i]){ if(dp[v][j][k^1] == -1){ win_cnt[v][j][k^1]--; if(!win_cnt[v][j][k^1]){ q.push({v,j,k^1}); dp[v][j][k^1] = 0; } } } { int v = i; if(dp[v][j][k^1] == -1){ win_cnt[v][j][k^1]--; if(!win_cnt[v][j][k^1]){ q.push({v,j,k^1}); dp[v][j][k^1] = 0; } } } } } } // rep(i,n){ // rep(j,n){ // cout << dp[i][j][0] << " " << dp[i][j][1] << endl; // } // cout << endl; // } pos = -1; rep(u,n){ bool ok = true; rep(v,n){ if(dp[u][v][0] != 1){ ok = false; } } if(ok){ pos = u; } } return pos; } int nextMove(int v) { pos = par[pos][v][0]; return pos; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...