이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll int
#define llinf 1LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 100005
ll n,m,Q;
ll d = 500;
vector<ll> g[maxn],rg[maxn];
vector<pll> v[maxn];
ll deg[maxn],dp[maxn];
bool cmp(pll a,pll b){return a.sc>b.sc;}
bool bad[maxn],vis[maxn];
void dfs(ll u){
vis[u] = 1;
for(ll s : rg[u]){
if(!vis[s]) dfs(s);
if(dp[s]==-llinf) continue;
dp[u] = max(dp[u],dp[s]+1);
}
}
void tc(){
cin >> n >> m >> Q;
for(ll i = 1;i<=m;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
deg[y]++;
rg[y].pb(x);
}
queue<ll> q;
for(ll i = 1;i<=n;i++){
if(deg[i]) continue;
q.push(i);
}
while(sz(q)){
ll u = q.front();
q.pop();
v[u].pb({u,0});
for(ll s : rg[u]) for(pll p : v[s]) v[u].pb({p.fi,p.sc+1});
vector<pll> w;
sort(all(v[u]));
for(ll i = 0;i<sz(v[u]);i++){
if(i==sz(v[u])-1||v[u][i].fi!=v[u][i+1].fi) w.pb(v[u][i]);
}
v[u].clear();
for(pll x : w) v[u].pb(x);
sort(all(v[u]),cmp);
while(sz(v[u])>d) v[u].popb();
for(ll s : g[u]){
deg[s]--;
if(!deg[s]) q.push(s);
}
}
while(Q--){
ll c,u;
cin >> u >> c;
vector<ll> w(c);
for(ll &x : w){
cin >> x;
bad[x] = 1;
}
if(c<d){
for(pll p : v[u]){
if(!bad[p.fi]){
cout<<p.sc<<endl;
goto lol;
}
}
cout<<-1<<endl;
lol:;
}else{
for(ll i = 1;i<=n;i++){
if(bad[i]) dp[i] = -llinf;
else dp[i] = 0;
vis[i] = 0;
}
dfs(u);
if(dp[u]==-llinf) dp[u] = -1;
cout<<dp[u]<<endl;
}
for(ll x : w) bad[x] = 0;
}
}
int main(){
daj_mi_malo_vremena
int t; t = 1;
while(t--){
tc();
}
return 0;
}
/**
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
**/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |