답안 #913933

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
913933 2024-01-20T14:42:30 Z pragmatist Stranded Far From Home (BOI22_island) C++17
100 / 100
561 ms 58060 KB
/*
JUDGE_ID : 331438IX
*/
 
#include<iostream>
#include<cassert>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<queue>
#include<map>
 
using namespace std;
 
const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const int MOD = (int)998244353;
const long long INF = (long long)1e18 + 7; 
 
int mult(int x, int y) {
	return 1ll*x*y%MOD;
}
 
int sum(int x, int y) {
	x += y;
	if(x >= MOD) {
		x -= MOD;
	}
	return x;
}
 
int sub(int x, int y) {
	x -= y;
	if(x < 0) {
		x += MOD;
	}
	return x;
}
 
bool is_prime(int x) {
	if(x == 1) {
		return 0;
	}
	for(int i = 2; i * i <= x; ++i) {
		if(x % i == 0) {
			return 0;
		}
	}
	return 1;
}
 
int n, m, a[N], p[N];
vector<int> pos[N];
vector<int> need[N];
bool ans[N];
 
set<pair<long long, int> > have;
long long s[N];

void comb(int u, int v) {
	int l = p[u];
	int r = p[v];
	if(l == r) {
		return;
	}
	have.erase({s[l], l});
	have.erase({s[r], r});
	if((int)pos[l].size() > (int)pos[r].size()) {
		for(auto x : pos[r]) {
			p[x] = l;
			pos[l].push_back(x);
		}	
	    for(auto x : need[r]) {
	    	need[l].push_back(x);
	    }
		pos[r].clear();
		need[r].clear();        		
		s[l] += s[r];
		if(!need[l].empty()) {
			have.insert({s[l], l});
		}
	} else {
		for(auto x : pos[l]) {
			p[x] = r;
			pos[r].push_back(x);
		}	
		for(auto x : need[l]) {
			need[r].push_back(x);
		}
		pos[l].clear();
		need[l].clear();
		s[r] += s[l];
		if(!need[r].empty()) {
			have.insert({s[r], r});
		}
	}
}
 
long long val[N];
 
int b[N]; 
 
bool cmp2(int i, int j) {
	return a[i] < a[j];
}
 
vector<int> g[N];

