Submission #790886

# Submission time Handle Problem Language Result Execution time Memory
790886 2023-07-23T09:14:45 Z esomer Stranded Far From Home (BOI22_island) C++17
100 / 100
165 ms 24368 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

struct DSU{
	vector<int> v;
	vector<ll> sum;
	vector<ll> ans;
	vector<int> lst;
	
	void init(vector<pair<int, int>>& a){
		int n = (int)a.size();
		v.assign(n, -1);
		sum.assign(n, 0);
		ans.resize(n);
		lst.resize(n);
		for(int i = 0; i < n; i++){
			sum[a[i].second] = a[i].first;
			ans[a[i].second] = a[i].first;
			lst[a[i].second] = a[i].second;
		}
	}
	int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}
	
	void unite(int x, int y, pair<int, int> val){
		x = get(x); y = get(y);
		if(x == y) return;
		if(v[x] > v[y]) swap(x, y);
		if(lst[x] != val.second && sum[x] >= val.first) ans[lst[x]] = -val.second;
		if(lst[y] != val.second && sum[y] >= val.first) ans[lst[y]] = -val.second;
		v[x] += v[y]; v[y] = x;	sum[x] += sum[y];
		lst[x] = val.second;
		ans[val.second] = sum[x];
	}
};

void solve(){
	int n, m; cin >> n >> m;
	vector<pair<int, int>> v(n);
	ll total_sum = 0;
	for(int i = 0; i < n; i++){
		int x; cin >> x;
		total_sum += x;
		v[i] = {x, i};
	}
	vector<vector<pair<int, int>>> adj(n);
	for(int i = 0; i < m; i++){
		int a, b; cin >> a >> b; a--; b--;
		adj[a].push_back({v[b].first, b});
		adj[b].push_back({v[a].first, a});
	}
	sort(v.begin(), v.end());
	DSU UF; UF.init(v);
	for(int i = 0; i < n; i++){
		for(pair<int, int> p : adj[v[i].second]){
			if(p > v[i]) continue;
			UF.unite(v[i].second, p.second, v[i]);
		}
	}
	for(int j = n - 1; j >= 0; j--){
		int i = v[j].second;
		if(UF.ans[i] <= 0) UF.ans[i] = UF.ans[-UF.ans[i]];
	}
	for(ll x : UF.ans) cout << (x == total_sum ? 1 : 0);
	cout << endl;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	solve();
}
# 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 0 ms 212 KB Output is correct
4 Correct 2 ms 496 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 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 105 ms 23124 KB Output is correct
4 Correct 85 ms 21832 KB Output is correct
5 Correct 91 ms 22288 KB Output is correct
6 Correct 91 ms 22988 KB Output is correct
7 Correct 113 ms 23072 KB Output is correct
8 Correct 96 ms 23080 KB Output is correct
9 Correct 116 ms 24364 KB Output is correct
10 Correct 75 ms 22848 KB Output is correct
11 Correct 110 ms 22712 KB Output is correct
12 Correct 93 ms 21780 KB Output is correct
13 Correct 78 ms 20560 KB Output is correct
14 Correct 80 ms 20812 KB Output is correct
15 Correct 89 ms 22104 KB Output is correct
16 Correct 71 ms 21196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 115 ms 21960 KB Output is correct
3 Correct 113 ms 22088 KB Output is correct
4 Correct 111 ms 22220 KB Output is correct
5 Correct 97 ms 20584 KB Output is correct
6 Correct 108 ms 22092 KB Output is correct
7 Correct 113 ms 22112 KB Output is correct
8 Correct 102 ms 22192 KB Output is correct
9 Correct 66 ms 21176 KB Output is correct
10 Correct 130 ms 21448 KB Output is correct
11 Correct 97 ms 20608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 119 ms 23036 KB Output is correct
3 Correct 117 ms 23028 KB Output is correct
4 Correct 125 ms 23192 KB Output is correct
5 Correct 127 ms 23104 KB Output is correct
6 Correct 108 ms 22272 KB Output is correct
7 Correct 91 ms 23608 KB Output is correct
8 Correct 68 ms 22476 KB Output is correct
9 Correct 71 ms 14132 KB Output is correct
10 Correct 114 ms 21632 KB Output is correct
11 Correct 105 ms 20644 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 0 ms 212 KB Output is correct
4 Correct 2 ms 496 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 105 ms 23124 KB Output is correct
18 Correct 85 ms 21832 KB Output is correct
19 Correct 91 ms 22288 KB Output is correct
20 Correct 91 ms 22988 KB Output is correct
21 Correct 113 ms 23072 KB Output is correct
22 Correct 96 ms 23080 KB Output is correct
23 Correct 116 ms 24364 KB Output is correct
24 Correct 75 ms 22848 KB Output is correct
25 Correct 110 ms 22712 KB Output is correct
26 Correct 93 ms 21780 KB Output is correct
27 Correct 78 ms 20560 KB Output is correct
28 Correct 80 ms 20812 KB Output is correct
29 Correct 89 ms 22104 KB Output is correct
30 Correct 71 ms 21196 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 115 ms 21960 KB Output is correct
33 Correct 113 ms 22088 KB Output is correct
34 Correct 111 ms 22220 KB Output is correct
35 Correct 97 ms 20584 KB Output is correct
36 Correct 108 ms 22092 KB Output is correct
37 Correct 113 ms 22112 KB Output is correct
38 Correct 102 ms 22192 KB Output is correct
39 Correct 66 ms 21176 KB Output is correct
40 Correct 130 ms 21448 KB Output is correct
41 Correct 97 ms 20608 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 119 ms 23036 KB Output is correct
44 Correct 117 ms 23028 KB Output is correct
45 Correct 125 ms 23192 KB Output is correct
46 Correct 127 ms 23104 KB Output is correct
47 Correct 108 ms 22272 KB Output is correct
48 Correct 91 ms 23608 KB Output is correct
49 Correct 68 ms 22476 KB Output is correct
50 Correct 71 ms 14132 KB Output is correct
51 Correct 114 ms 21632 KB Output is correct
52 Correct 105 ms 20644 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 1 ms 468 KB Output is correct
58 Correct 1 ms 492 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 504 KB Output is correct
63 Correct 1 ms 504 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 2 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 89 ms 23128 KB Output is correct
68 Correct 110 ms 21836 KB Output is correct
69 Correct 93 ms 22336 KB Output is correct
70 Correct 105 ms 22980 KB Output is correct
71 Correct 108 ms 23208 KB Output is correct
72 Correct 91 ms 23124 KB Output is correct
73 Correct 91 ms 24368 KB Output is correct
74 Correct 80 ms 22844 KB Output is correct
75 Correct 93 ms 22712 KB Output is correct
76 Correct 90 ms 21696 KB Output is correct
77 Correct 93 ms 20588 KB Output is correct
78 Correct 96 ms 20820 KB Output is correct
79 Correct 86 ms 22152 KB Output is correct
80 Correct 66 ms 21292 KB Output is correct
81 Correct 126 ms 22036 KB Output is correct
82 Correct 110 ms 22108 KB Output is correct
83 Correct 101 ms 22172 KB Output is correct
84 Correct 79 ms 20640 KB Output is correct
85 Correct 111 ms 22176 KB Output is correct
86 Correct 129 ms 22128 KB Output is correct
87 Correct 110 ms 22168 KB Output is correct
88 Correct 89 ms 21432 KB Output is correct
89 Correct 99 ms 20612 KB Output is correct
90 Correct 136 ms 23044 KB Output is correct
91 Correct 114 ms 22972 KB Output is correct
92 Correct 114 ms 23144 KB Output is correct
93 Correct 165 ms 23116 KB Output is correct
94 Correct 93 ms 22280 KB Output is correct
95 Correct 80 ms 23616 KB Output is correct
96 Correct 67 ms 22480 KB Output is correct
97 Correct 69 ms 14156 KB Output is correct
98 Correct 103 ms 21612 KB Output is correct
99 Correct 109 ms 20788 KB Output is correct
100 Correct 31 ms 6816 KB Output is correct
101 Correct 120 ms 23120 KB Output is correct
102 Correct 117 ms 19848 KB Output is correct
103 Correct 108 ms 20572 KB Output is correct
104 Correct 118 ms 22860 KB Output is correct