// Esti <3
//\
šťastia pre nás :)
// you're already the best
// _
// ^ ^ //
// >(O_O)<___//
// \ __ __ \
// \\ \\ \\\\
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3","unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("O3")
#pragma GCC target("popcnt")
using ll = long long;
#define int long long
#define forn(i,n) for(int i=0; i<(n); ++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
#define vii(a,n) vector<int> a(n); forn(i,n) cin>>a[i];
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int inf = 1e18;
const int mod = 998244353;
// \
\
:smiling_face_with_3_hearts: :smiling_face_with_3_hearts: :smiling_face_with_3_hearts:
//vidime sa veľmi skoro, moje slnko
const int N = 2e5+5;
int n,m;
int a[N], b[N];
pi e[N];
int is[N], r[N];
vector<pi> adj[N];
struct DSU {
vector<int> p,sz,mn;
vector<pi> rb;
void init(int n) {
p.clear(); sz.clear(); mn.clear(); rb.clear();
forn(i,n) p.pb(i), sz.pb(1), mn.pb(a[i]);
}
int get(int u) {
return p[u]==u?u:get(p[u]);
}
void uni(int u, int v) {
u=get(u), v=get(v);
if (u==v) {
rb.pb({-1,-1}); return;
}
if (sz[u]<sz[v]) swap(u,v);
p[v]=u;
sz[u]+=sz[v];
rb.pb({v,mn[u]});
mn[u]=min(mn[u],mn[v]);
}
void roll() {
auto it = rb.back(); rb.pop_back();
int v = it.f, old = it.s;
int u = p[v];
p[v] = v;
sz[u] -= sz[v];
mn[u] = old;
}
};
struct sgt {
vector<vector<int>> t; int sz=1;
int l,r,v;
DSU dsu;
sgt(int n) {
sz=1;
while (sz<n) sz<<=1;
t.resize(2*sz);
l=0, r=sz, v=0;
dsu.init(n);
}
void add(int v, int l, int r, int lx, int rx, int i) {
if (rx<=l || r<=lx) return;
if (lx<=l && r<=rx) {
t[v].pb(i); return;
}
int m=(l+r)>>1;
add(2*v+1,l,m,lx,rx,i);
add(2*v+2,m,r,lx,rx,i);
}
void add(int l, int r, int i) {
add(0,0,sz,l,r,i);
}
void go(int i) {
while (r<=i) {
if (v&1) r+=r-l;
else l-=r-l;
for(auto&x:t[v]) dsu.roll();
v = (v-1)>>1;
}
while (r-l > 1) {
int m = (l+r)>>1;
if (i<m) {
r=m;
v=2*v+1;
} else {
l=m;
v=2*v+2;
}
for(auto&x:t[v]) dsu.uni(e[x].f, e[x].s);
}
}
};
void solve() {
cin>>n>>m;
forn(i,n) cin>>a[i];
forn(i,n) cin>>b[i];
forn(i,m) cin>>e[i].f>>e[i].s;
forn(i,n) {
if (a[i]<b[i]) {
cout<<"0\n"; return;
}
}
forn(i,n) adj[i].clear();
forn(i,m) is[i]=r[i]=0;
sgt st(2*n);
forn(i,m) {
--e[i].f, --e[i].s;
int u=e[i].f, v=e[i].s;
adj[u].pb({v,i});
adj[v].pb({u,i});
}
vector<vector<int>> A(n+1), B(n+1);
forn(i,n) A[a[i]].pb(i);
forn(i,n) B[b[i]].pb(i);
for (int i=n; i>0; --i) {
for(auto&u:A[i]) {
for(auto&e:adj[u]) {
int v = e.s, j = e.s;
if (is[j]>=2) continue;
is[j]++;
if (is[j]==2) r[j] = i+1;
//cout<<"! "<<j<<' '<<i+1<<'\n';
}
}
for(auto&u:B[i]) {
for(auto&e:adj[u]) {
int v = e.s, j = e.s;
if (is[j]<2) {
is[j]=4; continue;
}
if (is[j]>2) continue;
is[j]=3;
st.add(i, r[j], j);
//cout<<j<<": "<<::e[j].f<<' '<<::e[j].s<<" "<<i<<' '<<r[j]<<'\n';
}
}
}
for (int i=1; i<=n; ++i) {
st.go(i);
//cout<<"! "<<i<<": ";
//forn(j,n) cout<<dsu.get(j)<<' '; cout<<'\n';
for(auto&u:B[i]) {
int p = st.dsu.get(u);
int m = st.dsu.mn[p];
if (m != i) {
cout<<"0\n"; return;
}
}
}
cout<<"1\n";
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t=1;
cin>>t;
while (t--) solve();
return 0;
}
Compilation message
colors.cpp:3:1: warning: multi-line comment [-Wcomment]
3 | //\
| ^
colors.cpp:9:1: warning: multi-line comment [-Wcomment]
9 | // \ __ __ \
| ^
colors.cpp:36:1: warning: multi-line comment [-Wcomment]
36 | // \
| ^
colors.cpp: In member function 'void sgt::go(long long int)':
colors.cpp:109:13: warning: unused variable 'x' [-Wunused-variable]
109 | for(auto&x:t[v]) dsu.roll();
| ^
colors.cpp: In function 'void solve()':
colors.cpp:157:9: warning: unused variable 'v' [-Wunused-variable]
157 | int v = e.s, j = e.s;
| ^
colors.cpp:166:9: warning: unused variable 'v' [-Wunused-variable]
166 | int v = e.s, j = e.s;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
10840 KB |
Output is correct |
2 |
Correct |
24 ms |
11100 KB |
Output is correct |
3 |
Correct |
5 ms |
11408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
21592 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
10952 KB |
Output is correct |
2 |
Correct |
28 ms |
10840 KB |
Output is correct |
3 |
Correct |
4 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
10952 KB |
Output is correct |
2 |
Correct |
28 ms |
10840 KB |
Output is correct |
3 |
Correct |
4 ms |
11100 KB |
Output is correct |
4 |
Correct |
150 ms |
10840 KB |
Output is correct |
5 |
Correct |
320 ms |
38608 KB |
Output is correct |
6 |
Runtime error |
77 ms |
87928 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
10840 KB |
Output is correct |
2 |
Correct |
24 ms |
11100 KB |
Output is correct |
3 |
Correct |
5 ms |
11408 KB |
Output is correct |
4 |
Correct |
74 ms |
10952 KB |
Output is correct |
5 |
Correct |
28 ms |
10840 KB |
Output is correct |
6 |
Correct |
4 ms |
11100 KB |
Output is correct |
7 |
Correct |
76 ms |
10840 KB |
Output is correct |
8 |
Correct |
28 ms |
10840 KB |
Output is correct |
9 |
Correct |
6 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
11092 KB |
Output is correct |
2 |
Correct |
340 ms |
42552 KB |
Output is correct |
3 |
Runtime error |
76 ms |
87976 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
21848 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
10840 KB |
Output is correct |
2 |
Correct |
24 ms |
11100 KB |
Output is correct |
3 |
Correct |
5 ms |
11408 KB |
Output is correct |
4 |
Runtime error |
10 ms |
21592 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |