Submission #1096147

#TimeUsernameProblemLanguageResultExecution timeMemory
1096147Zero_OPBitaro’s Party (JOI18_bitaro)C++14
14 / 100
602 ms350876 KiB
#include "bits/stdc++.h" using namespace std; #define sz(v) (int)v.size() const int MAX = 1e5 + 5; const int SQRT = 350; int n, m, q, dp[MAX], all_dp[MAX]; vector<int> adj[MAX], rev[MAX]; bool bad[MAX], vis[MAX]; vector<int> bestNodes[MAX]; //only take B nodes vector<int> S[MAX], tmpS; bool better(int u, int v){ return dp[u] > dp[v]; } void insert(vector<int>& A, vector<int>& B){ //define A is larger set vector<int>().swap(tmpS); int i = 0, j = 0; while(i < sz(A) || j < sz(B)){ if(sz(tmpS) == SQRT) break; if(i == sz(A)) tmpS.push_back(B[j++]); else if(j == sz(B)) tmpS.push_back(A[i++]); else if(better(A[i], B[j])) tmpS.push_back(A[i++]); else tmpS.push_back(B[j++]); } swap(tmpS, A); } int dfsDP(int u){ if(vis[u]) return dp[u]; vis[u] = true; for(int v : adj[u]){ dp[u] = max(dp[u], dfsDP(v) + 1); } return dp[u]; } queue<int> qu; void bfsAdj(int s){ vis[s] = true; qu.push(s); while(!qu.empty()){ int u = qu.front(); qu.pop(); bestNodes[u] = S[u]; for(int v : adj[u]){ insert(S[v], S[u]); if(!vis[v]){ insert(S[v], S[u]); vis[v] = true; qu.push(v); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; for(int i = 0; i < m; ++i){ int u, v; cin >> u >> v; adj[u].push_back(v); rev[v].push_back(u); } for(int i = n; i >= 1; --i){ if(!vis[i]){ dfsDP(i); } } for(int i = 1; i <= n; ++i){ S[i].push_back(i); } fill(vis + 1, vis + 1 + n, false); for(int i = 1; i <= n; ++i){ if(!vis[i]){ bfsAdj(i); } } while(q--){ int t, y; cin >> t >> y; vector<int> nodes(y); for(int i = 0; i < y; ++i) cin >> nodes[i]; for(int u : nodes) bad[u] = true; if(y > SQRT){ fill(dp + 1, dp + 1 + n, -1e9); dp[t] = 0; for(int i = t; i > 0; --i){ for(int j : rev[i]){ dp[j] = max(dp[j], dp[i] + 1); } } int ans = -1; for(int i = t; i > 0; --i){ if(!bad[i]) ans = max(ans, dp[i]); } cout << ans << '\n'; } else{ int ans = -1; for(int i = 0; i < sz(bestNodes[t]); ++i){ if(!bad[bestNodes[t][i]]){ ans = dp[bestNodes[t][i]]; break; } } if(ans == -1){ cout << ans << '\n'; } else{ ans -= dp[t]; cout << ans << '\n'; } } for(int u : nodes) bad[u] = false; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...