Submission #1308721

#TimeUsernameProblemLanguageResultExecution timeMemory
1308721mohammadhadinajafiBitaro’s Party (JOI18_bitaro)C++20
0 / 100
6 ms9784 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pii; const int maxn = 1e5 + 100; vector<int> a[maxn]; int key[maxn]; set<pii> st[maxn]; int addv[maxn]; set<int> quer[maxn]; vector<int> ques[maxn]; int ans[maxn]; int cnt = 1; // new: stored value for node inside its component (the first element of pair in that component's set) int val_stored[maxn]; void dfs(int x){ if(a[x].empty()){ key[x] = cnt; addv[cnt] = 0; st[cnt].insert({0,x}); val_stored[x] = 0; // initialize cnt++; return; } vector<int> pd; int maxi = 0; for(auto u : a[x]){ if(maxi == 0){ maxi = key[u]; continue; } if(st[key[u]].size() > st[maxi].size()){ pd.push_back(maxi); maxi = key[u]; } else pd.push_back(key[u]); } key[x] = maxi; addv[maxi] += 1; // merge other component sets into maxi, with proper updates to keep nodes unique and values maximal for(auto u : pd){ if(u == maxi) continue; if(st[u].empty()) continue; // iterate over a copy because we'll clear st[u] vector<pii> items; items.reserve(st[u].size()); for(auto p : st[u]) items.push_back(p); for(auto p : items){ int stored_in_u = p.first; int node = p.second; // effective value of node in component u: int effective = stored_in_u + addv[u]; if(key[node] == maxi){ // node already exists in maxi: compare effective values and keep the larger one int old_stored = val_stored[node]; int old_effective = old_stored + addv[maxi]; if(effective > old_effective){ // erase old pair and insert new one with updated stored st[maxi].erase({old_stored, node}); int new_stored = effective - addv[maxi]; st[maxi].insert({new_stored, node}); val_stored[node] = new_stored; } // else do nothing (old value is better) } else { // move node into maxi int new_stored = effective - addv[maxi]; st[maxi].insert({new_stored, node}); val_stored[node] = new_stored; key[node] = maxi; } } st[u].clear(); } // insert x itself (distance 0) st[maxi].insert({-addv[maxi], x}); val_stored[x] = -addv[maxi]; key[x] = maxi; for(int i = 0; i < (int)ques[x].size(); i++){ int id = ques[x][i]; ans[id] = -1; for(auto it = st[maxi].rbegin(); it != st[maxi].rend(); ++it){ int node = it->second; if(!quer[id].count(node)){ ans[id] = it->first + addv[maxi]; break; } } } } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,m,q; cin >> n >> m >> q; for(int i = 1; i <= m; i++){ int u,v; cin >> u >> v; a[v].push_back(u); } for(int i = 1; i <= q; i++){ int x,y; cin >> x >> y; ques[x].push_back(i); for(int j = 0; j < y; j++){ int z; cin >> z; quer[i].insert(z); } ans[i] = -1; } for(int i = 1; i <=n; i++) dfs(i); for(int i = 1; i <= q; i++){ if(ans[i]>1) cout << ans[i]-1 << "\n"; else cout << ans[i] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...