This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int t, n, m, a[N], b[N], mark[N], vis[N], par[N], sz[N], cnt[N];
vector<int> adj[N], seg[2*N], veca[N], vecb[N], rolbak;
int getPar(int v) {return (par[v]==0 ? v : getPar(par[v]));}
void merge(int v, int u) {
v = getPar(v), u=getPar(u);
if(v == u) return;
if(sz[v] < sz[u]) swap(v,u);
sz[v] += sz[u];
par[u] = v;
rolbak.pb(u);
}
void rbk(int x) {
while(size(rolbak) > x) {
int v = rolbak.back();
rolbak.pop_back();
sz[par[v]] -= sz[v];
par[v] = 0;
}
}
void add(int l, int r, int v, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(l>=rc || lc>=r) return;
if(lc>=l && rc<=r) {
seg[ind].pb(v);
return;
}
int mid = (lc+rc)/2;
add(l, r, v, 2*ind, lc, mid);
add(l, r, v, 2*ind+1, mid, rc);
}
void dfs(int ind=1, int lc=1, int rc=(1<<lg)+1) {
int tmp = size(rolbak);
for(auto it : seg[ind]) {
mark[it] = 1;
for(auto it2 : adj[it]) {
if(mark[it2] == 1) merge(it, it2);
}
}
if(ind >= (1<<lg)) {
int id = ind-(1<<lg)+1;
for(auto it : veca[id]) {cnt[getPar(it)] ++;}
for(auto it : vecb[id]) {
if(cnt[getPar(it)] > 0) vis[it] = 1;
}
for(auto it : veca[id]) cnt[getPar(it)] --;
} else {
int mid = (lc+rc)/2;
dfs(2*ind, lc, mid);
if(mid<=n) dfs(2*ind+1, mid, rc);
}
rbk(tmp);
for(auto it : seg[ind]) mark[it] = 0;
}
void solve() {
cin>>n>>m;
fill(sz, sz+n+1, 1);
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=n; i++) cin>>b[i];
for(int v,u,i=1; i<=m; i++) {
cin>>v>>u;
adj[v].pb(u);
adj[u].pb(v);
}
for(int i=1; i<=n; i++) {
if(a[i] < b[i]) {cout<<0<<endl; return;}
add(b[i], a[i]+1, i);
vecb[b[i]].pb(i);
veca[a[i]].pb(i);
}
dfs(1);
for(int i=1; i<=n; i++) {
if(vis[i] == 0) {
cout<<0<<endl; return ;
}
}
cout<<1<<endl;
}
/*
int t, n, m, a[N], b[N], mark[N], vis[N], par[N], sz[N], cnt[N];
vector<int> adj[N], seg[2*N], veca[N], vecb[N], rolbak;
*/
void clearseg(int ind=1, int lc=1, int rc=(1<<lg)+1) {
seg[ind].clear();
if(ind<(1<<lg)) {
int mid = (lc+rc)/2;
clearseg(2*ind, lc, mid);
if(mid<=n) clearseg(2*ind+1, mid, rc);
}
}
void reset() {
for(int i=0; i<=n+1; i++) {
mark[i] = vis[i] = par[i] = sz[i] = cnt[i] = 0;
adj[i].clear();
veca[i].clear();
vecb[i].clear();
}
rolbak.clear();
clearseg(1);
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>t;
while(t--) {
solve();
reset();
}
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... |