#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);
}
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]){
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
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
20556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
20800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
81 ms |
20540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
81 ms |
20540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
20556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
179 ms |
22248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
19908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
20556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |