Submission #852752

#TimeUsernameProblemLanguageResultExecution timeMemory
852752gun_ganBitaro’s Party (JOI18_bitaro)C++17
0 / 100
12 ms10072 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MX = 1e5 + 5, B = 330; int N, M, Q; vector<int> g[MX], r[MX]; int dist[MX], deg[MX]; bool vis[MX]; vector<pair<int,int>> vec[MX]; void merge(int u, int v) { // merge u to v for(auto [y, x] : vec[u]) vec[v].push_back({y + 1, x}); sort(vec[v].rbegin(), vec[v].rend()); vec[v].resize(unique(vec[v].begin(), vec[v].end()) - vec[v].begin()); while(vec[v].size() > B) vec[v].pop_back(); } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> N >> M >> Q; for(int i = 0; i < M; i++) { int u, v; cin >> u >> v; g[u].push_back(v); deg[v]++; r[v].push_back(u); } queue<int> q; for(int i = 1; i <= N; i++) { if(!deg[i]) q.push(i); } while(!q.empty()) { auto v = q.front(); q.pop(); if(vec[v].size() < B) vec[v].push_back({0, v}); for(auto u : g[v]) { merge(v, u); deg[u]--; if(!deg[u]) q.push(u); } } for(int j = 0; j < Q; j++) { int tj, yj; cin >> tj >> yj; if(yj < B) { int res = -1; vector<int> v; for(int i = 0; i < yj; i++) { int x; cin >> x; v.push_back(x); vis[x] = 1; } for(auto [y, x] : vec[tj]) { // cout << y << " " << x << '\n'; if(!vis[x]) res = max(res, y); } cout << res << '\n'; for(auto x : v) vis[x] = 0; } else { for(int i = 1; i <= N; i++) { deg[i] = 0; dist[i] = -1; } for(int i = 1; i <= N; i++) { for(auto k : r[i]) deg[k]++; } for(int i = 0; i < yj; i++) { int x; cin >> x; vis[x] = 1; } queue<int> q; if(!vis[tj]) { q.push(tj); dist[tj] = 0; } int res = -1; while(!q.empty()) { auto v = q.front(); q.pop(); if(!vis[v]) res = max(res, dist[v]); for(auto u : r[v]) { deg[u]--; dist[u] = max(dist[u], dist[v] + 1); if(!deg[u]) q.push(u); } } cout << res << '\n'; memset(vis, 0, sizeof vis); } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...