Submission #604071

# Submission time Handle Problem Language Result Execution time Memory
604071 2022-07-24T16:53:00 Z l_reho Stranded Far From Home (BOI22_island) C++14
20 / 100
352 ms 45980 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<bool> taken, vis;
vector<vector<int>> graph;
vector<ll> P;

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];
		
	}
	
	
	// cout<<curr<<" "<<P[curr]<<endl;
}
/*

8 7
8 8 7 7 7 6 5 4
1 2
2 3
1 4
4 5
1 6
6 7
7 8
 
 
*/
void solver(int curr, int p, vector<bool> &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);
		
	}
	
}

void solve(){
	cin>>N>>M;
	
	A.assign(N, 0);
	P.assign(N, 0);
	
	graph.assign(N, vector<int>());
	vis.assign(N, false);
	
	for(ll &v:A) cin>>v;
	
	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);
		
	}
	vector<bool> ans(N, 0);
	
	dfs(0);
	
	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++){
			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];
			
			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]){
					total = sum;
					break;
				}
				
				total += abitants;
				vector<int> adj = graph[node];

				for(int a : adj){
					if(taken[a]) continue;
					pq.push({a, A[a]});
				}
				
			}	
			ans[color] = total == sum;
		}
		
		for(int i = 0; i < N; i++) cout<<ans[i];
		cout<<endl;
		return;
	}

	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 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 6 ms 468 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 4 ms 468 KB Output is correct
7 Correct 6 ms 468 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 256 ms 568 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 4 ms 596 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 159 ms 660 KB Output is correct
14 Correct 83 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 343 ms 27088 KB Output is correct
4 Correct 232 ms 24532 KB Output is correct
5 Correct 292 ms 14264 KB Output is correct
6 Correct 309 ms 14708 KB Output is correct
7 Correct 328 ms 14908 KB Output is correct
8 Correct 352 ms 14744 KB Output is correct
9 Correct 307 ms 14680 KB Output is correct
10 Correct 246 ms 16188 KB Output is correct
11 Correct 214 ms 16180 KB Output is correct
12 Correct 254 ms 14800 KB Output is correct
13 Correct 228 ms 39560 KB Output is correct
14 Correct 273 ms 39884 KB Output is correct
15 Correct 299 ms 45980 KB Output is correct
16 Correct 218 ms 45436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 313 ms 45764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 349 ms 16816 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 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 0 ms 212 KB Output is correct
4 Correct 6 ms 468 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 4 ms 468 KB Output is correct
7 Correct 6 ms 468 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 256 ms 568 KB Output is correct
10 Correct 3 ms 596 KB Output is correct
11 Correct 4 ms 596 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 159 ms 660 KB Output is correct
14 Correct 83 ms 460 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 343 ms 27088 KB Output is correct
18 Correct 232 ms 24532 KB Output is correct
19 Correct 292 ms 14264 KB Output is correct
20 Correct 309 ms 14708 KB Output is correct
21 Correct 328 ms 14908 KB Output is correct
22 Correct 352 ms 14744 KB Output is correct
23 Correct 307 ms 14680 KB Output is correct
24 Correct 246 ms 16188 KB Output is correct
25 Correct 214 ms 16180 KB Output is correct
26 Correct 254 ms 14800 KB Output is correct
27 Correct 228 ms 39560 KB Output is correct
28 Correct 273 ms 39884 KB Output is correct
29 Correct 299 ms 45980 KB Output is correct
30 Correct 218 ms 45436 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Incorrect 313 ms 45764 KB Output isn't correct
33 Halted 0 ms 0 KB -