Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- int n, m;
- vector <pii> G;
- vector <int> d;
- int inf = 2e9;
- void bfs() {
- d[0] = 0;
- deque <int> Q;
- Q.push_back(0);
- while (!Q.empty()) {
- int v = Q.front();
- Q.pop_front();
- for (pii e: G[v]) {
- int u = e.first, w = e.second;
- if (d[u] != inf) {
- continue;
- }
- if (d[u] > d[v] + w) {
- d[u] = d[v] + w;
- if (w == 0) {
- Q.push_front(u);
- } else {
- Q.push_back(u);
- }
- }
- }
- }
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin >> n >> m;
- vector <int> kng(n,-1);
- for (int &i: kng) cin >> i;
- G.resize(n);
- d.assign(n, inf);
- for (int i = 0; i < n; ++i) {
- int a, b; cin >> a >> b;
- a--;
- b--;
- int w = 1;
- if (kng[a] == kng[b]) {
- w = 0;
- }
- pii ab = {b, w};
- pii ba = {a, w};
- G[a].pb(ab);
- G[b].pb(ba);
- }
- bfs();
- if (d[n - 1] == inf) {
- cout << "impossible";
- exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement