Submission #400385

#TimeUsernameProblemLanguageResultExecution timeMemory
400385nikatamlianiBitaro’s Party (JOI18_bitaro)C++14
100 / 100
1553 ms109208 KiB
#include <bits/stdc++.h> using namespace std; void maxi(int &x, int y) { if(x < y) x = y; } const int C = 100; void merge(vector<pair<int, int>> &a, const vector<pair<int, int>> &b) { vector<pair<int, int>> c; int n = (int)a.size(), m = (int)b.size(); for(int i = 0, j = 0; i < n || j < m;) { if(i+j >= C) break; if(j == m || (i < n && a[i].first > b[j].first)) { c.push_back(a[i++]); } else { c.push_back(b[j++]); } } while((int)c.size() > C) c.pop_back(); a = c; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<int>> g(n+1); vector<vector<int>> r(n+1); for(int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); r[v].push_back(u); } vector<int> dst(n+1); vector<vector<pair<int, int>>> list(n+1); for(int i = 1; i <= n; ++i) { for(int x : r[i]) { for(auto p : list[x]) maxi(dst[p.second], p.first); } for(int x : r[i]) { vector<pair<int, int>> v; for(auto p : list[x]) { if(dst[p.second] == p.first) { v.push_back({p.first+1, p.second}); dst[p.second] = -1; } } merge(list[i], v); } while((int)list[i].size() < C) list[i].push_back({0, i}); for(int x : r[i]) { for(auto p : list[x]) dst[p.second] = 0; } } vector<bool> blocked(n+1); vector<int> dp(n+1); while(q--) { int t, x; cin >> t >> x; vector<int> v(x); for(int i = 0; i < x; ++i) { cin >> v[i]; blocked[v[i]] = 1; } int ans = -1; if(x < C) { for(pair<int, int> p : list[t]) { if(!blocked[p.second]) { ans = p.first; break; } } } else { for(int i = 1; i <= t; ++i) { dp[i] = -1e6; if(!blocked[i]) { dp[i] = 0; } for(int x : r[i]) { maxi(dp[i], dp[x]+1); } } ans = dp[t]; } if(ans < 0) ans = -1; cout << ans << '\n'; for(int i : v) blocked[i] = 0; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...