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 int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"
const double PI=3.141592653589;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
void solve()
{
int n;
cin >> n;
vector<vector<int>>adj(n+1);
vector<int>deg(n+1);
for(int i = 1;i<=n;++i){
int l,r;
cin >> l >> r;
for(int j = l;j<=r;++j){
if(i==j)continue;
adj[i].pb(j);
deg[i]++;
}
}
int c = 0;
for(int j = 1;j<=n;++j){
if(deg[j]==0)c++;
}
int Q;
cin >> Q;
while(Q--){
vector<int>dist(n+1,1e18);
vector<int>visited(n+1),par(n+1);
queue<int>q;
int x;
cin >> x;
dist[x] = 0;
q.push(x);
while(!q.empty()){
int u = q.front();
q.pop();
if(visited[u])continue;
visited[u] = 1;
for(int v : adj[u]){
par[v] = u;
dist[v] = min(dist[v],dist[u]+1);
q.push(v);
}
}
int res = *max_element(dist.begin()+1, dist.end());
set<int>w;
for(int i = 1;i<=n;++i){
if(deg[i]==0)w.insert(par[i]);
}
if(res==1e18 || w.size()>1)res=-1;
cout << res << endl;
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int T=1;
for(int i = 1;i<=T;++i)
{
// cout << "Case #" << i << ": ";
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... |