int main() {	
	ios_base::sync_with_stdio(NULL);
	cin.tie(0);
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);         
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		p[i] = i;
		b[i] = i;
		s[i] = a[i];
		pos[i].push_back(i);
		need[i].push_back(i);
	}
	for(int i = 1; i <= n; ++i) {
		ans[i] = 1;
	}    
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}    
	sort(b+1, b+1+n, cmp2);
	for(int i = 1; i <= n; ++i) {
		if(a[b[i]] == a[b[n]]) {
			break;
		}
		int r = i;
		while(r<n && a[b[r]] == a[b[r+1]]) {
			r++;
		}
		for(int k = i; k <= r; ++k) {
			have.insert({a[b[r]], b[k]});
		}
		for(int k = i; k <= r; ++k) {
			for(auto u : g[b[k]]) {
				if(a[u] <= a[b[k]]) {
					comb(u, b[k]);
				}
			}
		}
		for(int k = i; k <= r; ++k) {
			for(auto u : g[b[k]]) {
				long long cur = s[p[b[k]]];
				if(cur >= a[u]) {
					comb(u, b[k]);
				}
			}
		}
		vector<pair<long long, int> > del;
		for(auto [cur, k] : have) {
			if(cur < a[b[r+1]]) {
				for(auto x : need[k]) {
					ans[x] = 0;
				}
				del.push_back({cur, k});
				need[k].clear();
			} else {
				break;
			}
		}
		for(auto e : del) {
			have.erase(e);
		}
		i = r;
	}
	for(int i = 1; i <= n; ++i) {
		cout << ans[i];
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 7 ms 19292 KB Output is correct
5 Correct 7 ms 19292 KB Output is correct
6 Correct 6 ms 19032 KB Output is correct
7 Correct 7 ms 19292 KB Output is correct
8 Correct 7 ms 19036 KB Output is correct
9 Correct 7 ms 19288 KB Output is correct
10 Correct 7 ms 19292 KB Output is correct
11 Correct 7 ms 19292 KB Output is correct
12 Correct 6 ms 19292 KB Output is correct
13 Correct 6 ms 19292 KB Output is correct
14 Correct 7 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 262 ms 41120 KB Output is correct
4 Correct 241 ms 53328 KB Output is correct
5 Correct 373 ms 47312 KB Output is correct
6 Correct 411 ms 52952 KB Output is correct
7 Correct 365 ms 53216 KB Output is correct
8 Correct 523 ms 57908 KB Output is correct
9 Correct 273 ms 56552 KB Output is correct
10 Correct 134 ms 43528 KB Output is correct
11 Correct 96 ms 43276 KB Output is correct
12 Correct 483 ms 53720 KB Output is correct
13 Correct 255 ms 54408 KB Output is correct
14 Correct 244 ms 53356 KB Output is correct
15 Correct 214 ms 45220 KB Output is correct
16 Correct 120 ms 44392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 484 ms 51324 KB Output is correct
3 Correct 465 ms 55492 KB Output is correct
4 Correct 99 ms 43512 KB Output is correct
5 Correct 243 ms 54996 KB Output is correct
6 Correct 459 ms 55128 KB Output is correct
7 Correct 213 ms 45264 KB Output is correct
8 Correct 177 ms 45252 KB Output is correct
9 Correct 118 ms 44336 KB Output is correct
10 Correct 226 ms 52144 KB Output is correct
11 Correct 485 ms 54488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 464 ms 44712 KB Output is correct
3 Correct 478 ms 44052 KB Output is correct
4 Correct 478 ms 44684 KB Output is correct
5 Correct 101 ms 38992 KB Output is correct
6 Correct 121 ms 38104 KB Output is correct
7 Correct 245 ms 52676 KB Output is correct
8 Correct 198 ms 51280 KB Output is correct
9 Correct 167 ms 32476 KB Output is correct
10 Correct 501 ms 55152 KB Output is correct
11 Correct 439 ms 52064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 7 ms 19292 KB Output is correct
5 Correct 7 ms 19292 KB Output is correct
6 Correct 6 ms 19032 KB Output is correct
7 Correct 7 ms 19292 KB Output is correct
8 Correct 7 ms 19036 KB Output is correct
9 Correct 7 ms 19288 KB Output is correct
10 Correct 7 ms 19292 KB Output is correct
11 Correct 7 ms 19292 KB Output is correct
12 Correct 6 ms 19292 KB Output is correct
13 Correct 6 ms 19292 KB Output is correct
14 Correct 7 ms 19292 KB Output is correct
15 Correct 4 ms 19036 KB Output is correct
16 Correct 5 ms 19036 KB Output is correct
17 Correct 262 ms 41120 KB Output is correct
18 Correct 241 ms 53328 KB Output is correct
19 Correct 373 ms 47312 KB Output is correct
20 Correct 411 ms 52952 KB Output is correct
21 Correct 365 ms 53216 KB Output is correct
22 Correct 523 ms 57908 KB Output is correct
23 Correct 273 ms 56552 KB Output is correct
24 Correct 134 ms 43528 KB Output is correct
25 Correct 96 ms 43276 KB Output is correct
26 Correct 483 ms 53720 KB Output is correct
27 Correct 255 ms 54408 KB Output is correct
28 Correct 244 ms 53356 KB Output is correct
29 Correct 214 ms 45220 KB Output is correct
30 Correct 120 ms 44392 KB Output is correct
31 Correct 5 ms 19036 KB Output is correct
32 Correct 484 ms 51324 KB Output is correct
33 Correct 465 ms 55492 KB Output is correct
34 Correct 99 ms 43512 KB Output is correct
35 Correct 243 ms 54996 KB Output is correct
36 Correct 459 ms 55128 KB Output is correct
37 Correct 213 ms 45264 KB Output is correct
38 Correct 177 ms 45252 KB Output is correct
39 Correct 118 ms 44336 KB Output is correct
40 Correct 226 ms 52144 KB Output is correct
41 Correct 485 ms 54488 KB Output is correct
42 Correct 5 ms 19036 KB Output is correct
43 Correct 464 ms 44712 KB Output is correct
44 Correct 478 ms 44052 KB Output is correct
45 Correct 478 ms 44684 KB Output is correct
46 Correct 101 ms 38992 KB Output is correct
47 Correct 121 ms 38104 KB Output is correct
48 Correct 245 ms 52676 KB Output is correct
49 Correct 198 ms 51280 KB Output is correct
50 Correct 167 ms 32476 KB Output is correct
51 Correct 501 ms 55152 KB Output is correct
52 Correct 439 ms 52064 KB Output is correct
53 Correct 5 ms 19036 KB Output is correct
54 Correct 5 ms 19036 KB Output is correct
55 Correct 6 ms 19036 KB Output is correct
56 Correct 7 ms 19308 KB Output is correct
57 Correct 7 ms 19288 KB Output is correct
58 Correct 6 ms 19292 KB Output is correct
59 Correct 7 ms 19244 KB Output is correct
60 Correct 7 ms 19292 KB Output is correct
61 Correct 7 ms 19292 KB Output is correct
62 Correct 7 ms 19368 KB Output is correct
63 Correct 7 ms 19292 KB Output is correct
64 Correct 7 ms 19292 KB Output is correct
65 Correct 7 ms 19288 KB Output is correct
66 Correct 8 ms 19292 KB Output is correct
67 Correct 201 ms 45612 KB Output is correct
68 Correct 252 ms 56592 KB Output is correct
69 Correct 358 ms 51396 KB Output is correct
70 Correct 399 ms 52908 KB Output is correct
71 Correct 368 ms 53036 KB Output is correct
72 Correct 536 ms 57952 KB Output is correct
73 Correct 243 ms 56856 KB Output is correct
74 Correct 138 ms 43456 KB Output is correct
75 Correct 92 ms 43200 KB Output is correct
76 Correct 482 ms 53792 KB Output is correct
77 Correct 254 ms 54432 KB Output is correct
78 Correct 231 ms 53560 KB Output is correct
79 Correct 233 ms 45424 KB Output is correct
80 Correct 119 ms 44396 KB Output is correct
81 Correct 450 ms 55688 KB Output is correct
82 Correct 461 ms 55016 KB Output is correct
83 Correct 113 ms 43604 KB Output is correct
84 Correct 235 ms 54944 KB Output is correct
85 Correct 450 ms 55100 KB Output is correct
86 Correct 213 ms 45236 KB Output is correct
87 Correct 178 ms 45372 KB Output is correct
88 Correct 235 ms 52040 KB Output is correct
89 Correct 488 ms 54724 KB Output is correct
90 Correct 475 ms 49284 KB Output is correct
91 Correct 454 ms 48532 KB Output is correct
92 Correct 459 ms 49184 KB Output is correct
93 Correct 111 ms 43600 KB Output is correct
94 Correct 99 ms 42312 KB Output is correct
95 Correct 229 ms 56772 KB Output is correct
96 Correct 197 ms 54352 KB Output is correct
97 Correct 165 ms 35020 KB Output is correct
98 Correct 561 ms 58060 KB Output is correct
99 Correct 488 ms 54968 KB Output is correct
100 Correct 41 ms 23376 KB Output is correct
101 Correct 519 ms 51136 KB Output is correct
102 Correct 93 ms 38692 KB Output is correct
103 Correct 345 ms 42700 KB Output is correct
104 Correct 452 ms 47416 KB Output is correct