Advertisement
Guest User

ois_ciclismo

a guest
Aug 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #define MAXN 1000000
  4.  
  5. using namespace std;
  6.  
  7. vector<int> G[MAXN];
  8. long H[MAXN];
  9. long N, M;
  10. bool visited[MAXN];
  11.  
  12. int dfs(int n)
  13. {
  14.     int Min = -1;
  15.     int Peso_Min = H[n];
  16.     for (auto x : G[n])
  17.     {
  18.         if(H[x] < Peso_Min)
  19.         {
  20.             Min = x;
  21.             Peso_Min = H[x];
  22.         }
  23.         if(!visited[Min]) dfs(Min);
  24.         else if(visited[Min]) return Min;
  25.     }
  26. }
  27.  
  28. int main()
  29. {
  30.     freopen("input.txt", "r", stdin);
  31.     freopen("output.txt", "w", stdout);
  32.     scanf("%ld%ld", &N, &M);
  33.     for (int i = 0; i < N; i++) scanf("%ld", &H[i]);
  34.     for (int i = 0; i < M; i++)
  35.     {
  36.         long a, b;
  37.         scanf("%ld%ld", &a, &b);
  38.         G[a].push_back(b);
  39.         G[b].push_back(a);
  40.     }
  41.     printf("%d\n", dfs(0));
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement