H题 广搜

#include<vector>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> pll;

bool vis[2020][2020];

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int>c(n + 1);
    vector<int>a[2050];
    memset(vis, false, sizeof(vis));
    for (int i = 1; i <= n; i++)cin >> c[i];
    while (m--) {
        int u, v;
        cin >> u >> v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    queue<pll>q;
    q.push({ {1,n},0 });
    while (!q.empty()) {
        int x = q.front().first.first, y = q.front().first.second;//cout<<x<<" "<<y<<endl;
        int num = q.front().second;
        q.pop();
        if (x == n && y == 1) {
            cout << num << endl;
            return;
        }
        for (int i = 0; i < a[x].size(); i++) {
            for (int j = 0; j < a[y].size(); j++) {
                if (vis[a[x][i]][a[y][j]]||c[a[x][i]]==c[a[y][j]])continue;
                else {
                    q.push({ {a[x][i],a[y][j]},num + 1 });
                    vis[a[x][i]][a[y][j]] = true;
                }
            }
        }
    }
    cout << "-1" << endl;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;
    while (t--)solve();
    return 0;
}