[題解] P6185 [NOI Online #1 提高組] 序列
阿新 • • 發佈:2021-08-12
#include <iostream> #include <cstdio> #include <cstring> #include <cctype> using namespace std; #define LL long long const int maxn = 2e5 + 10; template < class T > inline void read(T& s) { s = 0; int f = 0; char c = getchar(); while ('0' > c || c > '9') f |= c == '-', c = getchar(); while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar(); s = f ? -s : s; } int head[maxn],cnt=0,T,n,m; struct edge{ int to,nxt; }e[maxn<<2]; inline void add(int u,int v){ e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt; } inline void link(int u,int v){ add(u,v);add(v,u); } LL sum[maxn]; int val[maxn],pos[maxn],tot=0; void clear(){ cnt=tot=0; memset(head,0,sizeof head); memset(pos,0,sizeof pos); memset(sum,0,sizeof sum); } void dfs(int u,int block){ sum[pos[u]=block]+=val[u]; for(int i=head[u];i;i=e[i].nxt){ if(!pos[e[i].to])dfs(e[i].to,block); } } void solve(){ clear();read(n);read(m); for(int i=1;i<=(n<<1);i++)read(val[i]); for(int i=1;i<=m;i++){ int t,u,v;read(t);read(u);read(v); if(t==1) link(u,v+n),link(v,u+n); if(t==2) link(u,v),link(u+n,v+n); } for(int i=1;i<=n<<1;i++)if(!pos[i])dfs(i,++tot); int fl=1; for(int i=1;i<=n;i++){ if(pos[i]!=pos[i+n])fl&= sum[pos[i]]==sum[pos[i+n]]; else fl&= sum[pos[i]]%2==0; } puts(fl?"YES":"NO"); } int main(){ scanf("%d",&T); while(T--){ solve(); } return 0; }