이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=100005, s=300;
ll dp[maxn], seen[maxn];
vector <pll> best[maxn];
vector <ll> A[maxn], At[maxn];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, m, q; cin >> n >> m >> q;
for (ll i=1; i<=m; i++)
{
ll u, v; cin >> u >> v;
A[u].pb(v), At[v].pb(u);
}
for (ll u=1; u<=n; u++)
{
if (sz(At[u]))
{
vector <pll> val;
best[u]=best[At[u][0]];
for (ll i=1; i<sz(At[u]); i++)
{
val.clear(), val.reserve(s*2);
ll v=At[u][i];
for (ll p1=0, p2=0; p1<sz(best[u]) || p2<sz(best[v]);)
{
if (p1==sz(best[u]) || (p2<sz(best[v]) && best[u][p1]<best[v][p2]))
val.pb(best[v][p2]), p2++;
else val.pb(best[u][p1]), p1++;
}
best[u].clear();
for (auto [x, v]:val)
if (!seen[v])
{
seen[v]=1, best[u].pb({x, v});
if (sz(best[u])==s) break;
}
for (auto [x, v]:best[u]) seen[v]=0;
}
}
for (auto &[x, v]:best[u]) x++;
if (sz(best[u])<s) best[u].pb({0, u});
}
for (ll i=1; i<=q; i++)
{
ll X, x, ans=-1; cin >> X >> x;
vector <ll> a(x);
for (ll &i:a) cin >> i, seen[i]=1;
if (x>=s)
{
for (ll i=1; i<=n; i++) dp[i]=seen[i]?-1e9:0;
for (ll u=1; u<=X; u++)
for (ll v:At[u])
dp[u]=max(dp[u], dp[v]+1);
if (dp[X]>=0) ans=dp[X];
}
else for (auto [x, v]:best[X])
if (!seen[v]) {ans=x; break;}
for (ll j:a) seen[j]=0;
cout << ans << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |