# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1277582 | hiepsimauhong | Bitaro’s Party (JOI18_bitaro) | C++20 | 519 ms | 267980 KiB |
#include <bits/stdc++.h>
using namespace std;
#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 1, L){FOR(KO, 1, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';
#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly cin.tie(0) -> ios_base::sync_with_stdio(0);
const int N = 2e5 + 5;
const int SQRT = 320;
const int oo = 1e9;
int n, m, q;
bool dead[N];
int f[N], vis[N];;
ii dp[N][SQRT + 5], _new[N];
vector<int> g[N];
vector<int> go[N];
void PreWork(){
int id = 0;
FOR(u, 1, n){
dp[u][1] = {u, 0};
FOA(v, g[u]){
++id;
int i = 1, j = 1;
int k = 0;
while(dp[u][i].fs && dp[v][j].fs && k < SQRT){
int x, y;
if(dp[u][i].sd > dp[v][j].sd){
x = dp[u][i].fs;
y = dp[u][i].sd;
++i;
}
else{
x = dp[v][j].fs;
y = dp[v][j].sd + 1;
++j;
}
if(vis[x] != id){
vis[x] = id;
_new[++k] = {x, y};
}
}
while(dp[u][i].fs && k < SQRT){
int x = dp[u][i].fs;
int y = dp[u][i].sd;
++i;
if(vis[x] != id){
vis[x] = id;
_new[++k] = {x, y};
}
}
while(dp[v][j].fs && k < SQRT){
int x = dp[v][j].fs;
int y = dp[v][j].sd + 1;
++j;
if(vis[x] != id){
vis[x] = id;
_new[++k] = {x, y};
}
}
FOR(i, 1, k){
dp[u][i] = _new[i];
}
}
}
}
signed main(){ quickly
cin >> n >> m >> q;
FOR(i, 1, m){
int u, v;
cin >> u >> v;
g[v].push_back(u);
go[u].push_back(v);
}
PreWork();
memset(vis, 0, sizeof vis);
vis[0] = 1;
while(q--){
int t, s;
cin >> t >> s;
if(s >= SQRT){
memset(dead, 0, sizeof dead);
memset(f, -1, sizeof f);
FOR(i, 1, s){
int u; cin >> u;
dead[u] = 1;
}
f[t] = 0;
int ans = -1;
FOD(u, t, 1){
FOA(v, go[u]){
if(f[v] != -1){
f[u] = max(f[u], f[v] + 1);
}
}
if(!dead[u]){
ans = max(ans, f[u]);
}
}
cout << ans << '\n';
}
else{
vector<int> save;
FOR(i, 1, s){
int u; cin >> u;
save.push_back(u);
vis[u] = 1;
}
int ans = -1;
FOR(i, 1, SQRT){
if(!vis[dp[t][i].fs]){
ans = dp[t][i].sd;
break;
}
}
cout << ans << '\n';
FOR(i, 0, s - 1){
vis[save[i]] = 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... |