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 fi first
#define se second
#define vsz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
using namespace std;
const int N = 1e5+1;
int n, m, q, node[N];
vector<int> adj[N], rev_adj[N], Q[N];
namespace Sub1{
bool ckSub(){
return n <= 1e3;
}
const int N = 1e3+1;
int mx_dist[N], del[N];
void process()
{
for(int cur_q = 1; cur_q <= q; cur_q++){
for(int v : Q[cur_q]){
del[v] = cur_q;
}
memset(mx_dist, 0, sizeof(mx_dist));
mx_dist[node[cur_q]] = 1;
int answer = 0;
for(int i = node[cur_q]; i >= 1; i--){
if(!mx_dist[i]) continue;
if(del[i] != cur_q) answer = max(answer, mx_dist[i]);
for(int v : rev_adj[i]){
mx_dist[v] = max(mx_dist[v], mx_dist[i]+1);
}
}
--answer;
cout << answer <<"\n";
}
}
}
namespace Sub2{
bool ckSub(){
return q <= 1;
}
const int N = 1e5+1;
int mx_dist[N], del[N];
void process()
{
for(int cur_q = 1; cur_q <= q; cur_q++){
for(int v : Q[cur_q]){
del[v] = cur_q;
}
memset(mx_dist, 0, sizeof(mx_dist));
mx_dist[node[cur_q]] = 1;
int answer = 0;
for(int i = node[cur_q]; i >= 1; i--){
if(!mx_dist[i]) continue;
if(del[i] != cur_q) answer = max(answer, mx_dist[i]);
for(int v : rev_adj[i]){
mx_dist[v] = max(mx_dist[v], mx_dist[i]+1);
}
}
--answer;
cout << answer <<"\n";
}
}
}
namespace Sub3{
const int N = 1e5+1;
const int SZ = 400;
vector<pair<int,int>> mx_dist[N];
int in[N], cntMerge, dist[N], del[N];
void merge_vec(vector<pair<int,int>>& a, vector<pair<int,int>>& b){
// merge sort
++cntMerge;
vector<pair<int,int>> tmp;
int i = 0, j = 0;
while(vsz(tmp) < SZ && i < vsz(a) && j < vsz(b)){
if(in[a[i].se] == cntMerge){
i++;
continue;
}
if(in[b[j].se] == cntMerge){
j++;
continue;
}
if(a[i].fi > b[j].fi + 1){
in[a[i].se] = cntMerge;
tmp.push_back(a[i]);
i++;
}
else{
in[b[j].se] = cntMerge;
tmp.push_back(make_pair(b[j].fi+1, b[j].se));
j++;
}
}
while(vsz(tmp) < SZ && i < vsz(a)){
if(in[a[i].se] == cntMerge){
i++;
continue;
}
tmp.push_back(a[i]);
i++;
}
while(vsz(tmp) < SZ && j < vsz(b)){
if(in[b[j].se] == cntMerge){
j++;
continue;
}
tmp.push_back(make_pair(b[j].fi+1, b[j].se));
j++;
}
swap(tmp, a);
return;
}
void process()
{
//Get sqrt max dist for each node
for(int i = 1; i <= n; i++){
if(vsz(mx_dist[i]) < SZ){
mx_dist[i].push_back(make_pair(0, i));
}
for(int v : adj[i]){
merge_vec(mx_dist[v], mx_dist[i]);
}
}
for(int cur_q = 1; cur_q <= q; cur_q++){
if(vsz(Q[cur_q]) > SZ){
//Heavy
for(int v : Q[cur_q]){
del[v] = cur_q;
}
memset(dist, 0, sizeof(dist));
int answer = 0;
dist[node[cur_q]] = 1;
for(int i = node[cur_q]; i >= 1; i--){
if(!dist[i]) continue;
if(del[i] != cur_q) answer = max(answer, dist[i]);
for(int v : rev_adj[i]){
dist[v] = max(dist[v], dist[i]+1);
}
}
--answer;
cout << answer <<"\n";
}
else{
for(int v : Q[cur_q]){
del[v] = cur_q;
}
int answer = -1;
for(pair<int,int> w : mx_dist[node[cur_q]]){
int d = w.fi, v = w.se;
if(del[v] != cur_q){
answer = d;
break;
}
}
cout << answer <<"\n";
}
}
}
}
void solve()
{
cin >> n >> m >> q;
for(int i = 1; i <= m; i++){
int u,v; cin >> u >> v;
adj[u].push_back(v);
rev_adj[v].push_back(u);
}
for(int i = 1; i <= q; i++){
cin >> node[i];
int x; cin >> x;
for(int j = 1; j <= x; j++){
int v; cin >> v;
Q[i].push_back(v);
}
}
Sub3::process();
return;
if(Sub1::ckSub()){
Sub1::process();
}
else if(Sub2::ckSub()){
Sub2::process();
}
else Sub3::process();
return;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'int32_t main()':
bitaro.cpp:233:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
233 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:234:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
234 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |