#include <bits/stdc++.h>
#define N 200005
using namespace std;
int a[N];
int b[N];
vector<int> adj[N];
set<int> s[N];
vector<int> v[N];
int par[N];
int find(int a){
if(a == par[a])return a;
return par[a] = find(par[a]);
}
void merge(int a,int b){
a = find(a);
b = find(b);
if(a == b)return;
if(s[a].size() > s[b].size())
swap(s[a],s[b]);
for(auto u:s[a])
s[b].insert(u);
s[a].clear();
par[a] = b;
}
void solve(){
int n,m;
cin >> n >> m;
for(int i = 1;i<=n;i++){
cin >> a[i];
adj[i].clear();
par[i] = i;
s[i] = {a[i]};
v[i].clear();
}
for(int i = 1;i<=n;i++){
cin >> b[i];
v[b[i]].push_back(i);
}
for(int i = 1;i<=m;i++){
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
s[u].insert(a[v]);
s[v].insert(a[u]);
}
for(int i = 1;i<=n;i++){
if(a[i] < b[i]){
cout << 0 << endl;
return;
}
}
for(int i = 1;i<=n;i++){
if(v[i].empty())continue;
for(auto u:v[i]){
for(auto c:adj[u]){
if(b[c] <= b[u] && a[c]>=i){
merge(u,c);
}
}
}
for(auto u:v[i]){
//cout << u << " " << find(u) << endl;
if(s[find(u)].count(i) == 0){
cout << 0 << endl;
return;
}
}
}
cout << 1 << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds.";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
19044 KB |
Output is correct |
2 |
Correct |
35 ms |
19720 KB |
Output is correct |
3 |
Correct |
13 ms |
19400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
19276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
123 ms |
19136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
123 ms |
19136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
19044 KB |
Output is correct |
2 |
Correct |
35 ms |
19720 KB |
Output is correct |
3 |
Correct |
13 ms |
19400 KB |
Output is correct |
4 |
Incorrect |
123 ms |
19136 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
211 ms |
19160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
71 ms |
19204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
19044 KB |
Output is correct |
2 |
Correct |
35 ms |
19720 KB |
Output is correct |
3 |
Correct |
13 ms |
19400 KB |
Output is correct |
4 |
Correct |
133 ms |
19276 KB |
Output is correct |
5 |
Incorrect |
123 ms |
19136 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |