Submission #557937

# Submission time Handle Problem Language Result Execution time Memory
557937 2022-05-06T10:18:47 Z FatihSolak Colors (RMI18_colors) C++17
45 / 100
3000 ms 524288 KB
#include <bits/stdc++.h>
#define N 200005
using namespace std;
int a[N];
int b[N];
vector<int> adj[N];
set<int> s[N];
bool ok[N];
void dfs(int v,int par){
	s[v] = {a[v]};
	for(auto u:adj[v]){
		if(u == par)continue;
		dfs(u,v);
		if(s[v].size() < s[u].size())
			swap(s[v],s[u]);
		for(auto c:s[u])
			s[v].insert(c);
	}
	while(*s[v].begin() < b[v])
		s[v].erase(*s[v].begin());
	while(*s[v].rbegin() > a[v])
		s[v].erase(*s[v].rbegin());
	if(s[v].count(b[v]))
		ok[v] = 1;
}
void solve(){
	int n,m;
	cin >> n >> m;
	for(int i = 1;i<=n;i++){
		cin >> a[i];
		adj[i].clear();
		ok[i] = 0;
	}
	for(int i = 1;i<=n;i++){
		cin >> b[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;
		}
	}
	bool ck = 1;
	for(int i = 1;i<=n;i++){
		if(adj[i].size() > 2)ck = 0;
	}
	if(n < 10)ck = 0;
	for(int i = 1;i<=n;i++){
		if(!ck || (adj[i].size() == 1))
			dfs(i,0);
	}
	for(int i = 1;i<=n;i++){
		if(!ok[i]){
			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 Correct 643 ms 14432 KB Output is correct
2 Correct 659 ms 14752 KB Output is correct
3 Correct 748 ms 14772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 326 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 87 ms 14420 KB Output is correct
2 Correct 39 ms 14548 KB Output is correct
3 Correct 11 ms 14676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 14420 KB Output is correct
2 Correct 39 ms 14548 KB Output is correct
3 Correct 11 ms 14676 KB Output is correct
4 Correct 160 ms 14680 KB Output is correct
5 Correct 371 ms 30908 KB Output is correct
6 Correct 542 ms 53032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 643 ms 14432 KB Output is correct
2 Correct 659 ms 14752 KB Output is correct
3 Correct 748 ms 14772 KB Output is correct
4 Correct 87 ms 14420 KB Output is correct
5 Correct 39 ms 14548 KB Output is correct
6 Correct 11 ms 14676 KB Output is correct
7 Correct 703 ms 14548 KB Output is correct
8 Correct 719 ms 14712 KB Output is correct
9 Correct 1083 ms 14788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2338 ms 14492 KB Output is correct
2 Execution timed out 3037 ms 18740 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 308 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 643 ms 14432 KB Output is correct
2 Correct 659 ms 14752 KB Output is correct
3 Correct 748 ms 14772 KB Output is correct
4 Runtime error 326 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -