Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <vector>
- #define MAXN 1000000
- using namespace std;
- vector<int> G[MAXN];
- long H[MAXN];
- long N, M;
- bool visited[MAXN];
- int dfs(int n)
- {
- int Min = -1;
- int Peso_Min = H[n];
- for (auto x : G[n])
- {
- if(H[x] < Peso_Min)
- {
- Min = x;
- Peso_Min = H[x];
- }
- if(!visited[Min]) dfs(Min);
- else if(visited[Min]) return Min;
- }
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- scanf("%ld%ld", &N, &M);
- for (int i = 0; i < N; i++) scanf("%ld", &H[i]);
- for (int i = 0; i < M; i++)
- {
- long a, b;
- scanf("%ld%ld", &a, &b);
- G[a].push_back(b);
- G[b].push_back(a);
- }
- printf("%d\n", dfs(0));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement