#include<bits/stdc++.h>
using namespace std;
struct node {
vector<node*> l; // neighbors in the original graph
vector<node*> c; // children in the CT
int a, id; // a is the value of the node, id is its index
bool v = false; // initially, no node is marked
};
// <DSU>
int f(int i, int r[]){ // find representant of i
if(r[i] == i) return i;
return r[i] = f(r[i], r);
}
void merge(int a, int b, int r[], int s[]){
a = f(a, r);
b = f(b, r);
if(a == b) return;
if(s[a] > s[b]){
r[b] = a;
s[a] += s[b];
} else {
r[a] = b;
s[b] += s[a];
}
}
// </DSU>
void solve(){
// <boring setup>
int n,u,v,m;
cin >> n >> m;
node g[n+1];
int o[n], r[n+1], s[n+1];
node* c[n+1]; // c[u] is the node with maximum value that u is connected to in the DSU, if u is its own representant
for(int i = 1;i <= n;i++) {
cin >> g[i].a;
g[i].id = i;
r[i] = i;
s[i] = 1;
c[i] = &g[i];
o[i-1] = i;
}
while(m--){
cin >> u >> v;
g[u].l.push_back(&g[v]);
g[v].l.push_back(&g[u]);
}
// </boring setup>
sort(o, o+n, [&g](int a, int b){return g[a].a < g[b].a;}); // sorting the nodes by value
for(int j = 0;j < n;j++){
node* d = &g[o[j]]; // d is the current node
d->v = true; // mark the current node
for(node* x : d->l){ // iterate over the neighbors of d
if(!x->v || f(x->id, r) == f(d->id, r)) continue; // x is unmarked or it's already connected to d
d->c.push_back(c[f(x->id, r)]); // add and edge in the CT between d and the node with maximum value that is connected to x
merge(x->id, d->id, r, s); // connect d and x in the DSU
c[f(d->id, r)] = d;
}
}
// Use the CT
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IG5vZGUgewoJdmVjdG9yPG5vZGUqPiBsOyAvLyBuZWlnaGJvcnMgaW4gdGhlIG9yaWdpbmFsIGdyYXBoCgl2ZWN0b3I8bm9kZSo+IGM7IC8vIGNoaWxkcmVuIGluIHRoZSBDVAoJaW50IGEsIGlkOyAvLyB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