#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
#pragma GCC optimize("O3","unroll-loops")
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll n, m;
ll a[200005], b[200005], par[200005], siz[200005];
vector<pi> op;
vector<ll> fi[200005], las[200005];
ll find(ll x) {return ((par[x]==x)?x: find(par[x]));}
void unite(ll x, ll y)
{
x = find(x), y = find(y);
if (x==y) {op.pb(mp(-1, -1)); return;}
if (siz[x] < siz[y]) swap(x, y);
par[y] = x;
siz[x] += siz[y];
op.pb(mp(x, y));
}
void rollback()
{
ll x = op.back().f, y = op.back().s;
op.pop_back();
if (x==-1 && y==-1) return;
par[y] = y;
siz[x] -= siz[y];
}
struct node{
int s, e, m;
vector<pi> edges;
node *l, *r;
node (int S, int E)
{
s = S, e = E, m = (s+e)/2;
if (s!=e)
{
l = new node(s, m);
r = new node(m + 1, e);
}
}
void update(int S, int E, pi edge)
{
if (S > E) return;
if(s==S && e==E) {edges.pb(edge);}
else{
if(E <= m) l->update(S, E, edge);
else if (m < S) r->update(S, E, edge);
else l->update(S, m, edge),r->update(m+1, E, edge);
}
}
bool valid()
{
for (pi u: edges) unite(u.f, u.s);
if (s==e)
{
unordered_set<ll> ss;
for (ll u: fi[s]) ss.insert(find(u));
for (ll u: las[s]) if (ss.find(find(u))==ss.end()) return 0;
}
else
{
if (!l->valid()) return 0;
if (!r->valid()) return 0;
}
for (pi u: edges) rollback();
return 1;
}
} *root;
void solve()
{
ll n, m;
cin >> n >> m;
op.clear();
for (ll i=1; i<=n; ++i) par[i] = i, siz[i] = 1, fi[i].clear(), las[i].clear();
for (ll i=1; i<=n; ++i) cin >> a[i];
for (ll i=1; i<=n; ++i) cin >> b[i];
for (ll i=1; i<=n; ++i)
{
fi[a[i]].pb(i);
las[b[i]].pb(i);
}
root = new node(1, n);
for (ll i=1; i<=m; ++i)
{
ll u, v;
cin >> u >> v;
root->update(max(b[u], b[v]), min(a[u], a[v]), mp(u, v));
}
for (ll i=1; i<=n; ++i) if (a[i] < b[i]) {cout << "0\n"; return;}
if (root->valid()) cout << "1\n";
else cout << "0\n";
}
int main()
{
ll t;
cin >> t;
while (t--) solve();
return 0;
}
# | 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... |