# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1108060 | kiethm07 | Bitaro’s Party (JOI18_bitaro) | C++11 | 1060 ms | 92620 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pii pair<int,int>
#define iii pair<int,pii>
#define fi first
#define se second
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define TEXT "a"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int inf = 1e9 + 7;
const ld eps = 1e-8;
const double pi = acos(-1);
const int N = 1e5 + 5;
const int BLOCK = 100;
vector<int> a[N];
bool del[N];
int n,m,q;
int dp[N];
bool used[N];
pair<int,int> f[N][BLOCK];
int sz[N];
int cal(int u){
if (dp[u] != -inf) return dp[u];
dp[u] = del[u] ? -1 : 0;
for (int v : a[u]){
int f = cal(v);
if (f != -1) dp[u] = max(dp[u], f + 1);
}
return dp[u];
}
void precompute(){
for (int u = 1; u <= n; u++){
priority_queue<pii> pq;
for (int v : a[u]){
for (int i = 0; i < sz[v]; i++) pq.push(f[v][i]);
}
vector<int> tmp;
while(!pq.empty() && sz[u] < BLOCK){
pii t = pq.top();
pq.pop();
if (t.fi == -1) break;
if (used[t.se] == 0){
used[t.se] = 1;
tmp.push_back(t.se);
}
else continue;
t.fi++;
f[u][sz[u]++] = t;
}
if (sz[u] < BLOCK) f[u][sz[u]++] = pii(0,u);
for (int i : tmp) used[i] = 0;
}
}
void heavy(int str, vector<int>& v){
for (int i = 1; i <= n; i++) dp[i] = -inf;
for (int i : v) del[i] = 1;
cout << cal(str) << "\n";
for (int i : v) del[i] = 0;
}
void light(int str, vector<int>& v){
int i = 0;
for (int i : v) used[i] = 1;
int res = -1;
for (int i = 0; i < sz[str]; i++){
int dis = f[str][i].fi;
int id = f[str][i].se;
if (used[id]) continue;
res = dis;
break;
}
for (int i : v) used[i] = 0;
cout << res << "\n";
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
if (fopen(TEXT".inp","r")){
freopen(TEXT".inp","r",stdin);
freopen(TEXT".out","w",stdout);
}
cin >> n >> m >> q;
for (int i = 1; i <= m; i++){
int u,v;
cin >> u >> v;
a[v].push_back(u);
}
precompute();
// for (int u = 1; u <= n; u++) cout << u << " " << sz[u] << "\n";
while(q--){
int str;
cin >> str;
int num;
cin >> num;
vector<int> v(num);
for (int& i : v) cin >> i;
if (v.size() >= BLOCK) heavy(str,v);
else light(str,v);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |