이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int, int>
#define ff first
#define ss second
#define ld long double
#define pb push_back
pii a[200100];
pii c[200100];
int dp[200100], us[200100];
vector<int> dq[200100];
void solve(){
int n, k;
cin>>n>>k;
int m;
cin>>m;
for(int i=1; i<=m; i++){
cin>>a[i].ff>>a[i].ss;
if(a[i].ff < a[i].ss){
dq[a[i].ff].pb(a[i].ss);
dq[min(a[i].ff + k, a[i].ss + 1)].pb(-a[i].ss);
}
if(a[i].ff > a[i].ss){
dq[max(a[i].ff - k + 1, a[i].ss)].pb(a[i].ss);
dq[a[i].ff + 1].pb(-a[i].ss);
}
}
multiset<int> dd;
for(int i=1; i<=n; i++){
//cout<<"##############\n";
//cout<<i<<'\n';
for(int h: dq[i]){
//cout<<h<<' ';
if(h > 0) dd.insert(h);
else dd.erase(dd.find(-h));
}
//cout<<'\n';
int l=i, r=i;
if(dd.size()){
l = min(l, *dd.begin());
r = max(r, *dd.rbegin());
}
c[i] = {l, r};
}
//for(int i=1; i<=n; i++){
//cout<<c[i].ff<<' '<<c[i].ss<<'\n';
//}
int t;
cin>>t;
while(t--){
int s, f;
cin>>s>>f;
queue<int> q;
q.push(s);
for(int i=1; i<=n; i++){
dp[i] = us[i] = 0;
}
dp[s] = 0;
us[s] = 1;
while(q.size()){
int v = q.front();
q.pop();
for(int i = c[v].ff; i<=c[v].ss; i++){
if(us[i] == 1) continue;
dp[i] = dp[v] + 1;
q.push(i);
us[i] = 1;
}
}
if(us[f] == 0){
cout<<-1<<'\n';
}
else{
cout<<dp[f]<<'\n';
}
}
}
signed main(){
solve();
}
# | 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... |