Submission #1222711

#TimeUsernameProblemLanguageResultExecution timeMemory
1222711ericl23302Bitaro’s Party (JOI18_bitaro)C++20
100 / 100
950 ms410628 KiB
#include <bits/stdc++.h> using namespace std; const int K = 300; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<int>> adjacency(n); vector<vector<pair<int, int>>> preComp(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; adjacency[b].push_back(a); } vector<bool> included(n, false); vector<int> modified; for (int i = 0; i < n; ++i) { if (adjacency[i].empty()) { preComp[i].push_back({0, i}); continue; } vector<int> cnts(adjacency[i].size(), 0); while (preComp[i].size() <= K) { int cur = -1; for (int j = 0; j < adjacency[i].size(); ++j) { while (cnts[j] < preComp[adjacency[i][j]].size() && included[preComp[adjacency[i][j]][cnts[j]].second]) ++cnts[j]; if (cnts[j] >= preComp[adjacency[i][j]].size()) continue; if (cur == -1 || preComp[adjacency[i][j]][cnts[j]].first > preComp[adjacency[i][cur]][cnts[cur]].first) cur = j; } if (cur == -1) break; preComp[i].emplace_back(preComp[adjacency[i][cur]][cnts[cur]].first + 1, preComp[adjacency[i][cur]][cnts[cur]].second); included[preComp[adjacency[i][cur]][cnts[cur]].second] = true; modified.push_back(preComp[adjacency[i][cur]][cnts[cur]].second); ++cnts[cur]; } while (!modified.empty()) { included[modified.back()] = false; modified.pop_back(); } preComp[i].push_back({0, i}); } while (q--) { int t, y; cin >> t >> y; --t; for (int i = 0; i < y; ++i) { int city; cin >> city; --city; included[city] = true; modified.push_back(city); } if (y <= K || preComp[t].size() <= K) { bool done = false; for (auto &[distance, city] : preComp[t]) { if (!included[city]) { cout << distance << '\n'; done = true; break; } } if (!done) cout << -1 << '\n'; } else { vector<int> dp(t + 1, -1); dp[t] = 0; for (int i = t; i >= 0; --i) { if (dp[i] == -1) continue; for (int &a : adjacency[i]) dp[a] = max(dp[a], dp[i] + 1); } int res = -1; for (int i = 0; i <= t; ++i) { if (included[i]) continue; res = max(res, dp[i]); } cout << res << '\n'; } while (!modified.empty()) { included[modified.back()] = false; modified.pop_back(); } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...