Submission #381768

# Submission time Handle Problem Language Result Execution time Memory
381768 2021-03-25T21:16:46 Z MohamedAhmed04 Colors (RMI18_colors) C++14
0 / 100
212 ms 19200 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 2e5 + 10 ;

int a[MAX] , b[MAX] ;
int n , m ;

vector< vector<int> >adj(MAX) ;
vector<int>v[MAX] ;
set<int>s[MAX] ;
int par[MAX] ;

void init()
{
	for(int i = 1 ; i <= n ; ++i)
		par[i] = i , s[i].insert(a[i]) ; 
}

int root(int node)
{
	if(par[node] == node)
		return node ;
	return (par[node] = root(par[node])) ;
}

void Union(int x , int y)
{
	int a = root(x) ;
	int b = root(y) ;
	if(a == b)
		return ;
	if(s[a].size() < s[b].size())
		swap(a , b) ;
	while(s[b].size())
		s[a].insert(*s[b].begin()) , s[b].erase(s[b].begin()) ;
	par[b] = a ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	int t ;
	cin>>t ;
	while(t--)
	{
		cin>>n>>m ;
		for(int i = 1 ; i <= n ; ++i)
			adj[i].clear() , v[i].clear() , s[i].clear() ;
		for(int i = 1 ; i <= n ; ++i)
			cin>>a[i] ;
		for(int i = 1 ; i <= n ; ++i)
			cin>>b[i] ;
		for(int i = 0 ; i < m ; ++i)
		{
			int x , y ;
			cin>>x>>y ;
			adj[x].push_back(y) ;
			adj[y].push_back(x) ;
		}
		init() ;
		for(int i = 1 ; i <= n ; ++i)
			v[b[i]].push_back(i) ;
		bool flag = true ;
		for(int i = 1 ; i <= n ; ++i)
		{
			for(auto &node : v[i])
			{
				for(auto &child : adj[node])
				{
					if(b[child] <= b[node])
						Union(node , child) ;
				}
			}
			for(auto &node : v[i])
			{
				int a = root(node) ;
				if(s[a].find(i) == s[a].end())
					flag = false ;
			}
		}
		cout<<flag<<"\n" ;
	}
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 19180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 69 ms 19180 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 19200 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 19200 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 19180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 212 ms 19180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 42 ms 19180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 19180 KB Output isn't correct
2 Halted 0 ms 0 KB -