Submission #573741

#TimeUsernameProblemLanguageResultExecution timeMemory
573741kshitij_sodaniStranded Far From Home (BOI22_island)C++14
100 / 100
286 ms44360 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
 
 
llo it[200001];
llo n,m;
vector<llo> adj[200001];
vector<pair<llo,llo>> adj2[200001];
llo par[200001];
llo ss[200001];
llo find(llo no){
	if(par[no]==no){
		return no;
	}
	par[no]=find(par[no]);
	return par[no];
}
llo ans[200001];
void dfs(llo no,llo par=-1,llo ma=0){
	ans[no]=1-ma;
	for(auto j:adj2[no]){
		dfs(j.a,no,max(ma,j.b));
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>m;
	vector<pair<llo,llo>> tt;
	for(llo i=0;i<n;i++){
		cin>>it[i];
		par[i]=i;
		ss[i]=it[i];
		tt.pb({it[i],i});
	}
	for(llo i=0;i<m;i++){
		llo aa,bb;
		cin>>aa>>bb;
		aa--;
		bb--;
		adj[aa].pb(bb);
		adj[bb].pb(aa);
	}
	sort(tt.begin(),tt.end());
	llo ind=0;
	/*for(auto j:tt){
		cout<<j.a<<",";
	}
	cout<<endl;*/
	while(ind<tt.size()){
		llo ind2=ind;
		while(ind<tt.size()){
			if(tt[ind].a==tt[ind2].a){
				ind++;
			}
			else{
				break;
			}
		}
		vector<llo> ee;
		for(llo i=ind2;i<ind;i++){
			ee.pb(tt[i].b);
		}
 
		for(auto j:ee){
			for(auto jj:adj[j]){
				if(it[jj]<it[j]){
					llo x=find(jj);
					if(it[x]==it[j]){
						llo y=find(j);
						if(x!=y){
							adj2[y].pb({x,0});
							par[x]=y;
							ss[y]+=ss[x];
						}
						continue;
					}
					llo z=0;
					if(ss[x]<it[j]){
						z=1;
						//cout<<j<<":"<<x<<endl;
					}
					llo y=find(j);
					if(x!=y){
						adj2[y].pb({x,z});
						par[x]=y;
						ss[y]+=ss[x];
					}
				}
			}
		}
		//cout<<ind2<<"::"<<ind<<endl;
		for(auto j:ee){
			for(auto jj:adj[j]){
				if(j==jj){
					continue;
				}
				if(it[jj]==it[j]){
					llo x=find(j);
					llo y=find(jj);
 
					if(x==y){
						continue;
					}
					//cout<<j<<"::"<<jj<<endl;
					par[x]=y;
					ss[y]+=ss[x];
					adj2[y].pb({x,0});
				}
			}
		}
		
 
 
	}
	dfs(find(0));
	for(llo i=0;i<n;i++){
		cout<<ans[i];
	}
	cout<<endl;
 
 
 
 
 
 
	return 0;
}

Compilation message (stderr)

island.cpp: In function 'int main()':
island.cpp:55:11: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  while(ind<tt.size()){
      |        ~~~^~~~~~~~~~
island.cpp:57:12: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |   while(ind<tt.size()){
      |         ~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...