Kevin_Zhang

Untitled

Nov 6th, 2021
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. #define pb emplace_back
  5. #define AI(i) begin(i), end(i)
  6. template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
  7. template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
  8. #ifdef KEV
  9. #define DE(args...) kout("{ " + string(#args) + " } = ", args)
  10. void kout() { cerr << endl; }
  11. template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
  12. #else
  13. #define DE(...) 0
  14. #define debug(...) 0
  15. #endif
  16.  
  17. const int MAX_N = 300010, inf = 1e9;
  18.  
  19. struct dsu {
  20.     vector<int> g, sz;
  21.     dsu(int n) : g(n), sz(n, 1) {
  22.         iota(AI(g), 0);
  23.     }
  24.     int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
  25.     bool M(int a, int b) {
  26.         a = F(a), b = F(b);
  27.         if (a == b) return false;
  28.         if (sz[a] < sz[b]) swap(a, b);
  29.         return sz[a] += sz[b], g[b] = a, true;
  30.     }
  31. };
  32. int32_t main() {
  33.     ios_base::sync_with_stdio(0), cin.tie(0);
  34.  
  35.     int n, m;
  36.     cin >> n >> m;
  37.     vector<vector<int>> edge(n+1);
  38.  
  39.     vector<int> l(n);
  40.  
  41.     for (int &i : l) cin >> i;
  42.  
  43.     for (int a, b, i = 0;i < m;++i) {
  44.         cin >> a >> b, --a, --b;
  45.         edge[a].pb(b), edge[b].pb(a);
  46.     }
  47.  
  48.     vector<int> lcnt(n+10), rcnt(n+10);
  49.  
  50.     vector<int> id(n); iota(AI(id), 0); sort(AI(id), [&](int x, int y) {
  51.         return l[x] < l[y];
  52.     });
  53.  
  54.     vector<int> cl(l); sort(AI(cl));
  55.  
  56.     {
  57.         int sz = 0;
  58.         dsu g(n + 10);
  59.         for (int i = 0;i < n;++i) {
  60.             int x = id[i];
  61.             ++sz;
  62.             for (int j : edge[x]) if (l[j] <= l[x])
  63.                 sz -= g.M(j, x);
  64.  
  65.             if (i==n-1 || cl[i+1] > cl[i]) {
  66.                 lcnt[i] = sz;
  67.             }
  68.             else lcnt[i] = inf;
  69.         }
  70.     }
  71.  
  72.     {
  73.         int sz = 0;
  74.         dsu g(n + 10);
  75.         for (int i = n-1;i >= 0;--i) {
  76.             int x = id[i];
  77.             DE(x+1);
  78.             ++sz;
  79.             for (int j : edge[x]) if (l[j] >= l[x]) {
  80.                 sz -= g.M(j, x);
  81.                 DE(x+1, j+1, l[j], l[x]);
  82.             }
  83.  
  84.             if (i==0 || cl[i]>cl[i-1]) {
  85.                 rcnt[i] = sz;
  86.             }
  87.             else rcnt[i] = inf;
  88.         }
  89.     }
  90.     for (int i =0 ;i < n;++i) DE(i, lcnt[i], rcnt[i]);
  91.  
  92.     for (int i = 0;i+1 < n;++i) {
  93.         if (cl[i+1] > cl[i]) {
  94.             if (min(i+1, n-i-1) >= lcnt[i]+rcnt[i+1]-1) {
  95.                 DE(i, min(i+1, n-i-1), lcnt[i], rcnt[i+1]);
  96.                 cout << cl[i] << '\n';
  97.                 return 0;
  98.             }
  99.         }
  100.     }
  101.     cout << -1 << '\n';
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment