# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
285089 | Plurm | Bitaro’s Party (JOI18_bitaro) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int THRESH = 160;
vector<int> ord;
vector<int> g[100005];
vector<int> rg[100005];
bool found[100005];
void dfs(int u){
if(found[u]) return;
found[u] = true;
for(int v : g[u]){
dfs(v);
}
ord.push_back(u);
}
vector<pair<int,int> > dp[100005]; // | dp[i] | <= THRESH.
bool __used[100005];
vector<pair<int,int> > newVec;
void __append(pair<int,int> x){
if(newVec.empty() || !__used[x.second]){
newVec.push_back(x);
__used[x.second] = true;
}
}
void merge(vector<pair<int,int> >& dest, vector<pair<int,int> > src){
for(auto& v : src) v.first++;
int ptr1 = 0;
int ptr2 = 0;
while(ptr1 < dest.size() && ptr2 < src.size() && newVec.size() < THRESH){
if(dest[ptr1] > src[ptr2]){
__append(dest[ptr1]);
ptr1++;
}else{
__append(src[ptr2]);
ptr2++;
}
}
while(newVec.size() < THRESH && ptr1 < dest.size()){
__append(dest[ptr1]);
ptr1++;
}
while(newVec.size() < THRESH && ptr2 < src.size()){
__append(src[ptr2]);
ptr2++;
}
swap(dest, newVec);
for(int u : newVec) __used[u] = false;
newVec.clear();
}
bool isBusy[100005];
int subdp[100005];
int recalculateAnswer(int t){
for(int u : ord){
subdp[u] = isBusy[u] ? -10000000 : 0;
for(int v : rg[u]){
subdp[u] = max(subdp[u], subdp[v]+1);
}
}
return subdp[t] < 0 ? -1 : subdp[t];
}
int main(){
int n, m, q;
scanf("%d%d%d",&n,&m,&q);
for(int i = 0; i < m; i++){
int s,e;
scanf("%d%d",&s,&e);
g[s].push_back(e);
rg[e].push_back(s);
}
for(int i = 1; i <= n; i++) dfs(i);
reverse(ord.begin(), ord.end());
for(int u : ord){
for(int v : rg[u]){
merge(dp[u], dp[v]);
}
merge(dp[u], {make_pair(-1, u)});
/*
printf("DBG [%d]\n", u);
for(auto v : dp[u]){
printf("%d %d\n", v.first, v.second);
}
*/
}
int t, y;
for(int qq = 0; qq < q; qq++){
scanf("%d%d",&t,&y);
vector<int> busy;
busy.reserve(y);
for(int i = 0; i < y; i++){
int c;
scanf("%d",&c);
busy.push_back(c);
isBusy[c] = true;
}
int ans = -1;
if(y < THRESH-1){
for(auto p : dp[t]){
if(isBusy[p.second]) continue;
ans = p.first;
break;
}
}else{
ans = recalculateAnswer(t);
}
for(int c : busy) isBusy[c] = false;
printf("%d\n",ans);
}
return 0;
}