// Alir3za.Zar3 -> Shiraz , Iran
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int long long
#define pii pair<int,int>
const int mxN = 1e5+7 , T = 25;
int n , m , q; bool mrk[mxN];
set<pii , greater<pii>> bitaro[mxN];
vector<int> e[mxN];
void iN ()
{
cin >> n >> m >> q;
for (int i=1; i<=m; i++)
{
int u,v; cin >> u >> v;
if (u > v) swap(u , v);
e[u].push_back(v);
}
}
void preprocess ()
{
for (int i=1; i<=n; i++)
bitaro[i].insert({0 , i});
bool vis[mxN];
memset(vis , 0 , sizeof(vis));
for (int v=1; v<=n; v++)
{
set<pii,greater<pii>> update;
for (auto [k , p] : bitaro[v])
{
int sz = update.size();
if (sz >= T) break;
if (vis[p]) continue;
update.insert({k , p});
vis[p] = true;
}
bitaro[v] = update;
for (auto [k , p] : update)
vis[p] = 0;
for (auto to : e[v])
for (auto [k , p] : bitaro[v])
bitaro[to].insert({k+1 , p});
}
}
void reset (vector<int> que)
{
for (auto v : que) mrk[v] = 0;
}
int light (int o , int sz , vector<int> que)
{
for (auto v : que) mrk[v] = 1;
for (auto [len,v] : bitaro[o])
if (!mrk[v])
{
reset(que);
return len;
}
reset(que);
return -1;
}
int heavy (int o , int sz , vector<int> que)
{
int f[mxN];
memset(f , 0 , sizeof(f));
for (auto v : que)
f[v] = -mxN;
for (int v=1; v<=n; v++)
for (auto to : e[v])
f[to] = max(f[to] , f[v]+1);
if (f[o] < 0) return -1;
else return f[o];
}
void ouT ()
{
while (q--)
{
int v,sz; cin >> v >> sz;
vector<int> busy;
for (int i=1 , x; i<=sz; i++)
cin >> x , busy.push_back(x);
if (sz < T) cout << light (v , sz , busy) << '\n';
else cout << heavy (v , sz , busy) << '\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
iN ();
preprocess ();
ouT ();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |