#include<bits/stdc++.h>
#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"
using namespace std;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int N = 3e6 + 15;
int n, m;
int a[N], b[N];
pii edges[N];
bool ans = 1;
vector<pii> tree_edges[N];
vector<int> upd[N], ask[N];
void update(int l, int r, int id, int u, int v, pii e){
if(r < u || l > v) return;
if(u <= l && r <= v){
tree_edges[id].push_back(e);
return;
}
int mid = (l+r) >> 1;
update(l,mid,id<<1,u,v,e);
update(mid+1,r,id<<1|1,u,v,e);
}
// b[v] = a[u]
int par[N];
int siz[N];
stack<pair<int &, int>> st;
bool mark[N];
int snap(){
return (int)st.size();
}
int asc(int u){
return u == par[u] ? u : asc(par[u]);
}
void join(int u, int v){
u = asc(u), v = asc(v);
if(u == v) return;
if(siz[u] < siz[v]) swap(u, v);
st.push({par[v], par[v]});
st.push({siz[u], siz[u]});
par[v] = u;
siz[u] += siz[v];
}
void rollback(int snapshot){
while(snap() > snapshot){
st.top().fi = st.top().se;
st.pop();
}
}
void dnc(int l, int r, int id){
int snapshot = snap();
for(auto [u,v ] : tree_edges[id]) join(u, v);
if(l == r){
for(int u : upd[l]){
u = asc(u);
mark[u] = true;
}
for(int u : ask[l]){
u = asc(u);
ans &= mark[u];
}
for(int u : upd[l]){
u = asc(u);
mark[u] = false;
}
rollback(snapshot);
return;
}
int mid = (l+r) >> 1;
dnc(l,mid,id<<1);
dnc(mid+1,r,id<<1|1);
rollback(snapshot);
if(!ans) return;
}
void clear_all(int l, int r, int id){
tree_edges[id].clear();
if(l == r) return;
int mid = (l+r) >> 1;
clear_all(l,mid,id<<1);
clear_all(mid+1,r,id<<1|1);
}
void clean(){
ans = true;
for(int i = 1; i <= n; i++){
upd[i].clear();
ask[i].clear();
}
clear_all(1,n,1);
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
upd[a[i]].push_back(i);
par[i] = i;
siz[i] = 1;
}
for(int i = 1; i <= n; i++){
cin >> b[i];
ask[b[i]].push_back(i);
}
for(int i = 1; i <= m; i++) cin >> edges[i].fi >> edges[i].se;
for(int i = 1; i <= n; i++){
if(a[i] < b[i]){
ans = 0;
break;
}
}
if(!ans){
cout << ans << endl;
clean();
return;
}
for(int i = 1; i <= m; i++){
int u, v; tie(u, v) = edges[i];
if(max(b[u], b[v]) > min(a[u], a[v])) continue;
// range from L -> R will have edge (u, v)
//cout << "edges: " << max(b[u], b[v]) << " " << min(a[u], a[v]) << endl;
//cout << u << " " << v << endl;
update(1, n, 1, max(b[u], b[v]), min(a[u], a[v]), pii(u, v));
}
dnc(1,n,1);
cout << ans << endl;
clean();
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; cin >> t;
while(t--) solve();
}
Compilation message (stderr)
colors.cpp: In function 'int main()':
colors.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
181 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
colors.cpp:182:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
182 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |