Submission #873454

# Submission time Handle Problem Language Result Execution time Memory
873454 2023-11-15T06:15:41 Z vjudge1 Colors (RMI18_colors) C++17
7 / 100
104 ms 33768 KB
#include <bits/stdc++.h>
#define f first
#define S second
#define pb push_back
#define msk(x , y) ((x >> y) & 1)
#define all(x) x.begin() , x.end()
using namespace std;
typedef long long int ll;
const int N = 5e5 + 7;
const ll mod = 1e18;
const int dx[] = {-1,-1,1,1,2,-2,2,-2};
const int dy[] = {-2,2,2,-2,1,1,-1,-1};

int n , m , a[N] , b[N] , d[N] , c[N] , t[N][25] , lg[N] , tim = 1;
vector <int > g[N],e[N];

void dfs(int v){
	d[v] = 1;
	c[tim] = v ;
	tim++;
	for(auto to : g[v]){
		if(d[to]) continue;
		dfs(to);
	}
}

void table(){
	lg[1] = 0 ;
    for(int i = 2 ; i <= n ; i++) lg[i] = lg[i / 2] + 1;
    for(int i = 1 ; i <= n ; i++) t[0][i] = d[i];
    for(int i = 1 ; (1 << i) <= n; i++){
        for(int j = 1 ; j + (1 << i) - 1 <= n ; j++){
            t[i][j] = min(t[i - 1][j] , t[i - 1][j + (1 <<(i - 1))]);
        }
    }
}

int get(int l , int r){
    int k = lg[r - l + 1];
    return min(t[k][l] , t[k][r - (1 << k) + 1]);
}
void solve(){
	cin >> n >> m ;
	for(int i = 0 ; i <= n; i++) g[i].clear() , c[i] = 0 , e[i].clear();
	for(int i = 1 ; i <= n; i++)cin >> a[i];
	for(int i = 1 ; i <= n; i++)cin >> b[i];
	int rt = 0 ;
	for(int i = 1 ; i <= m; i++){
		int u , v;
		cin >> u >> v ;
		g[u].pb(v);
		g[v].pb(u);
	}
	for(int i = 1 ; i <= n ;i++){
		if(g[i].size() == 1) rt = i;
	}
	for(int i = 1 ; i <= n ;i++){
		if(a[i] < b[i]){
			cout << "0\n";
			return ;
		}
		d[a[i]] = 1;
	}
	for(int i = 1 ; i <= n; i++){
		if(d[b[i]])continue ;
		cout << "0\n";
		return ;
	}
	for(int i = 1 ; i <= n ;i++) d[i] = 0;
	dfs(rt);
	for(int i = 1 ; i <= n ;i++){
		d[i] = b[c[i]];
		c[i] = a[c[i]];
	}
	for(int i = 1 ; i <= n ; i++){
		e[c[i]].pb(i);
	}
	table();
	for(int i = 1 ; i <= n ;i++){
		bool ok = 0 ;
		for(auto to : e[d[i]]){
			if(get(to , i) <= d[i]) ok = 1 ;
		}
		if(!ok){
			cout << "0\n";
			return ;
		}
	}
	cout << "1\n";
}	
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);  
    cout.tie(0);
    int test=1;
    cin >> test ;
    for(int i=1;i<=test;i++){
//      cout << "Case " << i << ": ";
        solve();
     }
}
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 33372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 48 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 48 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 104 ms 33768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 33368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 33372 KB Output isn't correct
2 Halted 0 ms 0 KB -