#include <bits/stdc++.h>
#define DIM 150010
#define INF 2000000000
using namespace std;
vector <int> L[DIM];
int a[DIM],b[DIM],f[DIM];
int t,n,m,i,j,x,y;
int main (){
// ifstream cin ("colors2.in");
// ofstream cout ("colors2.out");
cin>>t;
for (int q=1;q<=t;q++){
if (q == 36)
q = 36;
cin>>n>>m;
for (i=1;i<=n;i++){
L[i].clear();
f[i] = 0;
}
for (i=1;i<=n;i++){
cin>>a[i];
f[a[i]] = 1;
}
int ok = 1;
for (i=1;i<=n;i++){
cin>>b[i];
if (b[i] > a[i] || !f[b[i]])
ok = 0;
}
for (i=1;i<=m;i++){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
if (!ok){
cout<<0<<"\n";
continue;
}
if (n == 1 && !m){
if (a[1] == b[1])
cout<<"1\n";
else cout<<"0\n";
continue;
}
if (n == 2 && m == 1){
if ( (a[1] == b[1] && a[2] == b[2]) || (min (a[1],a[2]) == b[1] && min (a[1],a[2]) == b[2]) )
cout<<"1\n";
else cout<<"0\n";
continue;
}
/// graf stea
int nod = 0, cnt = 0;
for (i=1;i<=n;i++){
if (L[i].size() == 1)
cnt++;
else nod = i;
}
if (cnt == n-1){
int maxi = 0, mini = n+1;
for (i=1;i<=n;i++){
if (b[i] == a[i])
continue;
maxi = max (maxi,b[i]);
if (i != nod)
mini = min (mini,b[i]);
}
if (maxi && (maxi > a[nod] || mini < b[nod])){
cout<<"0\n";
continue;
}
/// acum sa vad daca exista b[nod]
int ok = 0;
if (!maxi)
ok = 1;
for (i=1;i<=n;i++)
if (i != nod && b[i] == b[nod]){
ok = 1;
break;
}
cout<<ok<<"\n";
continue;
} else cout<<INF<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
3968 KB |
Output is correct |
2 |
Correct |
61 ms |
4480 KB |
Output is correct |
3 |
Correct |
12 ms |
3968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
162 ms |
3840 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
159 ms |
3976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
159 ms |
3976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
3968 KB |
Output is correct |
2 |
Correct |
61 ms |
4480 KB |
Output is correct |
3 |
Correct |
12 ms |
3968 KB |
Output is correct |
4 |
Incorrect |
159 ms |
3976 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
345 ms |
4036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
79 ms |
3840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
3968 KB |
Output is correct |
2 |
Correct |
61 ms |
4480 KB |
Output is correct |
3 |
Correct |
12 ms |
3968 KB |
Output is correct |
4 |
Incorrect |
162 ms |
3840 KB |
Output isn't correct |