# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096225 | Zero_OP | Bitaro’s Party (JOI18_bitaro) | C++14 | 1088 ms | 414804 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"
using namespace std;
#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define rall(v) rbegin(v), rend(v)
const int MAX = 1e5 + 5;
int SQRT = 350;
int n, m, q, f[MAX];
vector<int> rev[MAX];
vector<pair<int, int>> dp[MAX]; //[dist, u]
bool bad[MAX];
bool vis[MAX];
int ptr[MAX];
void process(int u){
vector<int> processed_vertices;
for(int t = 0; t < SQRT + 2; ++t){
pair<int, int> dist = {-1, -1};
int num = 0;
for(int v : rev[u]){
while(ptr[v] < sz(dp[v])){
if(vis[dp[v][ptr[v]].second]){
++ptr[v];
continue;
} else if(dp[v][ptr[v]].first + 1 > dist.first){
dist.first = dp[v][ptr[v]].first + 1;
dist.second = dp[v][ptr[v]].second;
num = v;
}
break;
}
}
if(dist.first == -1) break;
vis[dist.second] = true;
processed_vertices.push_back(dist.second);
dp[u].push_back(dist);
++ptr[num];
}
for(int v : rev[u]) ptr[v] = 0;
for(int u : processed_vertices) vis[u] = false;
dp[u].push_back({0, u});
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("task.inp", "r")){
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
cin >> n >> m >> q;
for(int i = 0; i < m; ++i){
int u, v;
cin >> u >> v;
rev[v].push_back(u);
}
for(int i = 1; i <= n; ++i){
sort(all(rev[i]));
compact(rev[i]);
}
SQRT = sqrt(n);
for(int i = 1; i <= n; ++i){
process(i);
}
// return 0;
// for(int i = 1; i <= n; ++i){
// cout << i << " :\n";
// for(auto j : best[i]) cout << j.first << ' ' << j.second << '\n'; cout << '\n';
// }
while(q--){
int t, y;
cin >> t >> y;
vector<int> nodes(y);
for(int i = 0; i < y; ++i) cin >> nodes[i];
for(int u : nodes) bad[u] = true;
if(y > SQRT){
// cout << "heavy\n";
fill(f + 1, f + 1 + n, -1e9);
f[t] = 0;
for(int i = t; i > 0; --i){
for(int j : rev[i]){
f[j] = max(f[j], f[i] + 1);
}
}
int ans = -1;
for(int i = t; i > 0; --i){
if(!bad[i]) ans = max(ans, f[i]);
}
cout << ans << '\n';
} else{
// cout << "light\n";
int ans = -1;
for(auto u : dp[t]){
if(!bad[u.second]){
ans = u.first;
break;
}
}
if(ans == -1){
cout << ans << '\n';
} else{
cout << ans << '\n';
}
}
for(int u : nodes) bad[u] = false;
}
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... |