Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- // #define int long long
- #define ld long double
- #define F first
- #define S second
- #define el '\n'
- #define cout(x) for(auto v:x)cout<<v<<' ';cout<<el
- #define coutp(x) for(auto v:x)cout<<v.F<<' '<<v.S<<el
- #define cin(x) for(auto &v:x)cin>>v;
- #define all(x) x.begin(),x.end()
- #define ll long long
- #define sz(x) (int)x.size()
- #define pi pair<ll,ll>
- using ull = unsigned long long;
- void solve(){
- ll n;
- cin>>n;
- vector<ll>dis(n+3,-1);
- vector<pi>v(n+3);
- unordered_map<ll,vector<ll>>mp[2];
- for (int i=0;i<n+2;i++)
- {
- cin>>v[i].first>>v[i].second;
- mp[0][v[i].first].push_back(i);
- mp[1][v[i].second].push_back(i);
- }
- queue<pair<ll,bool>>q;
- dis[0]=0;
- q.push({0,0});
- q.push({0,1});
- while (!q.empty())
- {
- ll u=q.front().first;
- ll dir=q.front().second;
- ll val=dir ? v[u].second : v[u].first;
- q.pop();
- for (auto it:mp[dir][val])
- {
- if (dis[it]==-1)q.push({it,!dir}),dis[it]=dis[u]+1;
- }
- }
- if (dis[1]==-1)cout<<-1;
- else cout<<dis[1]-1;
- }
- int32_t main()
- {
- // #ifndef ONLINE_JUDGE
- // freopen("in.txt", "r", stdin);
- // // freopen("output.txt", "w", stdout);
- // #endif
- freopen("lasers.in","r",stdin);
- freopen("lasers.out","w",stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int tc = 1;
- //cin >> tc;
- for (int i = 1; i <= tc; i++)solve();
- return 0;
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment