// grader for problem : 선재가 그래프를 잃어버렸습니다
// all rights to 14026 김지성. Gyeonggi Science High School

#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

const int max_n = 220000;
int n,m,Dist[max_n],dist[max_n],cnt[max_n];
long long d;
bool chk[max_n];
struct pi {
    int first,second;
    pi(){}
    pi(int first_,int second_)  { first=first_;second=second_;}
} v[max_n];
vector<int> ans[max_n];
queue<int> q;
FILE *input, *yoursolution;

bool cmp1(pi A,pi B){return A.first<B.first;}

bool cmp2(pi A,pi B){return A.second<B.second;}

bool make_graph() {
    for(int i=1;i<=n;i++) cnt[v[i].first] ++;
    int it = 1;
    if(1LL * d * cnt[0] < 1LL * cnt[1]) return false;
    while(cnt[it]) {
        if(1LL * (d-1) * cnt[it] < 1LL * cnt[it+1]) return false;
        it++;
    }
    return true;
}

bool checkifimpossible() {
    if(v[1].first) return true;
    if(v[2].first == 0) return true;
    for(int i=1;i<n;i++) if(v[i+1].first-v[i].first > 1) return true;
    if(!make_graph()) return true;
    return false;
}

bool checker() {
    fscanf(input,"%d %lld",&n,&d);
    for(int i=1;i<=n;i++) {
        fscanf(input,"%d",&Dist[i]);
        v[i] = pi(Dist[i],i);
    }
    if(fscanf(yoursolution,"%d",&m) == EOF)	return false;
    sort(v+1,v+n+1,cmp1);

    if(m==-1) return checkifimpossible();
    if(m<-1 || m>1000000) return false;
    if(v[1].first) return false;
    if(v[2].second == 0) return false;
    for(int i=1;i<n;i++) {
        if(v[i+1].first-v[i].first > 1) return false;
    } // checking : only one zero, all distance for min~max

    for(int i=0;i<m;i++) {
        int X,Y;
        if(fscanf(yoursolution,"%d %d",&X,&Y) == EOF) return false;
        if(X<1 || X>n || Y<1 || Y>n || X==Y) return false;
        ans[X].push_back(Y);
        ans[Y].push_back(X);
    }
    for(int i=1;i<=n;i++) {
        if((int)ans[i].size() > d) return false;
    }

    int colored = 0;
    q.push(v[1].second);
    chk[v[1].second] = true;
    dist[v[1].second] = 0;
    colored = 1;
    sort(v+1,v+n+1,cmp2);
    while(!q.empty()) {
        int t = q.front();
        q.pop();
        for(int i=0;i<(int)ans[t].size();i++) {
            int nxt = ans[t][i];
            if(chk[nxt]) continue;
            q.push(nxt);
            chk[nxt] = true;
            dist[nxt] = dist[t]+1;
            colored ++;
        }
    }

    if(colored < n) return false;
    for(int i=1;i<=n;i++) {
        if(dist[i] != Dist[i]) return false;
    }
    return true;
}

int main() {
    input = fopen("/var/www/bbs/prob/1370/input.txt","r"); // 문제 데이터 파일입니다.
    yoursolution = fopen("/var/www/bbs/prob/1370/output.txt", "r"); // 채점받고자 하는 사람의 파일입니다.
    if(checker()) printf("1"); // checker()가 true이면 맞는 솔루션입니다.
    else printf("0"); // checker()가 false이면 틀린 솔루션입니다.
    return 0;
}
