Submission #604689

# Submission time Handle Problem Language Result Execution time Memory
604689 2022-07-25T08:47:19 Z l_reho Stranded Far From Home (BOI22_island) C++14
100 / 100
875 ms 47504 KB
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
 
struct info{
	int node;
	ll people;
	
	bool operator <(const info &i) const{
		return people > i.people;
	}
	
};
 
int N, M;
 
vector<ll> A;
vector<pair<ll, int>> A1;
vector<bool> taken, vis;
vector<vector<int>> graph;
vector<ll> P;
vector<ll> S;
vector<ll> R;
vector<set<int>> sets;
vector<int> parent;

void dfs(int curr){
	vis[curr] = true;
	
	vector<int> adj = graph[curr];
	
	P[curr] = A[curr];
	
	for(int a : adj){
		if(vis[a]) continue;
		dfs(a);
		
		P[curr] += P[a];
		
	}
}
void solver(int curr, int p, vector<int> &ans){
	vis[curr] = true;
	vector<int> adj = graph[curr];
	
	if((int)adj.size() == 1 && curr){
		if(P[curr] >= A[p]){
			ans[curr] = 1;
		}else{
			ans[curr] = 0;
		}
		return;
	}
	
	for(int a : adj){
		if(vis[a]) continue;
		
		if(curr){
			if(P[curr] >= A[p]){
				ans[curr] = 1;
			}else{
				ans[curr] = 0;
				continue;
			}
		}
		
		solver(a, curr, ans);
		
	}
	
}

int getParent(int a){
	if(parent[a] == a) return a;
	return parent[a] = getParent(parent[a]);
}

void unionFind(int a, int b, vector<int> &ans){
	int x = getParent(a);
	int y = getParent(b);
	
	if(x == y) return;
	
	if(S[y] < A[a]){
		for(int c : sets[y])
			ans[c] = 0;
		sets[y].clear();
		
	}
	
	if(R[x] < R[y]) swap(x, y);

	parent[y] = x;
	
	S[x] += S[y];
	R[x] += R[y];
	
	for(int c : sets[y])
		sets[x].insert(c);
	
	S[y] = 0;
	sets[y].clear();
	// rank[y] = rank[x];
	
}
 
int dfs1(int curr, vector<int> &ans){
	vis[curr] = true;
	
	if(ans[curr] == 0) return 0; 
	
	vector<int> adj = graph[curr];
	
	for(int a : adj){
		if(vis[a] || A[a] < A[curr]) continue;
		
		int ret = dfs1(a, ans);
		
		if(ret == 0)
			ans[curr] = 0;
			
	}
	
	return 1;
	
} 

void solve(){
	cin>>N>>M;
	
	A.assign(N, 0);
	A1.assign(N, pair<ll, int>());
	
	P.assign(N, 0);
	
	graph.assign(N, vector<int>());
	vis.assign(N, false);
	
	parent.assign(N, false);
	
	
	R.assign(N, 1);
	S.assign(N, 0);
	sets.assign(N, set<int>());
	
	
	for(int i = 0; i < N; i++){
		int v;
		cin>>v;
		A[i] = v;
		A1[i] = {v, i};
		S[i] = v;
		parent[i] = i;
		sets[i].insert(i);
	}
	
		
	for(int i = 0; i < M; i++){
		int a, b;
		cin>>a>>b;
		a--;
		b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
		
		
	}
	
	sort(A1.begin(), A1.end());
	
	vector<int> ans(N, 1);
	
	
	for(int i = 0; i < N; i++){
		int color = A1[i].second;
		ll abitants = A1[i].first;
		
		vector<int> adj = graph[color];
		ll mn = LLONG_MAX;
		
		for(int a : adj){
			if(A[a] > abitants){
				if(A[a] < mn) mn = A[a];
				continue;
			}
			
			
			unionFind(color, a, ans);
			// cout<<color<<" "<<a<<endl;	
		}		
	
	
	}
	
	for(int a : ans) cout<<a;
	cout<<endl;
	
	/*
	bool subtask3 = true;
	
	for(int i = 0; i < M; i++){
		int a, b;
		cin>>a>>b;
		a--;
		b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
		
		subtask3 &= abs(a-b) == 1;
	}
	vector<int> ans(N, 2);
	
	
	ll sum = accumulate(A.begin(), A.end(), 0LL);
	
	bool subtask1 = (N <= 2000 && M <= 2000);
	
	if(subtask1){
		int last_color = N;
		
		for(int color = 0; color < last_color; color++){
			if(ans[color] != 2) continue;
			priority_queue<info> pq;
			// inizializzo la pq
			vector<int> adj = graph[color];
			
			taken.assign(N, false);
			taken[color] = true;
			
			for(int a : adj)
				pq.push({a, A[a]});
			
			ll total = A[color];
			stack<int> nodes;
			
			while(!pq.empty()){
				info i = pq.top();
				
				pq.pop();
				
				int node = i.node;
				ll abitants = i.people;
				
				if(taken[node]) continue;
				
				taken[node] = true;
				
				if(abitants > total)
					break;
				
				if(ans[node] == 1){
					total = sum;
					break;
				}
					
				nodes.push(node);
				total += abitants;
				vector<int> adj = graph[node];
 
				for(int a : adj){
					if(taken[a]) continue;
					pq.push({a, A[a]});
				}
				
			}
			// se ans[color] è false, allora saranno false anche tutti quelli
			// che sono stati convinti durante il processo e quindi è inutile
			// ricalcolarli
			ans[color] = total == sum;
			if(!ans[color]){
				while(!nodes.empty()){
					ans[nodes.top()] = 0;
					nodes.pop();
				}
			}
		}
		
		for(int i = 0; i < N; i++) cout<<ans[i];
		cout<<endl;
		return;
	}
 
	if(subtask3){
		vector<ll> pref_sum(N+1, 0);
		
		for(int i = 0; i < N; i++){
			pref_sum[i+1] += pref_sum[i] + A[i];
		}
		
		set<int> s;
		ans[A1[0].second] = 1;
		s.insert(A1[0].second);
		for(int i = 1; i < N; i++){
			int color = A1[i].second;
			// mi serve il primo abitante sulla sinistra già preso e 
			// il primo abitante sulla destra già preso cioè ans[i] = true;
			auto r = s.lower_bound(color);
			auto l = r;
			int r_idx = 0;
			int l_idx = 0;
			
			if(l == s.begin()){
				l_idx = -1;
			}else l_idx = *(--l);
			
			
			if(r == s.end()){
				r_idx = N;
			}else r_idx = *r;
			
			// cout<<"DEBUG-->"<<color<<" "<<l_idx<<" "<<r_idx<<endl;
			if(l_idx == -1)
				ans[color] = (pref_sum[r_idx] - pref_sum[l_idx+1]) >= A[r_idx];
			else if(r_idx == N)
				ans[color] = (pref_sum[r_idx] - pref_sum[l_idx+1]) >= A[l_idx];
			else ans[color] = (pref_sum[r_idx] - pref_sum[l_idx+1]) >= min(A[r_idx], A[l_idx]);
			
			if(ans[color])
				s.insert(color);
		}
		
		for(int i = 0; i < N; i++) cout<<ans[i];
		cout<<endl;
		return;
	}
 
	dfs(0);
 
	vis.assign(N, false);
	ans.assign(N, false);
	
	ans[0] = 1;
	
	solver(0, -1, ans);
	
	for(int i = 0; i < N; i++) cout<<ans[i];
	cout<<endl;
	
	*/
	
}
 
int main(){
	solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 6 ms 672 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 4 ms 540 KB Output is correct
8 Correct 3 ms 584 KB Output is correct
9 Correct 3 ms 692 KB Output is correct
10 Correct 5 ms 704 KB Output is correct
11 Correct 4 ms 696 KB Output is correct
12 Correct 3 ms 724 KB Output is correct
13 Correct 3 ms 648 KB Output is correct
14 Correct 4 ms 692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 465 ms 41172 KB Output is correct
4 Correct 392 ms 41172 KB Output is correct
5 Correct 546 ms 41376 KB Output is correct
6 Correct 609 ms 43124 KB Output is correct
7 Correct 550 ms 43920 KB Output is correct
8 Correct 597 ms 43856 KB Output is correct
9 Correct 458 ms 43616 KB Output is correct
10 Correct 376 ms 45612 KB Output is correct
11 Correct 520 ms 45500 KB Output is correct
12 Correct 483 ms 43172 KB Output is correct
13 Correct 330 ms 42504 KB Output is correct
14 Correct 431 ms 43576 KB Output is correct
15 Correct 403 ms 44768 KB Output is correct
16 Correct 347 ms 43412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 613 ms 40956 KB Output is correct
3 Correct 567 ms 43780 KB Output is correct
4 Correct 377 ms 45368 KB Output is correct
5 Correct 367 ms 43892 KB Output is correct
6 Correct 561 ms 44508 KB Output is correct
7 Correct 478 ms 44668 KB Output is correct
8 Correct 426 ms 44484 KB Output is correct
9 Correct 322 ms 43276 KB Output is correct
10 Correct 389 ms 42920 KB Output is correct
11 Correct 377 ms 44012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 640 ms 40980 KB Output is correct
3 Correct 630 ms 45276 KB Output is correct
4 Correct 701 ms 42764 KB Output is correct
5 Correct 792 ms 46620 KB Output is correct
6 Correct 725 ms 43084 KB Output is correct
7 Correct 425 ms 46892 KB Output is correct
8 Correct 281 ms 44420 KB Output is correct
9 Correct 296 ms 23944 KB Output is correct
10 Correct 760 ms 46676 KB Output is correct
11 Correct 380 ms 43160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 6 ms 672 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 4 ms 540 KB Output is correct
8 Correct 3 ms 584 KB Output is correct
9 Correct 3 ms 692 KB Output is correct
10 Correct 5 ms 704 KB Output is correct
11 Correct 4 ms 696 KB Output is correct
12 Correct 3 ms 724 KB Output is correct
13 Correct 3 ms 648 KB Output is correct
14 Correct 4 ms 692 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 465 ms 41172 KB Output is correct
18 Correct 392 ms 41172 KB Output is correct
19 Correct 546 ms 41376 KB Output is correct
20 Correct 609 ms 43124 KB Output is correct
21 Correct 550 ms 43920 KB Output is correct
22 Correct 597 ms 43856 KB Output is correct
23 Correct 458 ms 43616 KB Output is correct
24 Correct 376 ms 45612 KB Output is correct
25 Correct 520 ms 45500 KB Output is correct
26 Correct 483 ms 43172 KB Output is correct
27 Correct 330 ms 42504 KB Output is correct
28 Correct 431 ms 43576 KB Output is correct
29 Correct 403 ms 44768 KB Output is correct
30 Correct 347 ms 43412 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 613 ms 40956 KB Output is correct
33 Correct 567 ms 43780 KB Output is correct
34 Correct 377 ms 45368 KB Output is correct
35 Correct 367 ms 43892 KB Output is correct
36 Correct 561 ms 44508 KB Output is correct
37 Correct 478 ms 44668 KB Output is correct
38 Correct 426 ms 44484 KB Output is correct
39 Correct 322 ms 43276 KB Output is correct
40 Correct 389 ms 42920 KB Output is correct
41 Correct 377 ms 44012 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 640 ms 40980 KB Output is correct
44 Correct 630 ms 45276 KB Output is correct
45 Correct 701 ms 42764 KB Output is correct
46 Correct 792 ms 46620 KB Output is correct
47 Correct 725 ms 43084 KB Output is correct
48 Correct 425 ms 46892 KB Output is correct
49 Correct 281 ms 44420 KB Output is correct
50 Correct 296 ms 23944 KB Output is correct
51 Correct 760 ms 46676 KB Output is correct
52 Correct 380 ms 43160 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 4 ms 660 KB Output is correct
57 Correct 4 ms 724 KB Output is correct
58 Correct 3 ms 596 KB Output is correct
59 Correct 3 ms 596 KB Output is correct
60 Correct 3 ms 540 KB Output is correct
61 Correct 3 ms 724 KB Output is correct
62 Correct 4 ms 724 KB Output is correct
63 Correct 4 ms 692 KB Output is correct
64 Correct 5 ms 724 KB Output is correct
65 Correct 3 ms 724 KB Output is correct
66 Correct 3 ms 724 KB Output is correct
67 Correct 357 ms 45300 KB Output is correct
68 Correct 353 ms 44048 KB Output is correct
69 Correct 527 ms 44060 KB Output is correct
70 Correct 652 ms 44556 KB Output is correct
71 Correct 591 ms 42728 KB Output is correct
72 Correct 519 ms 44224 KB Output is correct
73 Correct 438 ms 44192 KB Output is correct
74 Correct 353 ms 44336 KB Output is correct
75 Correct 548 ms 44596 KB Output is correct
76 Correct 477 ms 43136 KB Output is correct
77 Correct 365 ms 43040 KB Output is correct
78 Correct 353 ms 42880 KB Output is correct
79 Correct 383 ms 44964 KB Output is correct
80 Correct 395 ms 44460 KB Output is correct
81 Correct 676 ms 43700 KB Output is correct
82 Correct 742 ms 44156 KB Output is correct
83 Correct 427 ms 41236 KB Output is correct
84 Correct 332 ms 43492 KB Output is correct
85 Correct 689 ms 45608 KB Output is correct
86 Correct 501 ms 45584 KB Output is correct
87 Correct 416 ms 45440 KB Output is correct
88 Correct 339 ms 42252 KB Output is correct
89 Correct 433 ms 41360 KB Output is correct
90 Correct 647 ms 43460 KB Output is correct
91 Correct 744 ms 45388 KB Output is correct
92 Correct 692 ms 45644 KB Output is correct
93 Correct 875 ms 45744 KB Output is correct
94 Correct 627 ms 42524 KB Output is correct
95 Correct 526 ms 46280 KB Output is correct
96 Correct 310 ms 44016 KB Output is correct
97 Correct 383 ms 23608 KB Output is correct
98 Correct 855 ms 47504 KB Output is correct
99 Correct 388 ms 43940 KB Output is correct
100 Correct 143 ms 5052 KB Output is correct
101 Correct 755 ms 45544 KB Output is correct
102 Correct 510 ms 35220 KB Output is correct
103 Correct 609 ms 34580 KB Output is correct
104 Correct 674 ms 40848 KB Output is correct