이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//JAVAIS BESOIN DU HINT :(
#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define last(a) a[sz(a)-1]
using namespace std;
vector<int> memo;
vector<bool> visited;
vector<vector<int>> neigh;
vector<vector<pair<int,int>>> minsqrt;
unordered_map<int,int> mp;
int timer=0;
int SQRT=0;
int mnDP2=0;
int dpinit(int x){
if(memo[x]>=0) return memo[x];
int mx=0;
for (auto t : neigh[x]) {
mx=max(mx,dpinit(t)+1);
}
memo[x]=mx;
return mx;
}
void dp(int x){
if(visited[x]) return;
visited[x]=true;
unordered_map<int,bool> mp2;
for (auto t : neigh[x]) {
vector<pair<int,int>> nw;
dp(t);
int i=0;
for (auto u : minsqrt[t])
{
if(mp2[u.second]==true) continue;
if(sz(nw)==SQRT) break;
while(i<SQRT&&minsqrt[x][i].first<=u.first) {
nw.push_back(minsqrt[x][i]);
if(minsqrt[x][i].second>=0) mp2[minsqrt[x][i].second]=true;
if(sz(nw)==SQRT) break;
i++;
}
if(sz(nw)==SQRT) break;
if(u.second>=0) mp2[u.second]=true;
nw.push_back(u);
}
while(sz(nw)<SQRT) {
nw.push_back(minsqrt[x][i]);
if(sz(nw)==SQRT) break;
i++;
}
for (i = 0; i < SQRT; i++) minsqrt[x][i]={nw[i].first,nw[i].second};
}
vector<pair<int,int>> nw;
bool b=false;
for (int i = 0; i < SQRT; i++){
if(!b&&memo[x]<minsqrt[x][i].first) {
nw.push_back({memo[x],x});
b=true;
}
if(sz(nw)==SQRT) break;
nw.push_back(minsqrt[x][i]);
if(sz(nw)==SQRT) break;
}
for (int i = 0; i < SQRT; i++) minsqrt[x][i]={nw[i].first,nw[i].second};
return;
}
void dp2(int x){
if(!mp[x]) mnDP2=min(mnDP2,memo[x]);
for (auto t : neigh[x]) dp2(t);
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int N,M,Q; cin >> N >> M >> Q;
memo.resize(N,-1);
neigh.resize(N);
visited.resize(N,false);
minsqrt.resize(N);
SQRT=sqrt(N);
for (int i = 0; i < N; i++)
{
minsqrt[i].resize(SQRT,{1e12,-1e12});
}
vector<bool> leaf(N,true);
for (int i = 0; i < M; i++)
{
int a,b; cin >> a >> b;
a--; b--;
neigh[b].push_back(a);
leaf[a]=false;
}
for (int i = 0; i < N; i++)
{
if(leaf[i]) dpinit(i);
}
for (int i = 0; i < N; i++)
{
if(leaf[i]) dp(i);
}
while (Q--)
{
int T,Y; cin >> T >> Y;
T--;
vector<int> y(Y);
mp.clear();
for (int i = 0; i < Y; i++)
{
cin >> y[i];
y[i]--;
mp[y[i]]=true;
}
if(true){
mnDP2=1e12;
dp2(T);
int d=memo[T];
cout << max(-1LL,d-mnDP2) << "\n";
}else{
visited.clear();
visited.resize(N,false);
mnDP2=1e12;
for (int i = 0; i < SQRT; i++)
{
if(minsqrt[T][i].second<0||mp[minsqrt[T][i].second]) continue;
mnDP2=min(minsqrt[T][i].first,mnDP2);
}
int d=memo[T];
cout << max(-1LL,d-mnDP2) << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |