#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2,popcnt,sse4")
#pragma GCC optimize("O3,unroll-loops")
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int B = 200;
const int mxn = 1e5+10;
vector<int> paths[mxn];
int N,M,Q;
namespace SMALL{
vector<pii> dp[mxn];
bitset<mxn> done;
void trim(int now){
vector<int> all;
vector<pii> v;
sort(dp[now].rbegin(),dp[now].rend());
for(auto &i:dp[now]){
if(v.size()>=B)break;
if(done[i.sc])continue;
v.push_back(i);
all.push_back(i.sc);
done[i.sc] = true;
}
for(auto &i:all)done[i] = false;
dp[now].swap(v);
return;
}
void PREC(){
for(int i = 1;i<=N;i++)dp[i].push_back(pii(0,i));
for(int i = 1;i<=N;i++){
for(auto nxt:paths[i]){
for(auto &j:dp[i])dp[nxt].push_back(pii(j.fs+1,j.sc));
trim(nxt);
}
}
return;
}
void GETANS(int tar,vector<int> ban){
for(auto &i:ban)done[i] = true;
int ans = -1;
for(auto &j:dp[tar]){
if(done[j.sc])continue;
ans = j.fs;
break;
}
for(auto &i:ban)done[i] = false;
cout<<ans<<'\n';
return;
return;
}
}
namespace BIG{
int dist[mxn];
void GETANS(int tar,vector<int> ban){
memset(dist,0,sizeof(dist));
for(auto &i:ban)dist[i] = -mxn*2;
for(int i = 1;i<=N;i++){
for(auto nxt:paths[i]){
dist[nxt] = max(dist[nxt],dist[i]+1);
}
}
cout<<(dist[tar]<0?-1:dist[tar])<<'\n';
return;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M>>Q;
for(int i = 0;i<M;i++){
int a,b;
cin>>a>>b;
paths[a].push_back(b);
}
SMALL::PREC();
while(Q--){
int st;
cin>>st;
vector<int> v;
int k;
cin>>k;
while(k--){
int tmp;
cin>>tmp;
v.push_back(tmp);
}
if(v.size()+10<B)SMALL::GETANS(st,v);
else BIG::GETANS(st,v);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Correct |
1 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
7 ms |
5980 KB |
Output is correct |
6 |
Correct |
6 ms |
5980 KB |
Output is correct |
7 |
Correct |
6 ms |
5980 KB |
Output is correct |
8 |
Correct |
17 ms |
7768 KB |
Output is correct |
9 |
Correct |
17 ms |
7516 KB |
Output is correct |
10 |
Correct |
17 ms |
7516 KB |
Output is correct |
11 |
Correct |
19 ms |
7260 KB |
Output is correct |
12 |
Correct |
11 ms |
6748 KB |
Output is correct |
13 |
Correct |
19 ms |
7176 KB |
Output is correct |
14 |
Correct |
14 ms |
6632 KB |
Output is correct |
15 |
Correct |
10 ms |
6488 KB |
Output is correct |
16 |
Correct |
15 ms |
6488 KB |
Output is correct |
17 |
Correct |
15 ms |
6748 KB |
Output is correct |
18 |
Correct |
12 ms |
6380 KB |
Output is correct |
19 |
Correct |
17 ms |
6968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Correct |
1 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
7 ms |
5980 KB |
Output is correct |
6 |
Correct |
6 ms |
5980 KB |
Output is correct |
7 |
Correct |
6 ms |
5980 KB |
Output is correct |
8 |
Correct |
17 ms |
7768 KB |
Output is correct |
9 |
Correct |
17 ms |
7516 KB |
Output is correct |
10 |
Correct |
17 ms |
7516 KB |
Output is correct |
11 |
Correct |
19 ms |
7260 KB |
Output is correct |
12 |
Correct |
11 ms |
6748 KB |
Output is correct |
13 |
Correct |
19 ms |
7176 KB |
Output is correct |
14 |
Correct |
14 ms |
6632 KB |
Output is correct |
15 |
Correct |
10 ms |
6488 KB |
Output is correct |
16 |
Correct |
15 ms |
6488 KB |
Output is correct |
17 |
Correct |
15 ms |
6748 KB |
Output is correct |
18 |
Correct |
12 ms |
6380 KB |
Output is correct |
19 |
Correct |
17 ms |
6968 KB |
Output is correct |
20 |
Correct |
1652 ms |
8600 KB |
Output is correct |
21 |
Correct |
1498 ms |
8884 KB |
Output is correct |
22 |
Correct |
1585 ms |
8604 KB |
Output is correct |
23 |
Correct |
1884 ms |
8728 KB |
Output is correct |
24 |
Correct |
1842 ms |
156180 KB |
Output is correct |
25 |
Execution timed out |
2005 ms |
160332 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Correct |
1 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5212 KB |
Output is correct |
5 |
Correct |
7 ms |
5980 KB |
Output is correct |
6 |
Correct |
6 ms |
5980 KB |
Output is correct |
7 |
Correct |
6 ms |
5980 KB |
Output is correct |
8 |
Correct |
17 ms |
7768 KB |
Output is correct |
9 |
Correct |
17 ms |
7516 KB |
Output is correct |
10 |
Correct |
17 ms |
7516 KB |
Output is correct |
11 |
Correct |
19 ms |
7260 KB |
Output is correct |
12 |
Correct |
11 ms |
6748 KB |
Output is correct |
13 |
Correct |
19 ms |
7176 KB |
Output is correct |
14 |
Correct |
14 ms |
6632 KB |
Output is correct |
15 |
Correct |
10 ms |
6488 KB |
Output is correct |
16 |
Correct |
15 ms |
6488 KB |
Output is correct |
17 |
Correct |
15 ms |
6748 KB |
Output is correct |
18 |
Correct |
12 ms |
6380 KB |
Output is correct |
19 |
Correct |
17 ms |
6968 KB |
Output is correct |
20 |
Correct |
1652 ms |
8600 KB |
Output is correct |
21 |
Correct |
1498 ms |
8884 KB |
Output is correct |
22 |
Correct |
1585 ms |
8604 KB |
Output is correct |
23 |
Correct |
1884 ms |
8728 KB |
Output is correct |
24 |
Correct |
1842 ms |
156180 KB |
Output is correct |
25 |
Execution timed out |
2005 ms |
160332 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |