Con questo codice prendo 10/100, fallendo 3 testcase apparentemente random (2, 9, 15) in 3 subtask diversi, tutti per output non corretto.
#include <bits/stdc++.h>
#define maxnodi 10000
using namespace std;
int h[maxnodi];
bool vis[maxnodi];
vector<pair<int,int>> grafo[maxnodi];
queue<int> coda;
int n, m, nodoAtt;
int main(){
freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i=0; i<n; i++)
cin >> h[i];
for(int i=0; i<m; i++){
int from, to;
cin >> from >> to;
grafo[from].push_back(make_pair(h[to], to));
grafo[to].push_back(make_pair(h[from], from));
}
int prev = -1;
coda.push(0);
while (1)
{
nodoAtt = coda.front(); coda.pop();
if (vis[nodoAtt]) { cout << nodoAtt; return 0; }
vis[nodoAtt] = 1;
if (grafo[nodoAtt].size() == 1) { cout << nodoAtt; return 0; }
sort(grafo[nodoAtt].begin(), grafo[nodoAtt].end());
if (grafo[nodoAtt].front().second != prev)
coda.push(grafo[nodoAtt].front().second);
else coda.push(grafo[nodoAtt].at(1).second);
prev = nodoAtt;
}
return 0;
}