|
| 1 | +#include <cstring> |
| 2 | +#include <map> |
| 3 | +#include <deque> |
| 4 | +#include <queue> |
| 5 | +#include <stack> |
| 6 | +#include <sstream> |
| 7 | +#include <numeric> |
| 8 | +#include <iostream> |
| 9 | +#include <iomanip> |
| 10 | +#include <cstdio> |
| 11 | +#include <cmath> |
| 12 | +#include <fstream> |
| 13 | +#include <string> |
| 14 | +#include <cstdlib> |
| 15 | +#include <ctime> |
| 16 | +#include <functional> |
| 17 | +#include <algorithm> |
| 18 | +#include <vector> |
| 19 | +#include <set> |
| 20 | +#include <complex> |
| 21 | +#include <list> |
| 22 | +#include <climits> |
| 23 | +#include <cctype> |
| 24 | +#include <bitset> |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +#define PI 3.14159265359 |
| 28 | +#define all(v) v.begin(),v.end() |
| 29 | +#define sortva(v) sort(all(v)) |
| 30 | +#define sortvd(v) sort(v.rbegin(),v.rend()) |
| 31 | +#define sortaa(a,n) sort(a,a+n) |
| 32 | +#define sortad(a,n) sort(a,a+n),reverse(a,a+n) |
| 33 | +#define sfi1(v) scanf("%d",&v) |
| 34 | +#define sfi2(v1,v2) scanf("%d %d",&v1,&v2) |
| 35 | +#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3) |
| 36 | +#define sfll1(v) scanf("%I64d",&v); |
| 37 | +#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2) |
| 38 | +#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3) |
| 39 | +#define sfstr(v) scanf("%s", v); |
| 40 | +#define sz(v) (int)v.size() |
| 41 | +#define ndl puts("") |
| 42 | +#define flush fflush(stdout) |
| 43 | +#define SS stringstream |
| 44 | +typedef long long ll; |
| 45 | +typedef unsigned long long ull; |
| 46 | +typedef long double ld; |
| 47 | + |
| 48 | +int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; |
| 49 | +int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 }; |
| 50 | + |
| 51 | +ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } |
| 52 | +ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } |
| 53 | + |
| 54 | +void PLAY() { |
| 55 | +//#ifndef ONLINE_JUDGE |
| 56 | +freopen("lasers.in", "r", stdin); |
| 57 | +freopen("lasers.out", "w", stdout); |
| 58 | +//#endif |
| 59 | + |
| 60 | +cout << fixed << setprecision(10); |
| 61 | +ios::sync_with_stdio(0); |
| 62 | +cin.tie(0); |
| 63 | +cout.tie(0); |
| 64 | +} |
| 65 | + |
| 66 | +const int MAX = 1e6; |
| 67 | + |
| 68 | +pair<pair<int, int>, int> arr[MAX]; |
| 69 | +pair<pair<int, int>, int> arr1[MAX]; |
| 70 | +vector<pair<int, int>> adj[MAX]; |
| 71 | +bool vis[MAX]; |
| 72 | + |
| 73 | +int main() { |
| 74 | +PLAY(); |
| 75 | + |
| 76 | +int n; |
| 77 | +cin >> n; |
| 78 | +n += 2; |
| 79 | +for (int i = 0, idx = 0; i < n; i++, idx += 2) { |
| 80 | +cin >> arr[i].first.first >> arr[i].first.second; |
| 81 | +arr[i].second = idx; |
| 82 | +arr1[i] = { { arr[i].first.second, arr[i].first.first }, idx + 1 }; |
| 83 | +adj[idx].push_back({ idx + 1, 1 }); |
| 84 | +adj[idx + 1].push_back({ idx, 1 }); |
| 85 | +} |
| 86 | +int from1 = 0, from2 = 1; |
| 87 | +int to1 = 2, to2 = 3; |
| 88 | + |
| 89 | +sort(arr, arr + n); |
| 90 | +map<int, int> prev; |
| 91 | + |
| 92 | +for (int i = 0; i < n; i++) { |
| 93 | +if (prev.find(arr[i].first.first) == prev.end()) |
| 94 | +prev[arr[i].first.first] = arr[i].second; |
| 95 | +else { |
| 96 | +int p = prev[arr[i].first.first]; |
| 97 | +adj[p].push_back({ arr[i].second, 0 }); |
| 98 | +adj[arr[i].second].push_back({ p, 0 }); |
| 99 | +prev[arr[i].first.first] = arr[i].second; |
| 100 | +} |
| 101 | +} |
| 102 | + |
| 103 | +sort(arr1, arr1 + n); |
| 104 | +prev.clear(); |
| 105 | + |
| 106 | +for (int i = 0; i < n; i++) { |
| 107 | +if (prev.find(arr1[i].first.first) == prev.end()) |
| 108 | +prev[arr1[i].first.first] = arr1[i].second; |
| 109 | +else { |
| 110 | +int p = prev[arr1[i].first.first]; |
| 111 | +adj[p].push_back({ arr1[i].second, 0 }); |
| 112 | +adj[arr1[i].second].push_back({ p, 0 }); |
| 113 | +prev[arr1[i].first.first] = arr1[i].second; |
| 114 | +} |
| 115 | +} |
| 116 | + |
| 117 | +deque<pair<int, int>> qu; |
| 118 | +qu.push_back({ from1, 0 }); |
| 119 | +qu.push_back({ from2, 0 }); |
| 120 | + |
| 121 | +while (sz(qu)) { |
| 122 | +int cur_node = qu.front().first; |
| 123 | +int cur_cost = qu.front().second; |
| 124 | +if (cur_node == to1 || cur_node == to2) { |
| 125 | +cout << cur_cost << endl; |
| 126 | +return 0; |
| 127 | +} |
| 128 | +qu.pop_front(); |
| 129 | +if (vis[cur_node]) continue; |
| 130 | +vis[cur_node] = true; |
| 131 | +for (int i = 0; i < sz(adj[cur_node]); i++) { |
| 132 | +int to_node = adj[cur_node][i].first; |
| 133 | +int to_cost = adj[cur_node][i].second; |
| 134 | +if (!vis[to_node]) { |
| 135 | +if (to_cost) qu.push_back({ to_node, cur_cost + 1 }); |
| 136 | +else qu.push_front({ to_node, cur_cost }); |
| 137 | +} |
| 138 | +} |
| 139 | +} |
| 140 | + |
| 141 | +cout << -1 << endl; |
| 142 | + |
| 143 | +return 0; |
| 144 | +} |
0 commit comments