Submission #958630

#TimeUsernameProblemLanguageResultExecution timeMemory
958630steveonalexBitaro’s Party (JOI18_bitaro)C++17
100 / 100
712 ms171328 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); // mt19937_64 rng(69); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int N = 1e5 + 69, INF = 1e9 + 69; const int BLOCK = 200; int n, m, q; vector<int> graph[N]; namespace Solve1{ int dp[N]; void solve(int t, vector<int> banned){ for(int i = 1; i<=t; ++i) dp[i] = 0; for(int i: banned) dp[i] = -INF; for(int i = 1; i<=t; ++i){ for(int j: graph[i]) maximize(dp[i], dp[j] + 1); } cout << max(-1, dp[t]) << "\n"; } } namespace Solve2{ vector<pair<int, int>> dp[N]; int max_val[N]; void init(){ for(int i = 1; i<=n; ++i) { dp[i].reserve(BLOCK); max_val[i] = -INF; } vector<pair<int, int>> tmp; vector<int> st; st.reserve(n * BLOCK); tmp.reserve(n); for(int i = 1; i<=n; ++i) { for(int j: graph[i]){ for(pair<int, int> k: dp[j]){ if (maximize(max_val[k.second], k.first + 1)) st.push_back(k.second); } } for(int j: st) if (max_val[j] != -INF){ tmp.push_back({max_val[j], j}); max_val[j] = -INF; } st.clear(); tmp.push_back({0, i}); if (tmp.size() > BLOCK){ nth_element(tmp.begin(), tmp.begin() + BLOCK, tmp.end(), greater<pair<int, int>>()); while(tmp.size() > BLOCK) tmp.pop_back(); } dp[i] = tmp; tmp.clear(); } } void solve(int t, vector<int> banned){ for(auto i: dp[t]) max_val[i.second] = i.first; for(int i: banned) max_val[i] = -INF; int ans = -INF; for(auto i: dp[t]) { maximize(ans, max_val[i.second]); max_val[i.second] = -INF; } cout << max(-1, ans) << "\n"; } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; while(m--){ int u, v; cin >> u >> v; if (u > v) swap(u, v); graph[v].push_back(u); } Solve2::init(); while(q--){ int t, k; cin >> t >> k; vector<int> banned(k); for(int &i: banned) cin >> i; if (k >= BLOCK) Solve1::solve(t, banned); else Solve2::solve(t, banned); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...