Submission #586840

# Submission time Handle Problem Language Result Execution time Memory
586840 2022-06-30T18:04:02 Z nots0fast Stranded Far From Home (BOI22_island) C++17
100 / 100
740 ms 65720 KB
#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define mt make_tuple
#define ff first
#define setp setprecision(12)<<fixed
#define ss second
#define fori(v) for(int i=0; i<v; i++)
#define forj(v) for(int j=0; j<v; j++)
#define fork(v) for(int k=0; k<v; k++)
#define forl(v) for(int l=0; l<v; l++)
#define fort(v) for(int t=0; t<v; t++)
#define forz(v) for(int z=0; z<v; z++)
#define ll long long
#define lll __int128
#define ld long double
#define pb(a) push_back(a)
// #define cout out
// #define cin in
ll inf = pow(10,18);
ll modulo = 1000000007;
double eps = 1e-10;
ifstream in;
ofstream out;

#define MAX 200'200


ll arr[MAX];

namespace dsu{
	int aid[MAX];
	vector<int> dsu[MAX];
	vector<int> good[MAX];
	priority_queue<pair<int, int> , vector<pair<int, int> > , greater<pair<int, int> > > edg[MAX];
	ll s[MAX];
	void ini(int n){
		fori(n){
			aid[i] = i, dsu[i].clear(), dsu[i].push_back(i), good[i].pb(i), s[i] = arr[i];
			while(!edg[i].empty()){
				edg[i].pop();
			}
		}
	}
	ll join(int a,int b){  //	1 - if joined, 0 - if they were already joined 
		int aid1 = aid[a], aid2 = aid[b];
		if(aid1==aid2)
			return -1;
		
		if(dsu[aid2].size() > dsu[aid1].size())
			swap(aid1,aid2);
		
		{
			if(good[aid1].size() < good[aid2].size()){
				swap(good[aid1], good[aid2]);
			}
			for(auto& el : good[aid2]){
				good[aid1].pb(el);
			}
			good[aid2].clear();
		}
		
		{
			if(edg[aid1].size() < edg[aid2].size()){
				swap(edg[aid1], edg[aid2]);
			}
			while(!edg[aid2].empty()){
				auto tp = edg[aid2].top();
				auto hd = tp.ss;
				if(aid[hd] != aid1 && aid[hd]!=aid2){
					edg[aid1].push(tp);
				}
				edg[aid2].pop();
			}
		}
		
		
		for(auto hd : dsu[aid2]){
			aid[hd] = aid1;
			dsu[aid1].push_back(hd);
		}
		dsu[aid2].clear();
		
		s[aid1]+=s[aid2];
		
		return aid1;
	}
};

using namespace dsu;

bool debug = 0;

void deal(){
	srand(time(NULL));
	
	
	ll n, m;
	if(debug){
		n = 2e2, m = 2e3;
	}
	else{
		cin>>n>>m;
	}
	
	fori(n){
		if(debug){
			arr[i] = rand() % (100);
		}
		else{
			cin>>arr[i];
		}
	}
	ini(n);
	
	/*
	if(debug){
		fori(n-1){
			edg[0].push(mp(arr[i+1],i+1));
			edg[i+1].push(mp(arr[0],0));
		}
	}
	*/
	
	fori(m){
		ll ai, bi;
		if(!debug){
			cin>>ai>>bi;
			--ai, --bi;
		}
		else{
			ai = rand() % n , bi = (ai + rand() % (n-1) + 1)%n;
			assert(ai != bi);
		}
		edg[ai].push(mp(arr[bi], bi));
		edg[bi].push(mp(arr[ai], ai));
	}
	
	
	set<pair<ll, ll> > all; // components and their sizes
	fori(n){
		all.insert(mp(arr[i], i));
	}
	
	while((ll)all.size() > 1){
		auto it = all.begin();
		ll hd = (*it).ss;
		all.erase(all.begin());
		assert(!edg[hd].empty());
		// assert(!all.empty());
		while(1){
			assert(!edg[hd].empty());
			auto it = edg[hd].top();
			ll wh = it.ss;
			if(aid[wh] == hd){
				edg[hd].pop();
				continue;
			}
			else{
				if(s[hd] >= arr[wh]){
					edg[hd].pop();
					ll hr = aid[wh];
					auto it = all.find(mp(s[hr], hr) );
					if(it != all.end()){
						all.erase(it);
					}
					ll cr = join(hd, hr);
					assert(cr != -1);
					all.insert(mp(s[cr], cr));
				}
				else{
					good[hd].clear();
				}
				break;
			}
		}
		
	}
	
	
	vector<bool> ans(n, 0);
	fori(n){
		for(auto& el : good[i]){
			ans[el] = 1;
		}
	}
	fori(n){
		cout<<ans[i];
	}
	
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 9 ms 15956 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 11 ms 16340 KB Output is correct
5 Correct 11 ms 16388 KB Output is correct
6 Correct 11 ms 16300 KB Output is correct
7 Correct 10 ms 16364 KB Output is correct
8 Correct 12 ms 16260 KB Output is correct
9 Correct 10 ms 16344 KB Output is correct
10 Correct 11 ms 16340 KB Output is correct
11 Correct 12 ms 16428 KB Output is correct
12 Correct 11 ms 16340 KB Output is correct
13 Correct 11 ms 16340 KB Output is correct
14 Correct 11 ms 16364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15888 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 430 ms 59548 KB Output is correct
4 Correct 302 ms 52388 KB Output is correct
5 Correct 564 ms 55680 KB Output is correct
6 Correct 570 ms 56864 KB Output is correct
7 Correct 570 ms 56720 KB Output is correct
8 Correct 341 ms 53284 KB Output is correct
9 Correct 360 ms 56416 KB Output is correct
10 Correct 181 ms 52900 KB Output is correct
11 Correct 292 ms 52980 KB Output is correct
12 Correct 465 ms 55760 KB Output is correct
13 Correct 339 ms 51396 KB Output is correct
14 Correct 284 ms 51308 KB Output is correct
15 Correct 279 ms 51364 KB Output is correct
16 Correct 206 ms 51096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16012 KB Output is correct
2 Correct 727 ms 60840 KB Output is correct
3 Correct 698 ms 60856 KB Output is correct
4 Correct 314 ms 51376 KB Output is correct
5 Correct 304 ms 51380 KB Output is correct
6 Correct 740 ms 62792 KB Output is correct
7 Correct 271 ms 53024 KB Output is correct
8 Correct 265 ms 53032 KB Output is correct
9 Correct 194 ms 51988 KB Output is correct
10 Correct 387 ms 62988 KB Output is correct
11 Correct 458 ms 64436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 628 ms 59244 KB Output is correct
3 Correct 596 ms 59760 KB Output is correct
4 Correct 583 ms 61204 KB Output is correct
5 Correct 660 ms 61368 KB Output is correct
6 Correct 598 ms 60172 KB Output is correct
7 Correct 243 ms 52920 KB Output is correct
8 Correct 214 ms 54604 KB Output is correct
9 Correct 271 ms 44524 KB Output is correct
10 Correct 625 ms 62740 KB Output is correct
11 Correct 468 ms 62604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 9 ms 15956 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 11 ms 16340 KB Output is correct
5 Correct 11 ms 16388 KB Output is correct
6 Correct 11 ms 16300 KB Output is correct
7 Correct 10 ms 16364 KB Output is correct
8 Correct 12 ms 16260 KB Output is correct
9 Correct 10 ms 16344 KB Output is correct
10 Correct 11 ms 16340 KB Output is correct
11 Correct 12 ms 16428 KB Output is correct
12 Correct 11 ms 16340 KB Output is correct
13 Correct 11 ms 16340 KB Output is correct
14 Correct 11 ms 16364 KB Output is correct
15 Correct 10 ms 15888 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 430 ms 59548 KB Output is correct
18 Correct 302 ms 52388 KB Output is correct
19 Correct 564 ms 55680 KB Output is correct
20 Correct 570 ms 56864 KB Output is correct
21 Correct 570 ms 56720 KB Output is correct
22 Correct 341 ms 53284 KB Output is correct
23 Correct 360 ms 56416 KB Output is correct
24 Correct 181 ms 52900 KB Output is correct
25 Correct 292 ms 52980 KB Output is correct
26 Correct 465 ms 55760 KB Output is correct
27 Correct 339 ms 51396 KB Output is correct
28 Correct 284 ms 51308 KB Output is correct
29 Correct 279 ms 51364 KB Output is correct
30 Correct 206 ms 51096 KB Output is correct
31 Correct 9 ms 16012 KB Output is correct
32 Correct 727 ms 60840 KB Output is correct
33 Correct 698 ms 60856 KB Output is correct
34 Correct 314 ms 51376 KB Output is correct
35 Correct 304 ms 51380 KB Output is correct
36 Correct 740 ms 62792 KB Output is correct
37 Correct 271 ms 53024 KB Output is correct
38 Correct 265 ms 53032 KB Output is correct
39 Correct 194 ms 51988 KB Output is correct
40 Correct 387 ms 62988 KB Output is correct
41 Correct 458 ms 64436 KB Output is correct
42 Correct 8 ms 15956 KB Output is correct
43 Correct 628 ms 59244 KB Output is correct
44 Correct 596 ms 59760 KB Output is correct
45 Correct 583 ms 61204 KB Output is correct
46 Correct 660 ms 61368 KB Output is correct
47 Correct 598 ms 60172 KB Output is correct
48 Correct 243 ms 52920 KB Output is correct
49 Correct 214 ms 54604 KB Output is correct
50 Correct 271 ms 44524 KB Output is correct
51 Correct 625 ms 62740 KB Output is correct
52 Correct 468 ms 62604 KB Output is correct
53 Correct 9 ms 15956 KB Output is correct
54 Correct 9 ms 15956 KB Output is correct
55 Correct 8 ms 15956 KB Output is correct
56 Correct 10 ms 16272 KB Output is correct
57 Correct 12 ms 16404 KB Output is correct
58 Correct 10 ms 16340 KB Output is correct
59 Correct 11 ms 16276 KB Output is correct
60 Correct 10 ms 16256 KB Output is correct
61 Correct 11 ms 16400 KB Output is correct
62 Correct 11 ms 16404 KB Output is correct
63 Correct 10 ms 16340 KB Output is correct
64 Correct 10 ms 16340 KB Output is correct
65 Correct 10 ms 16268 KB Output is correct
66 Correct 11 ms 16340 KB Output is correct
67 Correct 399 ms 62092 KB Output is correct
68 Correct 281 ms 54168 KB Output is correct
69 Correct 539 ms 56412 KB Output is correct
70 Correct 530 ms 58584 KB Output is correct
71 Correct 560 ms 56848 KB Output is correct
72 Correct 292 ms 55096 KB Output is correct
73 Correct 346 ms 58508 KB Output is correct
74 Correct 165 ms 54008 KB Output is correct
75 Correct 237 ms 54392 KB Output is correct
76 Correct 435 ms 56992 KB Output is correct
77 Correct 288 ms 52540 KB Output is correct
78 Correct 266 ms 52504 KB Output is correct
79 Correct 272 ms 53644 KB Output is correct
80 Correct 180 ms 54336 KB Output is correct
81 Correct 731 ms 62988 KB Output is correct
82 Correct 700 ms 63512 KB Output is correct
83 Correct 290 ms 51416 KB Output is correct
84 Correct 288 ms 53224 KB Output is correct
85 Correct 671 ms 65720 KB Output is correct
86 Correct 249 ms 55732 KB Output is correct
87 Correct 248 ms 55608 KB Output is correct
88 Correct 405 ms 61856 KB Output is correct
89 Correct 463 ms 62136 KB Output is correct
90 Correct 581 ms 59384 KB Output is correct
91 Correct 579 ms 63544 KB Output is correct
92 Correct 587 ms 65412 KB Output is correct
93 Correct 626 ms 64812 KB Output is correct
94 Correct 609 ms 61960 KB Output is correct
95 Correct 284 ms 55208 KB Output is correct
96 Correct 218 ms 56684 KB Output is correct
97 Correct 277 ms 45116 KB Output is correct
98 Correct 609 ms 63088 KB Output is correct
99 Correct 444 ms 63164 KB Output is correct
100 Correct 123 ms 28580 KB Output is correct
101 Correct 659 ms 63480 KB Output is correct
102 Correct 501 ms 59668 KB Output is correct
103 Correct 561 ms 56108 KB Output is correct
104 Correct 634 ms 58692 KB Output is correct