#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
int arr[150005];
int arr2[150005];
vector<int>adj[150005];
vector<int>storage[150005];
vector<int>storage2[150005];
struct DSU{
vector<int>e;
stack<array<int,4>>stk;
void init(int n){
e=vector<int>(n,-1);
}
int get(int x){
return e[x]<0?x:get(e[x]);
}
bool unite(int x, int y){
//show(trigger,1);
x=get(x); y=get(y);
if(x==y) return 1;
stk.push({x,y,e[x],e[y]});
if(e[x]>e[y]) swap(x,y);
e[x]+=e[y];
e[y]=x;
return 0;
}
void rollback(){
array<int,4>hold=stk.top();
stk.pop();
e[hold[0]]=hold[2];
e[hold[1]]=hold[3];
}
};
DSU dsu;
bool amos=true;
int cnt[150005];
int done[150005];
void dnc(int l, int r){
if(l==r){
int counter=0;
for(auto it:storage2[l]){
cnt[it]++;
if(cnt[it]==2){
for(auto neigh:adj[it]){
if(cnt[neigh]==2){
counter++;
dsu.unite(it,neigh);
//show2(unite,it,unite,neigh);
}
}
}
}
//check
for(auto it:storage[l]){
//paint
cnt[it]++;
if(cnt[it]==2){
for(auto neigh:adj[it]){
if(cnt[neigh]==2){
counter++;
dsu.unite(it,neigh);
//show2(unite,it,unite,neigh);
}
}
}
}
for(auto it:storage[l]){
int hold=dsu.get(it);
done[hold]=true;
//show(hold,hold);
}
//cout << l << " " << r << "\n";
//for(int x=1;x<=4;x++){
//cout << cnt[x] << " ";
//}
//cout << " cnt\n";
for(auto it:storage2[l]){
int hold=dsu.get(it);
//show(check,hold);
if(!done[hold]){
amos=false;
//show(l,l);
}
cnt[it]--;
}
for(auto it:storage[l]){
int hold=dsu.get(it);
done[hold]=false;
cnt[it]--;
}
for(int x=0;x<counter;x++) dsu.rollback();
//cout << "rollback " << counter << "\n";
//cout << "\n";
return;
}
int mid=(l+r)/2;
//go left
int counter=0;
for(int x=mid+1;x<=r;x++){
for(auto it:storage[x]){
cnt[it]++;
if(cnt[it]==2){
for(auto neigh:adj[it]){
if(cnt[neigh]==2){
//show2(unite,it,unite,neigh);
dsu.unite(it,neigh);
counter++;
}
}
}
}
}
dnc(l,mid);
for(int x=0;x<counter;x++) dsu.rollback();
//cout << "rollback " << counter << "\n";
//go right
for(int x=mid+1;x<=r;x++){
for(auto it:storage[x]){
cnt[it]--;
}
}
counter=0;
for(int x=l;x<=mid;x++){
for(auto it:storage2[x]){
cnt[it]++;
if(cnt[it]==2){
for(auto neigh:adj[it]){
if(cnt[neigh]==2){
//show2(unite,it,unite,neigh);
dsu.unite(it,neigh);
counter++;
}
}
}
}
}
dnc(mid+1,r);
for(int x=0;x<counter;x++) dsu.rollback();
//cout << "rollback " << counter << "\n";
for(int x=l;x<=mid;x++){
for(auto it:storage2[x]){
cnt[it]--;
}
}
}
void solve(){
cin >> n >> m;
for(int x=1;x<=n;x++){
adj[x].clear();
cnt[x]=0;
storage[x].clear();
storage2[x].clear();
}
for(int x=0;x<n;x++){
cin >> arr[x];
storage[arr[x]].push_back(x+1);
}
for(int x=0;x<n;x++){
cin >> arr2[x];
storage2[arr2[x]].push_back(x+1);
}
int temp,temp2;
for(int x=0;x<m;x++){
cin >> temp >> temp2;
adj[temp].push_back(temp2);
adj[temp2].push_back(temp);
}
dsu.init(n+5);
amos=true;
dnc(1,n);
if(amos) cout << 1 << "\n";
else cout << 0 << "\n";
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t=1;
cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |