// #pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define sz(x) (int)x.size()
#define bit(a, i) ((a>>i)&1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 31;
const int K = 400;
const ll inf = 1e9;
const ll INF = 1e18;
const int mod = 1e9+7;
const int maxn = 2e5 + 10;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
int timer;
int a[maxn];
int b[maxn];
int c[maxn];
int n, m, sz;
int us[maxn];
int tin[maxn];
int t[2][maxn*4];
vector<int>g[maxn];
map<int, int>dp[maxn];
int dfs(int v, int x){
us[v] = sz;
if(a[v] < x) return 0;
if(b[v] > x) return 0;
if(a[v] == x) return 1;
if(dp[v][x] > 0) return dp[v][x]-1;
int ans = 0;
for(int to: g[v]){
if(us[to] < sz && dfs(to, x)) {
ans = 1;
break;
}
} dp[v][x] = ans + 1;
return ans;
}
void dfs1(int v, int pr){
tin[v] = ++timer;
c[tin[v]] = v;
for(int to: g[v]){
if(to != pr)
dfs1(to, v);
}
}
void build(int v=1, int tl=1, int tr=n){
if(tl == tr){
t[0][v] = a[c[tl]];
t[1][v] = b[c[tl]];
} else{
int tm = tl+tr>>1;
build(v<<1, tl, tm);
build(v<<1|1, tm+1, tr);
t[0][v] = max(t[0][v<<1], t[0][v<<1|1]);
t[1][v] = min(t[1][v<<1], t[1][v<<1|1]);
}
}
pii merge(pii a, pii b){
return {max(a.f, b.f), min(a.s, b.s)};
}
pii get(int l, int r, int v=1, int tl=1, int tr=n){
if(l > tr || r < tl) return {inf, 0};
if(l <= tl && tr <= r) return {t[0][v], t[1][v]};
int tm = tl+tr>>1;
return merge(get(l, r, v<<1, tl, tm), get(l, r, v<<1|1, tm+1, tr));
}
int ihtw1(){
sz++;
timer = 0;
for(int i=1; i<=n; i++){
if(sz(g[i]) == 1){
dfs1(i, 0);
break;
}
}
build();
map<int, int>mp;
for(int i=1; i<=n; i++){
if(mp[b[i]]){
pii x = get(mp[b[i]], i);
us[i] = sz * (x.f >= b[i] && x.s <= b[i]);
} mp[a[i]] = i;
} mp.clear();
for(int i=n; i>0; i--){
if(mp[b[i]]){
pii x = get(i, mp[b[i]]);
us[i] = sz * (x.f >= b[i] && x.s <= b[i]);
} mp[a[i]] = i;
} int ans = 1;
for(int i=1; i<=n; i++) ans &= (us[i]==sz);
return ans;
}
void ihtw(){
set<int>st;
cin >> n >> m;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=1; i<=n; i++) {
cin >> b[i];
g[i].clear();
dp[i].clear();
st.insert(a[i]);
}
for(int i=0; i<m; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
} bool ok = 1;
for(int i=1; i<=n; i++){
if(sz(g[i]) > 2) ok = 0;
}
if(m == n-1 && ok) {
cout << ihtw1() << "\n";
return;
}
// if(m == n-1 && sz(st) == n) ihtw2();
for(int i=1; i<=n; i++){
sz++;
if(!dfs(i, b[i])){
cout << "0\n";
return;
}
} cout << "1\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; cin >> t;
while(t--) ihtw();
}
Compilation message
colors.cpp: In function 'void build(int, int, int)':
colors.cpp:64:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int tm = tl+tr>>1;
| ~~^~~
colors.cpp: In function 'pii get(int, int, int, int, int)':
colors.cpp:79:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
79 | int tm = tl+tr>>1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
21164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
21152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
21412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
21412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
21164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
21084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
21084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
21164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |