RT,求大佬带STO
#include<bits/stdc++.h>
#define N 100100
#define M 100100
using namespace std;
int n,m;
int a[N],fa[N],co[N],su[3];
map<pair<int,int>,bool>qq;
vector<int>to[N];
inline void build(int x,int y){
if(qq[make_pair(x,y)])return;
to[x].push_back(y);
qq[make_pair(x,y)]=1;
}
inline int find(int k){
if(fa[k]==k)return k;
return fa[k]=find(fa[k]);
}
inline void un(int x,int y){
x=find(x);y=find(y);
fa[x]=y;
a[y]+=a[x];
a[x]=0;
}
struct Jack{
int k,a,b;
inline bool operator <(const Jack &b)const{
return k<b.k;
}
}done[M];
inline bool dfs(int k,int c){
co[k]=c;
su[c]+=a[k];
vector<int>::iterator i;
for(i=to[k].begin( );i!=to[k].end( );i++){
if(!co[*i]){
if(!dfs(*i,3-c))return 0;
}else if(co[*i]==c)return 0;
}
return 1;
}
inline void work( ){
cin>>n>>m;
memset(co,0,sizeof(co));
int i,j,x;
for(i=1;i<=n;i++)cin>>a[i];
for(i=1;i<=n;i++){
cin>>x;
a[i]-=x;
fa[i]=i;
}
for(i=1;i<=m;i++)cin>>done[i].k>>done[i].a>>done[i].b;
sort(done+1,done+1+m);
while(m){
if(done[m].k==1)break;
if(find(done[m].a)!=find(done[m].b))
un(done[m].a,done[m].b);
m--;
}
for(i=1;i<=m;i++){
build(find(done[i].a),find(done[i].b));
build(find(done[i].b),find(done[i].a));
}
bool op=1,qwq;
for(i=1;i<=n;i++)
if(!co[i]&&find(i)==i){
su[1]=su[2]=0;
qwq=dfs(i,1);
if(qwq&&su[1]!=su[2])op=0;
if(!qwq&&(su[1]^su[2])&1)op=0;
}
if(op)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
int main( ){
//freopen("sequence.in","r",stdin);
//freopen("sequence.out","w",stdout);
std::ios::sync_with_stdio(0);
int t;
cin>>t;
while(t--)work( );
}