#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long

vector<int> parent;
int root(int i){
  if(parent[i]==i) return i;
  parent[i] = root(parent[i]);
  return parent[i];
}


int main(){

  int n,q;
  cin >> n >> q;

  vector<int> c(n);
  for(auto& i : c) cin >> i;

  vector<map<int,int>> cla(n);
  parent.resize(n);
  //vector<int> len(n,1);
  
  for(int i=0; i<n; ++i) {
    cla[i][c[i]]++;
    parent[i] = i;
  }

  for(int i=0; i<q; ++i) {
    int t,a,b;
    cin >> t >> a >> b;

    //cout << t << ' ' << a << ' ' << b << endl;

    if(t==1){

      int ra = root(a-1), rb = root(b-1);
      if(ra == rb) continue;

      if(cla[ra].size()>cla[rb].size()){
	for(auto j : cla[rb])
	  cla[ra][j.first] += j.second;
	//cla[rb].clear();
	parent[rb] = ra;
	//len[a-1] += len[b-1];
	//len[b-1] = 0;
      }else{
	for(auto j : cla[ra])
	  cla[rb][j.first] += j.second;
	//cla[ra].clear();
	parent[ra] = rb;
	//len[b-1] += len[a-1];
	//len[a-1] = 0;

      }
    }
    else{
      cout << cla[root(a-1)][b] << endl;
    }
  }

}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
#define nl "\n"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define fl(a,x) memset(a,x,sizeof(a));
#define pt(a,x) cout << get<x>(a) << " " ;
#define mt make_tuple
#define vec vector
#define ld long double
#define eps 1e-9

#define int long long

const int mod = (int) 1e9 + 7 ;
const int MOD = (int) 998244353 ;
const int INF = (int) 1e18 ;

typedef vector<int> vii;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<vector<int> > vv;

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

const int N = 2e5 + 5; 
vii g[N] ;
int a[N], b[N], A, B; 
bool vis[N] ;

void dfs(int node) {
	if(vis[node]) return ; 
	vis[node] = 1 ;

	A += a[node], B += b[node]; 

	for(auto i : g[node]) {
		if(not vis[i]) {
			dfs(i) ;
		}	
	}
}

void solve() { 

	int n, m; cin >> n >> m ;

	rep(i,1,n+1) cin >> a[i] ;
	rep(i,1,n+1) cin >> b[i] ;

	rep(i,0,m) {
		int u,v; cin >> u >> v;
		g[u].eb(v) ;
		g[v].eb(u) ;	
	}

	rep(i,1,n+1) {
		if(not vis[i]) {
			A = 0 , B = 0 ;
			dfs(i) ;
			if(A != B) {
				cout << "No"; return ; 
			}
		}
	}

	cout << "Yes" ;
}

int32_t main() {

#ifdef LOCAL
    freopen("error.txt", "w", stderr);
#endif

    fast;

	// int t; cin >> t; while(t--)
		solve() ;

	// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}