#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
//#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
int n,m,a[150005],b[150005],u[150005],v[150005];
bool ans[150005];
vector<int> qry[150005];
struct union_find{
int par[150005],sz[150005];
multiset<int> ms[150005];
stack<ii> st;
int fp(int i){
return (par[i]==i)?i:fp(par[i]);
}
void join(int x,int y){
x=fp(x),y=fp(y);
if(sz(ms[x])<sz(ms[y]))swap(x,y);
par[y]=x;
for(int k:ms[y])ms[x].insert(k);
st.push(ii(x,y));
}
void rollback(int left){
while(left<st.size()){
int x,y;tie(x,y)=st.top();st.pop();
par[y]=y;
for(int k:ms[y])ms[x].erase(ms[x].find(k));
}
}
}ufds;
struct node{
int s,e,m;vii v;
node *l,*r;
node(int _s,int _e){
s=_s;e=_e;m=(s+e)/2;
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void add(int x,int y,ii nv){
if(s==x&&e==y){
v.pb(nv);return;
}
if(y<=m)l->add(x,y,nv);
else if(x>m)r->add(x,y,nv);
else l->add(x,m,nv),r->add(m+1,y,nv);
}
void descend(){
int enter_size=ufds.st.size();
for(ii p:v){
debug("join: %d %d\n",p.fi,p.se);
ufds.join(p.fi,p.se);
}
if(s==e){
for(int x:qry[s]){
int par=ufds.fp(x);
debug("query: %d %d %d %d\n",s,e,x,ufds.ms[par].count(s));
if(ufds.ms[par].count(s))ans[x]=true;
else ans[x]=false;
}
}
else{
l->descend();
r->descend();
}
ufds.rollback(enter_size);
}
}*root;
int main(){
int t;sf("%d",&t);
while(t--){
sf("%d%d",&n,&m);
bool pos=true;
root=new node(1,n);
for(int i=1;i<=n;++i)sf("%d",&a[i]);
for(int i=1;i<=n;++i)sf("%d",&b[i]);
for(int i=0;i<m;++i)sf("%d%d",&u[i],&v[i]);
for(int i=1;i<=n;++i){
if(b[i]>a[i]){
pos=false;
}
ufds.ms[i].insert(a[i]);
ufds.par[i]=i;
qry[b[i]].pb(i);
}
if(!pos){
printf("0\n");
continue;
}
for(int i=0;i<m;++i){
int lo=max(b[u[i]],b[v[i]]);
int hi=min(a[u[i]],a[v[i]]);
debug("edge: %d %d %d %d\n",u[i],v[i],lo,hi);
if(lo>hi)continue;
root->add(lo,hi,ii(u[i],v[i]));
}
root->descend();
for(int i=1;i<=n;++i)pos=pos&&ans[i];
pf("%d\n",pos);
for(int i=1;i<=n;++i)qry[i].clear();
}
}
/*
2
4 4
3 3 2 1
2 1 2 1
1 2
2 3
3 4
4 2
4 4
3 3 2 1
1 2 2 1
1 2
2 3
3 4
4 2
*/
Compilation message
colors.cpp: In member function 'void union_find::rollback(int)':
colors.cpp:60:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::stack<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while(left<st.size()){
| ~~~~^~~~~~~~~~
colors.cpp: In function 'int main()':
colors.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | int t;sf("%d",&t);
| ^
colors.cpp:112:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | sf("%d%d",&n,&m);
| ^
colors.cpp:115:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | for(int i=1;i<=n;++i)sf("%d",&a[i]);
| ^
colors.cpp:116:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | for(int i=1;i<=n;++i)sf("%d",&b[i]);
| ^
colors.cpp:117:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | for(int i=0;i<m;++i)sf("%d%d",&u[i],&v[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3074 ms |
16684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3118 ms |
438764 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3084 ms |
16948 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3084 ms |
16948 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3074 ms |
16684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3073 ms |
19416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3116 ms |
443632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3074 ms |
16684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |