#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <bits/stdc++.h>
#include <queue>
#include <math.h>
#include <bitset>
#include <map>
#include <vector>
#include <cstdio>
#include <climits>
#define white 0
#define gray 1
#define black 2
#define LIMIT (1<<30)
#define MOD 1000000007
#define ll long long
using namespace std;

class DisjointSet{
    public:
        vector<ll> rank, p;
        DisjointSet(){}
        DisjointSet(ll size){
            rank.resize(size, 0);
            p.resize(size, 0);
            for(ll i=0; i<size; i++) makeSet(i);
        }
        
        void makeSet(ll x){
            p[x]=x;
            rank[x]=0;
        }
        
        bool same(ll x, ll y){
            return findSet(x) == findSet(y);
        }
        
        void unite(ll x, ll y){
            link(findSet(x), findSet(y));
        }
        
        void link(ll x, ll y){
            if(rank[x]>rank[y]){
                p[y]=x;
            }else{
                p[x]=y;
                
                if(rank[x]==rank[y]){
                    rank[y]++;
                }
            }
        }
        
        ll findSet(ll x){
            if(x!=p[x]){
                p[x]=findSet(p[x]);
            }
            
            return p[x];
        }
};

int main(){
    ll n, m;
    cin >> n >> m;
    ll a[n], b[n];
    for(ll i=0; i<n; i++) cin >> a[i];
    for(ll i=0; i<n; i++) cin >> b[i];

    DisjointSet ds = DisjointSet(n + 1);
    
    for(ll i=0; i<m; i++){
        ll c, d;
        cin >> c >> d;
        ds.unite(c, d);
    }
    
    map<ll, ll> mpA, mpB;
    for(ll i=1; i<=n; i++){
        mpA[ds.findSet(i)] += a[i - 1];
        mpB[ds.findSet(i)] += b[i - 1];
        //cout << ds.findSet(i) << endl;
    }
    
    bool ans = true;
    for(auto it = mpA.begin(); it != mpA.end(); it++){
        ll num = it -> first;
        //cout << num << " " << mpA[num] << " " << mpB[num] << endl;
        if(mpA[num] != mpB[num]){
            ans = false;
            break;
        }
    }
    
    if(ans) cout << "Yes" << endl;
    else cout << "No" << endl;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define mod 1000000007
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int> 
#define vvi vector<vector<int>> 
#define pb push_back
#define endl '\n'
vector<int> vis;
vector<vector<int>> g;

int dfs(int u){
	int ans=1;
	vis[u]=1;
	for(auto v:g[u]) if(!vis[v]) ans+=dfs(v);
	return ans;
}
void tests(){
	int n, m;
	cin>>n>>m;

	g.resize(n+1);
	int res=0; 
	for(int i=0;i<m;i++){
		int u, v;
		cin>>u>>v;
		g[u].push_back(v);
	}
	for(int i=1;i<=n;i++){
		vis.assign(n+1,0);
		res+=dfs(i);
	}
	cout<<res<<endl;
}
int32_t main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL) ; 
	int t = 1;
	// cin>>t;
	while(t--){
		tests();
	}
 	return 0 ; 
}