Submission #90477

#TimeUsernameProblemLanguageResultExecution timeMemory
90477radoslav11Bitaro’s Party (JOI18_bitaro)C++14
100 / 100
1451 ms400676 KiB
#include <bits/stdc++.h> #define endl '\n' #define SZ(x) ((int)x.size()) #define pb push_back using namespace std; template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; } template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; } const int MAXN = (int)1e5 + 17 + 42; const int B = 200; int n, m, q; vector<int> adj[MAXN]; void read() { cin >> n >> m >> q; for(int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[v].push_back(u); } } bool ok[MAXN]; int _dp[MAXN]; int Tm_vis[MAXN], cnt_t; int TMS = 0; vector<pair<int, int> > dp[MAXN]; int rec(int u) { if(Tm_vis[u] >= cnt_t) return _dp[u]; Tm_vis[u] = cnt_t; if(ok[u]) _dp[u] = 0; else _dp[u] = -1e9; for(int v: adj[u]) chkmax(_dp[u], rec(v) + 1); return _dp[u]; } int solve_brute(int u, vector<int> blocked) { for(int x: blocked) ok[x] = 0; cnt_t++; int ans = rec(u); for(int x: blocked) ok[x] = 1; return ans < 0 ? -1 : ans; } bool same[MAXN]; bool cmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } int USED[MAXN]; vector<pair<int, int> > merge(vector<pair<int, int> > a, vector<pair<int, int> > b) { for(auto &it: b) it.second++; int po_b = 0; vector<pair<int, int> > ret; for(auto it: a) { while(po_b < SZ(b) && b[po_b].second > it.second) { if(USED[b[po_b].first] != -1) { po_b++; continue; } else { USED[b[po_b].first] = 1; ret.pb(b[po_b]); po_b++; } } if(USED[it.first] != -1) continue; USED[it.first] = 1; ret.pb(it); } while(po_b < SZ(b)) { if(USED[b[po_b].first] != -1) { po_b++; continue; } else { USED[b[po_b].first] = 1; ret.pb(b[po_b]); po_b++; } } for(auto it: ret) USED[it.first] = -1; while(SZ(ret) > B) ret.pop_back(); return ret; } void solve() { memset(USED, -1, sizeof(USED)); for(int i = 1; i <= n; i++) ok[i] = 1; for(int u = 1; u <= n; u++) { dp[u].push_back({u, 0}); for(auto v: adj[u]) dp[u] = merge(dp[u], dp[v]); } while(q--) { int st, sz; vector<int> li; cin >> st >> sz; for(int i = 0; i < sz; i++) { int x; cin >> x; li.push_back(x); } vector<int> initial_li = li; if(li.size() >= B) { cout << solve_brute(st, li) << endl; continue; } for(auto it: li) same[it] = 1; int answer = 0; for(auto it: dp[st]) if(!same[it.first]) chkmax(answer, it.second); for(auto it: li) same[it] = 0; if(answer < 0) answer = -1; if(answer == 0) { for(auto it: initial_li) if(it == st) answer = -1; } cout << answer << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...