#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define f first
#define s second
const int maxn = 1e5 + 5e4 + 10;
int n, m, a[maxn], b[maxn];
vector<int> sexo[maxn], q[maxn];
bool ans;
struct edge{
int l, r, u, v;
edge():l(0), r(0), u(0), v(0){}
edge(int l, int r, int u, int v):l(l), r(r), u(u), v(v){}
};
class DSU{
private:
int pai[maxn], sz[maxn];
bool ok[maxn];
stack<pii> s;
public:
void init(int n){ for(int i=1; i<=n; i++) pai[i] = i, sz[i] = 1; }
int find(int x){ return pai[x] == x ? x : find(pai[x]); }
void join(int a, int b){
a = find(a), b = find(b);
if(a == b){
s.push({0, 0});
return;
}
if(sz[a] > sz[b]) swap(a, b);
pai[a] = b;
sz[b] += sz[a];
s.push({a, b});
}
void rollback(){
int a = s.top().f, b = s.top().s;
s.pop();
pai[a] = a;
sz[b] -= sz[a];
}
void att(int x, bool v){ ok[find(x)] = v; }
bool query(int x){ return ok[find(x)]; }
}dsu;
class SEG{
private:
vector<pii> tree[1<<19];
public:
void build(int node, int tl, int tr){
tree[node].clear();
if(tl == tr) return;
int mid = (tl + tr) >> 1;
build(2*node, tl, mid); build(2*node+1, mid+1, tr);
}
void update(int node, int tl, int tr, edge e){
if(tl > e.r or tr < e.l) return;
if(tl >= e.l and tr <= e.r){
tree[node].push_back({e.u, e.v});
return;
}
int mid = (tl + tr) >> 1;
update(2*node, tl, mid, e); update(2*node+1, mid+1, tr, e);
}
void solve(int node, int tl, int tr){
for(pii p : tree[node]) dsu.join(p.f, p.s);
if(tl == tr){
for(int x : sexo[tl]) dsu.att(x, 1);
for(int x : q[tl]) ans &= dsu.query(x);
for(int x : sexo[tl]) dsu.att(x, 0);
}
else{
int mid = (tl + tr) >> 1;
solve(2*node, tl, mid); solve(2*node+1, mid+1, tr);
}
for(pii p : tree[node]) dsu.rollback();
}
}seg;
void reset(){
dsu.init(n);
seg.build(1, 1, n);
for(int i=1; i<=n; i++) sexo[i].clear(), q[i].clear();
ans = 1;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t;
cin >> t;
while(t--){
cin >> n >> m;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<=n; i++) cin >> b[i];
reset();
for(int i=1; i<=n; i++){
if(a[i] < b[i]) ans = 0;
q[b[i]].push_back(i);
sexo[a[i]].push_back(i);
}
for(int i=1; i<=m; i++){
int u, v;
cin >> u >> v;
int r = min(a[u], a[v]), l = max(b[u], b[v]);
if(l > r) continue;
edge e(l, r, u, v);
seg.update(1, 1, n, e);
}
seg.solve(1, 1, n);
cout << ans << "\n";
}
return 0;
}
Compilation message
colors.cpp: In member function 'void SEG::solve(int, int, int)':
colors.cpp:77:11: warning: variable 'p' set but not used [-Wunused-but-set-variable]
77 | for(pii p : tree[node]) dsu.rollback();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
20244 KB |
Output is correct |
2 |
Correct |
42 ms |
20320 KB |
Output is correct |
3 |
Correct |
14 ms |
20024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
20300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
20204 KB |
Output is correct |
2 |
Correct |
45 ms |
20204 KB |
Output is correct |
3 |
Correct |
15 ms |
19908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
20204 KB |
Output is correct |
2 |
Correct |
45 ms |
20204 KB |
Output is correct |
3 |
Correct |
15 ms |
19908 KB |
Output is correct |
4 |
Correct |
153 ms |
22788 KB |
Output is correct |
5 |
Correct |
442 ms |
41424 KB |
Output is correct |
6 |
Correct |
708 ms |
61560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
20244 KB |
Output is correct |
2 |
Correct |
42 ms |
20320 KB |
Output is correct |
3 |
Correct |
14 ms |
20024 KB |
Output is correct |
4 |
Correct |
76 ms |
20204 KB |
Output is correct |
5 |
Correct |
45 ms |
20204 KB |
Output is correct |
6 |
Correct |
15 ms |
19908 KB |
Output is correct |
7 |
Correct |
77 ms |
21100 KB |
Output is correct |
8 |
Correct |
46 ms |
20204 KB |
Output is correct |
9 |
Correct |
15 ms |
20004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
20264 KB |
Output is correct |
2 |
Correct |
440 ms |
41936 KB |
Output is correct |
3 |
Correct |
423 ms |
52392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
20300 KB |
Output is correct |
2 |
Correct |
22 ms |
20420 KB |
Output is correct |
3 |
Correct |
17 ms |
19992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
20244 KB |
Output is correct |
2 |
Correct |
42 ms |
20320 KB |
Output is correct |
3 |
Correct |
14 ms |
20024 KB |
Output is correct |
4 |
Correct |
71 ms |
20300 KB |
Output is correct |
5 |
Correct |
76 ms |
20204 KB |
Output is correct |
6 |
Correct |
45 ms |
20204 KB |
Output is correct |
7 |
Correct |
15 ms |
19908 KB |
Output is correct |
8 |
Correct |
153 ms |
22788 KB |
Output is correct |
9 |
Correct |
442 ms |
41424 KB |
Output is correct |
10 |
Correct |
708 ms |
61560 KB |
Output is correct |
11 |
Correct |
77 ms |
21100 KB |
Output is correct |
12 |
Correct |
46 ms |
20204 KB |
Output is correct |
13 |
Correct |
15 ms |
20004 KB |
Output is correct |
14 |
Correct |
130 ms |
20264 KB |
Output is correct |
15 |
Correct |
440 ms |
41936 KB |
Output is correct |
16 |
Correct |
423 ms |
52392 KB |
Output is correct |
17 |
Correct |
38 ms |
20300 KB |
Output is correct |
18 |
Correct |
22 ms |
20420 KB |
Output is correct |
19 |
Correct |
17 ms |
19992 KB |
Output is correct |
20 |
Correct |
101 ms |
22372 KB |
Output is correct |
21 |
Correct |
435 ms |
44312 KB |
Output is correct |
22 |
Correct |
665 ms |
76472 KB |
Output is correct |