Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- #define pb emplace_back
- #define AI(i) begin(i), end(i)
- template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
- template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
- #ifdef KEV
- #define DE(args...) kout("{ " + string(#args) + " } = ", args)
- void kout() { cerr << endl; }
- template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
- #else
- #define DE(...) 0
- #define debug(...) 0
- #endif
- const int MAX_N = 300010, inf = 1e9;
- struct dsu {
- vector<int> g, sz;
- dsu(int n) : g(n), sz(n, 1) {
- iota(AI(g), 0);
- }
- int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
- bool M(int a, int b) {
- a = F(a), b = F(b);
- if (a == b) return false;
- if (sz[a] < sz[b]) swap(a, b);
- return sz[a] += sz[b], g[b] = a, true;
- }
- };
- int32_t main() {
- ios_base::sync_with_stdio(0), cin.tie(0);
- int n, m;
- cin >> n >> m;
- vector<vector<int>> edge(n+1);
- vector<int> l(n);
- for (int &i : l) cin >> i;
- for (int a, b, i = 0;i < m;++i) {
- cin >> a >> b, --a, --b;
- edge[a].pb(b), edge[b].pb(a);
- }
- vector<int> lcnt(n+10), rcnt(n+10);
- vector<int> id(n); iota(AI(id), 0); sort(AI(id), [&](int x, int y) {
- return l[x] < l[y];
- });
- vector<int> cl(l); sort(AI(cl));
- {
- int sz = 0;
- dsu g(n + 10);
- for (int i = 0;i < n;++i) {
- int x = id[i];
- ++sz;
- for (int j : edge[x]) if (l[j] <= l[x])
- sz -= g.M(j, x);
- if (i==n-1 || cl[i+1] > cl[i]) {
- lcnt[i] = sz;
- }
- else lcnt[i] = inf;
- }
- }
- {
- int sz = 0;
- dsu g(n + 10);
- for (int i = n-1;i >= 0;--i) {
- int x = id[i];
- DE(x+1);
- ++sz;
- for (int j : edge[x]) if (l[j] >= l[x]) {
- sz -= g.M(j, x);
- DE(x+1, j+1, l[j], l[x]);
- }
- if (i==0 || cl[i]>cl[i-1]) {
- rcnt[i] = sz;
- }
- else rcnt[i] = inf;
- }
- }
- for (int i =0 ;i < n;++i) DE(i, lcnt[i], rcnt[i]);
- for (int i = 0;i+1 < n;++i) {
- if (cl[i+1] > cl[i]) {
- if (min(i+1, n-i-1) >= lcnt[i]+rcnt[i+1]-1) {
- DE(i, min(i+1, n-i-1), lcnt[i], rcnt[i+1]);
- cout << cl[i] << '\n';
- return 0;
- }
- }
- }
- cout << -1 << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment