#include <bits/stdc++.h>
#define MAX 150005
using namespace std;
int v1[MAX],v2[MAX];
set<int>cul;
vector<int>aint[4*MAX];
bool activ[MAX];
vector<int>nodes[MAX];
vector<int>G[MAX];
int tata[MAX],rnk[MAX],minim[MAX];
struct str
{
int a,b,c,d;
};
stack<str>stv;
void add(int nod,int st,int dr,int a,int b,int val)
{
if(a<=st && dr<=b)
aint[nod].push_back(val);
else
{
int mij=(st+dr)/2;
if(a<=mij)
add(2*nod,st,mij,a,b,val);
if(b>mij)
add(2*nod+1,mij+1,dr,a,b,val);
}
}
int root(int nod)
{
if(tata[nod])
return root(tata[nod]);
return nod;
}
bool DSU(int nod1,int nod2)
{
int r1=root(nod1);
int r2=root(nod2);
if(r1==r2)
return 0;
if(rnk[r1]<rnk[r2])
swap(r1,r2);
stv.push({r1,rnk[r1],minim[r1],r2});
rnk[r1]=max(rnk[r1],rnk[r2]+1);
tata[r2]=r1;
minim[r1]=min(minim[r1],minim[r2]);
return 1;
}
void dfs(int nod,int st,int dr,bool& rasp)
{
int cnt=0;
for(auto act : aint[nod])
{
activ[act]=1;
for(auto vec : G[act])
if(activ[vec])
cnt+=DSU(act,vec);
}
if(st==dr)
{
for(auto nd : nodes[st])
if(minim[root(nd)]>st)
rasp=0;
}
else
{
int mij=(st+dr)/2;
dfs(2*nod+1,mij+1,dr,rasp);
dfs(2*nod,st,mij,rasp);
}
for(auto act : aint[nod])
activ[act]=0;
while(cnt--)
{
str bck=stv.top(); stv.pop();
rnk[bck.a]=bck.b;
tata[bck.d]=0;
minim[bck.a]=bck.c;
}
}
bool solve()
{
int n,m;
cin>>n>>m;
fill(activ+1,activ+n+1,0);
fill(tata+1,tata+n+1,0);
fill(rnk+1,rnk+n+1,0);
int i;
cul.clear();
for(i=1;i<=n;++i)
{
cin>>v1[i];
cul.insert(v1[i]);
minim[i]=v1[i];
}
for(i=1;i<=n;++i)
cin>>v2[i];
for(i=1;i<=n;++i)
G[i].clear();
for(i=1;i<=m;++i)
{
int a,b;
cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
for(i=1;i<=4*n;++i)
aint[i].clear();
for(i=1;i<=n;++i)
nodes[i].clear();
for(i=1;i<=n;++i)
{
if(cul.find(v2[i])==cul.end() || v1[i]<v2[i])
return 0;
add(1,1,n,v2[i],v1[i],i);
nodes[v2[i]].push_back(i);
}
bool rasp=1;
dfs(1,1,n,rasp);
return rasp;
}
int main()
{
int t;
cin>>t;
while(t--)
cout<<solve()<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
24660 KB |
Output is correct |
2 |
Correct |
32 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
24916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
24708 KB |
Output is correct |
2 |
Correct |
38 ms |
23800 KB |
Output is correct |
3 |
Correct |
8 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
24708 KB |
Output is correct |
2 |
Correct |
38 ms |
23800 KB |
Output is correct |
3 |
Correct |
8 ms |
23388 KB |
Output is correct |
4 |
Correct |
205 ms |
26396 KB |
Output is correct |
5 |
Correct |
458 ms |
42248 KB |
Output is correct |
6 |
Correct |
723 ms |
63556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
24660 KB |
Output is correct |
2 |
Correct |
32 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23640 KB |
Output is correct |
4 |
Correct |
99 ms |
24708 KB |
Output is correct |
5 |
Correct |
38 ms |
23800 KB |
Output is correct |
6 |
Correct |
8 ms |
23388 KB |
Output is correct |
7 |
Correct |
98 ms |
24660 KB |
Output is correct |
8 |
Correct |
37 ms |
23888 KB |
Output is correct |
9 |
Correct |
8 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
26300 KB |
Output is correct |
2 |
Correct |
454 ms |
43232 KB |
Output is correct |
3 |
Correct |
556 ms |
61544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24084 KB |
Output is correct |
2 |
Correct |
16 ms |
23640 KB |
Output is correct |
3 |
Correct |
9 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
24660 KB |
Output is correct |
2 |
Correct |
32 ms |
23900 KB |
Output is correct |
3 |
Correct |
8 ms |
23640 KB |
Output is correct |
4 |
Correct |
76 ms |
24916 KB |
Output is correct |
5 |
Correct |
99 ms |
24708 KB |
Output is correct |
6 |
Correct |
38 ms |
23800 KB |
Output is correct |
7 |
Correct |
8 ms |
23388 KB |
Output is correct |
8 |
Correct |
205 ms |
26396 KB |
Output is correct |
9 |
Correct |
458 ms |
42248 KB |
Output is correct |
10 |
Correct |
723 ms |
63556 KB |
Output is correct |
11 |
Correct |
98 ms |
24660 KB |
Output is correct |
12 |
Correct |
37 ms |
23888 KB |
Output is correct |
13 |
Correct |
8 ms |
23388 KB |
Output is correct |
14 |
Correct |
191 ms |
26300 KB |
Output is correct |
15 |
Correct |
454 ms |
43232 KB |
Output is correct |
16 |
Correct |
556 ms |
61544 KB |
Output is correct |
17 |
Correct |
40 ms |
24084 KB |
Output is correct |
18 |
Correct |
16 ms |
23640 KB |
Output is correct |
19 |
Correct |
9 ms |
23388 KB |
Output is correct |
20 |
Correct |
101 ms |
25684 KB |
Output is correct |
21 |
Correct |
417 ms |
44476 KB |
Output is correct |
22 |
Correct |
642 ms |
68656 KB |
Output is correct |