#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 0, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';
#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILE "LEASTSUMQ"
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e9;
int n, m;
int a[N], b[N];
bool check[N], run[N], ans[N];
vector<int> g[N], to[N], rev[N];
struct per{
int u, szu, v , szv;
};
vector<per> st[N << 2];
struct DSU{
int parent[N], sz[N];
void build(){
FOR(i, 1, n){
parent[i] = i;
sz[i] = 1;
}
}
int found(int u){
return (parent[u] == u ? u : found(parent[u]));
}
void setting(int u, int v, int id){
int U = found(u);
int V = found(v);
if(sz[V] > sz[U]) swap(U, V);
st[id].push_back({U, sz[U], V, sz[V]});
if(U != V){
parent[V] = U;
sz[U] += sz[V];
}
}
void rollback(int id){
while(!st[id].empty()){
int u = st[id].back().u;
int v = st[id].back().v;
int szu = st[id].back().szu;
int szv = st[id].back().szv;
parent[u] = u;
parent[v] = v;
sz[u] = szu;
sz[v] = szv;
st[id].pop_back();
}
}
} dsu;
struct SegmentTree{
vector<int> sg[N << 2];
void update(int id, int l, int r, int u, int v, int p){
if(l > v || r < u){
return;
}
if(u <= l && r <= v){
sg[id].push_back(p);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, p);
update(id << 1 | 1, mid + 1, r, u, v, p);
}
void build(int id, int l, int r){
FOA(u, sg[id]){
run[u] = 1;
}
FOA(u, sg[id]){
FOA(v, g[u]){
if(run[v]){
dsu.setting(u, v, id);
}
}
}
if(l == r){
ans[l] = 1;
FOA(u, rev[l]){
check[dsu.found(u)] = 1;
}
FOA(v, to[l]){
ans[l] &= check[dsu.found(v)];
}
FOA(u, rev[l]){
check[dsu.found(u)] = 0;
}
dsu.rollback(id);
FOA(u, sg[id]){
run[u] = 0;
}
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
dsu.rollback(id);
FOA(u, sg[id]){
run[u] = 0;
}
}
} sg;
void reset(){
FOR(i, 1, n){
g[i].clear();
to[i].clear();
rev[i].clear();
}
FOR(i, 1, (n << 2)){
sg.sg[i].clear();
}
}
bool solve(){
cin >> n >> m;
FOR(i, 1, n){
cin >> a[i];
}
FOR(i, 1, n){
cin >> b[i];
}
FOR(i, 1, n){
to[b[i]].push_back(i);
rev[a[i]].push_back(i);
}
FOR(i, 1, m){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
FOR(i, 1, n){
if(b[i] > a[i]){
return 0;
}
}
FOR(i, 1, n){
sg.update(1, 1, n, b[i], a[i], i);
}
dsu.build();
sg.build(1, 1, n);
bool res = 1;
FOR(i, 1, n){
res = min(res, ans[i]);
}
return res;
}
signed main(){ quickly
if(fopen(FILE".inp", "r")){
freopen(FILE".inp", "r", stdin);
freopen(FILE".out", "w", stdout);
}
int test = 1; cin >> test;
while(test--){
cout << solve() << '\n';
reset();
}
}
컴파일 시 표준 에러 (stderr) 메시지
colors.cpp: In function 'int main()':
colors.cpp:198:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
198 | freopen(FILE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
colors.cpp:199:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
199 | freopen(FILE".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... |