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 T ll tt ; cin>>tt ; while(tt--)
#define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define endl '\n'
#define yes void (cout << "YES" << endl)
#define no void (cout << "NO" << endl)
#define pb push_back
#define F first
#define S second
#define ld long double
#define fixed(g) fixed<<setprecision(g)
#define ll long long
using namespace std;
const ll N = 2e6 + 9 ;
const ll oo = 1e18 + 7 ;
ll n , m , k , x , y , z , ind , ans , a[N] , b[N] , mn , sz[N] , dp[5009][5009];
bool vis[N] , ff , ok[N];
vector<ll>v[N];
void bfs(ll x){
for(ll i=1 ; i<=n ; i++){
vis[i] = 0 ;
sz[i] = 0 ;
}
queue<ll> q ;
q.push(x) ;
while(!q.empty()){
ll node = q.front() ;
q.pop() ;
vis[node] = 1 ;
for(auto e : v[node]){
if(vis[e])continue ;
vis[e] = 1 ;
sz[e] = sz[node] + 1 ;
dp[x][e] = sz[e] ;
q.push(e);
}
}
}
int main(){
fast
cin >> n >> m ;
for(ll i=1 ; i<=n ; i++){
cin >> a[i] >> b[i] ;
}
for(ll i=1 ; i<=n ; i++){
for(ll j=1 ; j<=n ; j++){
if(i==j)continue ;
dp[i][j] = -1 ;
if(a[j]<=b[i] && b[i]<=b[j])
v[i].pb(j);
}
}
while(m--){
cin >> x >> y ;
if(x==y)cout<<0<<endl;
else{
if(ok[x]){
if(dp[x][y]==-1)cout<<"impossible"<<endl;
else cout<<dp[x][y]<<endl ;
continue ;
}
ok[x] = 1 ;
bfs(x) ;
if(sz[y]==0)cout<<"impossible"<<endl ;
else cout<<sz[y]<<endl ;
}
}
return 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |