답안 #581744

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581744 2022-06-23T05:34:58 Z amunduzbaev Stranded Far From Home (BOI22_island) C++17
0 / 100
286 ms 37004 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;
#define int ll

const int N = 2e5 + 5;
int a[N], p[N], sz[N], par[N], cmp[N];
int sum[N], is[N], ner[N];
vector<int> edges[N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		p[i] = i;
	}
	
	for(int i=0;i<m;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	
	sort(p + 1, p + n + 1, [&](int i, int j){
		return a[i] < a[j];
	});
	
	for(int i=1;i<=n;i++){
		cmp[i] = a[i];
		sz[i] = 1;
		par[i] = i;
	}
	vector<vector<ar<int, 2>>> last;
	function<int(int)> f = [&](int x) { return (par[x] == x ? x : f(par[x])); };
	auto merge = [&](int a, int b){
		a = f(a), b = f(b);
		if(a == b) return;
		if(sz[a] < sz[b]) swap(a, b);
		sz[a] += sz[b], par[b] = a;
		cmp[a] += cmp[b];
		last.back().push_back({a, b});
	};
	
	auto add = [&](int v){
		for(auto x : edges[v]){
			if(a[x] > a[v]) continue;
			merge(v, x);
		}
	};
	
	for(int i=1,j=1;i<=n;){
		last.push_back({});
		while(j<=n && a[p[j]] == a[p[i]]){
			add(p[j]);
			j++;
		}
		
		while(i<j){
			sum[p[i]] = cmp[f(p[i])];
			i++;
		}
	}
	//~ cout<<"here"<<endl;
	
	auto rem = [&](int v){
		for(auto [a, b] : last.back()){
			par[b] = b, cmp[a] -= cmp[b], sz[a] -= sz[b];
		} last.pop_back();
	};
	
	for(int i=n,j=n;i>0;){
		while(j>0 && a[p[j]] == a[p[i]]){
			if(sum[p[j]] >= ner[f(p[j])]){
				is[p[j]] |= 1;
			}
			j--;
		}
		rem(a[p[i]]);
		
		while(i>j){
			if(is[p[i]]){
				for(auto x : edges[p[i]]){
					if(a[x] >= a[p[i]]) continue;
					ner[f(x)] = a[p[i]];
				}
			}
			i--;
		}
	}
	
	for(int i=1;i<=n;i++){
		cout<<is[i];
	} cout<<"\n";
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Incorrect 4 ms 5332 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 211 ms 36484 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Incorrect 264 ms 37004 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Incorrect 286 ms 29364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Incorrect 4 ms 5332 KB Output isn't correct
5 Halted 0 ms 0 KB -