Submission #790495

# Submission time Handle Problem Language Result Execution time Memory
790495 2023-07-22T17:48:46 Z esomer Stranded Far From Home (BOI22_island) C++17
100 / 100
301 ms 54620 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

struct DSU{
	vector<int> v;
	vector<ll> sum;
	vector<set<pair<int, int>>> adj;
	
	void init(vector<pair<int, int>>& a, vector<vector<pair<int, int>>>& adj1){
		int n = (int)a.size();
		v.assign(n, -1);
		sum.assign(n, 0);
		adj.assign(n, {});
		for(int i = 0; i < n; i++){
			sum[a[i].second] = a[i].first;
			for(pair<int, int> p : adj1[a[i].second]){
				adj[a[i].second].insert(p);
			}
		}
	}
	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);
		v[x] += v[y]; v[y] = x;	sum[x] += sum[y];
		if(adj[x].size() < adj[y].size()) swap(adj[x], adj[y]);
		for(pair<int, int> p : adj[y]) adj[x].insert(p);
		while((int)adj[x].size() > 0 && *adj[x].begin() <= val) adj[x].erase(adj[x].begin());
	}
	
	ll calc(int i){
		i = get(i);
		if((int)adj[i].size() == 0) return sum[i];
		else if(sum[i] >= (*adj[i].begin()).first) return -(*adj[i].begin()).second;
		else return sum[i];
	}
};

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});
	}
	vector<ll> ans(n);
	sort(v.begin(), v.end());
	DSU UF; UF.init(v, adj);
	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]);
		}
		ans[v[i].second] = UF.calc(v[i].second);
	}
	for(int j = n - 1; j >= 0; j--){
		int i = v[j].second;
		if(ans[i] <= 0) ans[i] = ans[-ans[i]];
	}
	for(ll x : 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 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 844 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 2 ms 756 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 165 ms 46036 KB Output is correct
4 Correct 192 ms 49224 KB Output is correct
5 Correct 169 ms 48840 KB Output is correct
6 Correct 177 ms 50388 KB Output is correct
7 Correct 175 ms 50564 KB Output is correct
8 Correct 212 ms 53352 KB Output is correct
9 Correct 170 ms 51912 KB Output is correct
10 Correct 215 ms 50220 KB Output is correct
11 Correct 225 ms 50036 KB Output is correct
12 Correct 218 ms 50628 KB Output is correct
13 Correct 150 ms 47944 KB Output is correct
14 Correct 144 ms 48204 KB Output is correct
15 Correct 129 ms 49516 KB Output is correct
16 Correct 104 ms 48368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 228 ms 44804 KB Output is correct
3 Correct 257 ms 49520 KB Output is correct
4 Correct 195 ms 49584 KB Output is correct
5 Correct 157 ms 47956 KB Output is correct
6 Correct 264 ms 49580 KB Output is correct
7 Correct 168 ms 49612 KB Output is correct
8 Correct 164 ms 49624 KB Output is correct
9 Correct 98 ms 48328 KB Output is correct
10 Correct 144 ms 48936 KB Output is correct
11 Correct 187 ms 48044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 253 ms 45992 KB Output is correct
3 Correct 259 ms 50252 KB Output is correct
4 Correct 269 ms 50652 KB Output is correct
5 Correct 241 ms 50368 KB Output is correct
6 Correct 205 ms 48920 KB Output is correct
7 Correct 200 ms 51036 KB Output is correct
8 Correct 126 ms 54620 KB Output is correct
9 Correct 182 ms 39420 KB Output is correct
10 Correct 228 ms 49120 KB Output is correct
11 Correct 206 ms 48072 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 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 2 ms 844 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 2 ms 756 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 165 ms 46036 KB Output is correct
18 Correct 192 ms 49224 KB Output is correct
19 Correct 169 ms 48840 KB Output is correct
20 Correct 177 ms 50388 KB Output is correct
21 Correct 175 ms 50564 KB Output is correct
22 Correct 212 ms 53352 KB Output is correct
23 Correct 170 ms 51912 KB Output is correct
24 Correct 215 ms 50220 KB Output is correct
25 Correct 225 ms 50036 KB Output is correct
26 Correct 218 ms 50628 KB Output is correct
27 Correct 150 ms 47944 KB Output is correct
28 Correct 144 ms 48204 KB Output is correct
29 Correct 129 ms 49516 KB Output is correct
30 Correct 104 ms 48368 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 228 ms 44804 KB Output is correct
33 Correct 257 ms 49520 KB Output is correct
34 Correct 195 ms 49584 KB Output is correct
35 Correct 157 ms 47956 KB Output is correct
36 Correct 264 ms 49580 KB Output is correct
37 Correct 168 ms 49612 KB Output is correct
38 Correct 164 ms 49624 KB Output is correct
39 Correct 98 ms 48328 KB Output is correct
40 Correct 144 ms 48936 KB Output is correct
41 Correct 187 ms 48044 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 253 ms 45992 KB Output is correct
44 Correct 259 ms 50252 KB Output is correct
45 Correct 269 ms 50652 KB Output is correct
46 Correct 241 ms 50368 KB Output is correct
47 Correct 205 ms 48920 KB Output is correct
48 Correct 200 ms 51036 KB Output is correct
49 Correct 126 ms 54620 KB Output is correct
50 Correct 182 ms 39420 KB Output is correct
51 Correct 228 ms 49120 KB Output is correct
52 Correct 206 ms 48072 KB Output is correct
53 Correct 0 ms 320 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 2 ms 724 KB Output is correct
57 Correct 2 ms 724 KB Output is correct
58 Correct 2 ms 720 KB Output is correct
59 Correct 2 ms 724 KB Output is correct
60 Correct 2 ms 716 KB Output is correct
61 Correct 2 ms 852 KB Output is correct
62 Correct 2 ms 716 KB Output is correct
63 Correct 2 ms 724 KB Output is correct
64 Correct 2 ms 724 KB Output is correct
65 Correct 2 ms 724 KB Output is correct
66 Correct 3 ms 724 KB Output is correct
67 Correct 143 ms 50524 KB Output is correct
68 Correct 154 ms 49248 KB Output is correct
69 Correct 167 ms 48828 KB Output is correct
70 Correct 186 ms 50392 KB Output is correct
71 Correct 170 ms 50484 KB Output is correct
72 Correct 224 ms 53428 KB Output is correct
73 Correct 194 ms 51800 KB Output is correct
74 Correct 207 ms 50232 KB Output is correct
75 Correct 200 ms 50108 KB Output is correct
76 Correct 216 ms 50864 KB Output is correct
77 Correct 145 ms 47948 KB Output is correct
78 Correct 135 ms 48188 KB Output is correct
79 Correct 141 ms 49512 KB Output is correct
80 Correct 98 ms 48320 KB Output is correct
81 Correct 237 ms 49272 KB Output is correct
82 Correct 237 ms 49592 KB Output is correct
83 Correct 164 ms 49584 KB Output is correct
84 Correct 139 ms 47980 KB Output is correct
85 Correct 239 ms 49596 KB Output is correct
86 Correct 197 ms 49616 KB Output is correct
87 Correct 157 ms 49544 KB Output is correct
88 Correct 142 ms 48844 KB Output is correct
89 Correct 172 ms 48092 KB Output is correct
90 Correct 263 ms 50372 KB Output is correct
91 Correct 261 ms 50284 KB Output is correct
92 Correct 265 ms 50528 KB Output is correct
93 Correct 254 ms 50484 KB Output is correct
94 Correct 206 ms 48972 KB Output is correct
95 Correct 206 ms 51004 KB Output is correct
96 Correct 127 ms 54596 KB Output is correct
97 Correct 206 ms 39320 KB Output is correct
98 Correct 219 ms 49060 KB Output is correct
99 Correct 177 ms 48008 KB Output is correct
100 Correct 134 ms 25744 KB Output is correct
101 Correct 301 ms 50520 KB Output is correct
102 Correct 264 ms 49240 KB Output is correct
103 Correct 272 ms 49724 KB Output is correct
104 Correct 294 ms 50100 KB Output is correct