/* ok this problem is marvelous */
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define sz() size()
#define fr first
#define sc second
#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
using namespace std;
int t,n,m,a[150005],b[150005],par[150005],siz[150005],ans;
vector<pair<pair<int,int>,int>>DSU;
vector<pair<int,int>>tree[4*150005];
vector<int>A[150005],B[150005];
vector<int>nod[150005];
bitset<150005>viz;
void update(int l,int r,int le,int ri,pair<int,int>pi,int nod){
if(l>ri || r<le) return;
else if(l>=le && r<=ri) tree[nod].push_back(pi);
else{
int mid=l+(r-l)/2;
update(l,mid,le,ri,pi,2*nod);
update(mid+1,r,le,ri,pi,2*nod+1);
}
}
int findpar(int x){
if(x!=par[x]){
DSU.push_back({{x,par[x]},0});
par[x]=findpar(par[x]);
}
return par[x];
}
int join(int x,int y){
int x1=findpar(x);
int y1=findpar(y);
if(x1!=y1){
if(siz[x1]<siz[y1]) swap(x1,y1);
DSU.push_back({{y1,par[y1]},siz[y1]});
par[y1]=x1;
siz[x1]+=siz[y1];
}
}
void remov(int x){
while(DSU.size()>x){
auto it=DSU.back();
DSU.pop_back();
siz[par[it.fr.fr]]-=it.sc;
par[it.fr.fr]=it.fr.sc;
}
}
void solve(int l,int r,int nod){
int sz=DSU.size();
for(auto it:tree[nod]){
join(it.fr,it.sc);
}
if(l==r){
for(auto it:A[l]) viz[findpar(it)]=1;
for(auto it:B[l]){
if(viz[findpar(it)]==0) ans=0;
}
for(auto it:A[l]) viz[findpar(it)]=0;
}
else{
int mid=l+(r-l)/2;
solve(l,mid,2*nod);
solve(mid+1,r,2*nod+1);
}
remov(sz);
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
srand(chrono::steady_clock::now().time_since_epoch().count());
cin >> t;
while(t--){
ans=1;
cin >> n >> m;
for(int i=1;i<=n;i++) par[i]=i,siz[i]=1;
for(int i=1;i<=n;i++){
cin >> a[i];
A[a[i]].push_back(i);
}
for(int i=1;i<=n;i++){
cin >> b[i];
B[b[i]].push_back(i);
}
for(int i=1;i<=m;i++){
int x,y;
cin >> x >> y;
nod[x].push_back(y);
nod[y].push_back(x);
if(max(b[x],b[y])<=min(a[x],a[y])) update(1,n,max(b[x],b[y]),min(a[x],a[y]),{x,y},1);
}
solve(1,n,1);
cout << ans << '\n';
DSU.clear();
for(int i=1;i<=n;i++){
par[i]=0,siz[i]=0;
A[i].clear();
B[i].clear();
nod[i].clear();
}
for(int i=1;i<=4*n;i++) tree[i].clear();
}
}
Compilation message
colors.cpp: In function 'long long int join(long long int, long long int)':
colors.cpp:50:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
colors.cpp: In function 'void remov(long long int)':
colors.cpp:53:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(DSU.size()>x){
~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
25088 KB |
Output is correct |
2 |
Correct |
41 ms |
25344 KB |
Output is correct |
3 |
Correct |
24 ms |
25728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
25464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
25088 KB |
Output is correct |
2 |
Correct |
43 ms |
25208 KB |
Output is correct |
3 |
Correct |
23 ms |
25600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
25088 KB |
Output is correct |
2 |
Correct |
43 ms |
25208 KB |
Output is correct |
3 |
Correct |
23 ms |
25600 KB |
Output is correct |
4 |
Correct |
173 ms |
25088 KB |
Output is correct |
5 |
Correct |
575 ms |
55192 KB |
Output is correct |
6 |
Correct |
1020 ms |
93848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
25088 KB |
Output is correct |
2 |
Correct |
41 ms |
25344 KB |
Output is correct |
3 |
Correct |
24 ms |
25728 KB |
Output is correct |
4 |
Correct |
83 ms |
25088 KB |
Output is correct |
5 |
Correct |
43 ms |
25208 KB |
Output is correct |
6 |
Correct |
23 ms |
25600 KB |
Output is correct |
7 |
Correct |
87 ms |
25208 KB |
Output is correct |
8 |
Correct |
44 ms |
25344 KB |
Output is correct |
9 |
Correct |
24 ms |
25600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
25216 KB |
Output is correct |
2 |
Correct |
585 ms |
60260 KB |
Output is correct |
3 |
Correct |
634 ms |
73164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
25344 KB |
Output is correct |
2 |
Correct |
30 ms |
26284 KB |
Output is correct |
3 |
Correct |
24 ms |
25600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
25088 KB |
Output is correct |
2 |
Correct |
41 ms |
25344 KB |
Output is correct |
3 |
Correct |
24 ms |
25728 KB |
Output is correct |
4 |
Correct |
91 ms |
25464 KB |
Output is correct |
5 |
Correct |
83 ms |
25088 KB |
Output is correct |
6 |
Correct |
43 ms |
25208 KB |
Output is correct |
7 |
Correct |
23 ms |
25600 KB |
Output is correct |
8 |
Correct |
173 ms |
25088 KB |
Output is correct |
9 |
Correct |
575 ms |
55192 KB |
Output is correct |
10 |
Correct |
1020 ms |
93848 KB |
Output is correct |
11 |
Correct |
87 ms |
25208 KB |
Output is correct |
12 |
Correct |
44 ms |
25344 KB |
Output is correct |
13 |
Correct |
24 ms |
25600 KB |
Output is correct |
14 |
Correct |
151 ms |
25216 KB |
Output is correct |
15 |
Correct |
585 ms |
60260 KB |
Output is correct |
16 |
Correct |
634 ms |
73164 KB |
Output is correct |
17 |
Correct |
53 ms |
25344 KB |
Output is correct |
18 |
Correct |
30 ms |
26284 KB |
Output is correct |
19 |
Correct |
24 ms |
25600 KB |
Output is correct |
20 |
Correct |
119 ms |
26024 KB |
Output is correct |
21 |
Correct |
604 ms |
67864 KB |
Output is correct |
22 |
Correct |
1075 ms |
129200 KB |
Output is correct |