两个人,一个从1到n,另一个从n到1,通过多次移动完成,每次两个走的下一个点的颜色不同
输入
3
4 4
0 1 0 1
1 2
2 3
1 3
2 4
3 3
0 1 0
1 2
2 3
1 3
6 6
0 0 1 1 0 1
1 2
2 6
3 6
4 6
4 5
2 4
输出
3
-1
3
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <algorithm>
#define x first
#define y second
#define pb emplace_back
#define fu(i,a,b) for(int i=a;i<=b; ++ i)
#define fd(i,a,b) for(int i=a;i>=b; -- i)
#define endl '\n'
#define ms(x,y) memset(x,y,sizeof x)
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<vector<LL>> VVL;
typedef vector<vector<int>> VVI;
typedef vector<LL> VL;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef pair<PII,int> PIII;
typedef pair<double,double> PDD;
typedef pair<double,int> PDI;
typedef pair<char,int> PCI;
typedef pair<string,int> PSI;
typedef pair<int,string> PIS;
typedef pair<int,char> PIC;
typedef pair<LL,LL> PLL;
typedef __int128 i128;
typedef unsigned long long ULL;
const int N = 2e3 + 10,M = N * N /2 ,INF = 0x3f3f3f3f,P = 131;
const double eps = 1e-8,DNF = 1e18;
const int mod = 1e3,base= 20010;
const LL LNF=(LL) INF * INF;
bool st[N];
int h[N],ne[M],e[M],idx;
int dist[N][N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
inline void solve()
{
ms(h,-1);idx=0;
ms(dist,-1);
int n,m;
cin >> n >> m ;
VI c(n+1);
fu(i,1,n) cin >> c[i];
fu(i,1,m)
{
int a,b;cin >> a >> b;
add(a,b);add(b,a);
}
queue<tuple<int,int,int>> q;
q.emplace(0,1,n);
while(q.size())
{
auto t = q.front();q.pop();
int d= get<0>(t),x = get<1>(t),y = get<2>(t);
if(dist[x][y] != - 1) continue;
dist[x][y] = d;
for(int i=h[x];~i;i=ne[i])
{
int a = e[i];
for(int k=h[y];~k;k=ne[k])
{
int b= e[k];
if(c[a] != c[b])
q.emplace(d+1,a,b);
}
}
}
cout << dist[n][1] << endl;
}
signed main()
{
// freopen("1.txt","r",stdin);
// #define int long long
// init(N-1);
ios
// cout << fixed<<setprecision(2);
int t=1;
cin>>t;
int now = 1;
while(t -- )
{
// cout<<"Case ";
// cout<<"Case #";
// cout<<"Scenario #";
// cout<< now ++ <<": ";
// cout<< now ++ <<": \n";
solve();
}
return 0;
}
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <algorithm>
#define x first
#define y second
#define pb emplace_back
#define fu(i,a,b) for(int i=a;i<=b; ++ i)
#define fd(i,a,b) for(int i=a;i>=b; -- i)
#define endl '\n'
#define ms(x,y) memset(x,y,sizeof x)
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<vector<LL>> VVL;
typedef vector<vector<int>> VVI;
typedef vector<LL> VL;
typedef vector<bool> VB;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef pair<PII,int> PIII;
typedef pair<double,double> PDD;
typedef pair<double,int> PDI;
typedef pair<char,int> PCI;
typedef pair<string,int> PSI;
typedef pair<int,string> PIS;
typedef pair<int,char> PIC;
typedef pair<LL,LL> PLL;
typedef __int128 i128;
typedef unsigned long long ULL;
const int N = 2e3 + 10,M = N * N /2 ,INF = 0x3f3f3f3f,P = 131;
const double eps = 1e-8,DNF = 1e18;
const int mod = 1e3,base= 20010;
const LL LNF=(LL) INF * INF;
inline void solve()
{
int n,m;
cin >> n >> m ;
VI c(n);
fu(i,0,n-1) cin >> c[i];
VVI adj(n);
fu(i,1,m)
{
int a,b;cin >> a >> b;
a--,b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
VVI dp(n,VI(n,-1));
queue<tuple<int,int,int>> q;
q.emplace(0,0,n-1);
while(q.size())
{
auto t = q.front();q.pop();
int d= get<0>(t),x = get<1>(t),y = get<2>(t);
if(dp[x][y] != - 1 )continue;
dp[x][y] = d;
for(auto a : adj[x])
for(auto b : adj[y])
if(c[a] != c[b])
q.emplace(d+1,a,b);
}
cout << dp[n-1][0] << endl;
}
signed main()
{
// freopen("1.txt","r",stdin);
// #define int long long
// init(N-1);
ios
// cout << fixed<<setprecision(2);
int t=1;
cin>>t;
int now = 1;
while(t -- )
{
// cout<<"Case ";
// cout<<"Case #";
// cout<<"Scenario #";
// cout<< now ++ <<": ";
// cout<< now ++ <<": \n";
solve();
}
return 0;
}