답안 #810356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
810356 2023-08-06T08:45:26 Z qwerasdfzxcl Security Guard (JOI23_guard) C++17
51 / 100
344 ms 72692 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n, mn, mx;
ll a[200200];
pair<int, int> E[400400];

ll ans[200200];

priority_queue<array<ll, 4>, vector<array<ll, 4>>, greater<array<ll, 4>>> pq;

struct DSU{
	int path[200200], valid[200200], n;
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq2[200200];

	void init(int N){
		n = N;
		for (int i=1;i<=n;i++){
			path[i] = i;
			valid[i] = 1;
		}
	}

	void reset(int x){valid[x] = 0;}

	void push(int x, int y){
		pq2[x].emplace(a[x]+a[y], y);
		pq2[y].emplace(a[x]+a[y], x);
	}

	void init2(){
		for (int i=1;i<=n;i++) if (valid[i] && !pq2[i].empty()){
			auto [cst, v] = pq2[i].top();
			pq.push({cst - a[i] - a[mn], cst, v, i});
		}
	}

	int find(int s){
		if (path[s]==s) return s;
		return path[s] = find(path[s]);
	}

	void merge(int s, int v){
		s = find(s), v = find(v);
		assert(s!=v);
		assert(valid[v]);

		path[v] = s;
		if (!valid[s]) return;

		if (pq2[s].size() < pq2[v].size()) swap(pq2[s], pq2[v]);
		while(!pq2[v].empty()){
			pq2[s].push(pq2[v].top()); pq2[v].pop();
		}

		while(!pq2[s].empty() && find(pq2[s].top().second)==s) pq2[s].pop();
		if (!pq2[s].empty()){
			auto [cst, nxt] = pq2[s].top();
			pq.push({cst - a[s] - a[mn], cst, nxt, s});
		} 

	}
}dsu;

bool valid(const array<ll, 4> &p){
	if (dsu.find(p[2]) == dsu.find(p[3])) return 0;
	return p[1] - a[mn] - a[dsu.find(p[3])] == p[0];
}

int main(){
	int n, m, q;
	scanf("%d %d %d", &n, &m, &q);
	for (int i=1;i<=n;i++) scanf("%lld", a+i);
	for (int i=1;i<=m;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		E[i] = {x, y};
	}

	mn = min_element(a+1, a+n+1) - a;
	mx = max_element(a+1, a+n+1) - a;
	dsu.init(n);

	int sz = n-1;

	for (int i=1;i<=m;i++){
		if (E[i].first==mn || E[i].second==mn){
			if (E[i].second==mn) swap(E[i].first, E[i].second);
			dsu.reset(E[i].second);
			sz--;
		}

		else{
			dsu.push(E[i].first, E[i].second);
		}
	}

	dsu.init2();

	for (int i=1;i<=n;i++) ans[sz] += a[i];
	ans[sz] += a[mn] * (n-2);

	for (int i=sz-1;i>=0;i--){
		while(!pq.empty() && !valid(pq.top())) pq.pop();
		assert(!pq.empty());
		auto [cst, _, u, v] = pq.top(); pq.pop();

		dsu.merge(u, v);
		ans[i] = ans[i+1] + cst;
	}

	ll ofs = a[mx];
	for (int i=1;i<=n;i++) ofs -= a[i];

	for (int i=0;i<=q;i++) printf("%lld\n", ans[min(i, sz)] + ofs);
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
guard.cpp:75:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |  for (int i=1;i<=n;i++) scanf("%lld", a+i);
      |                         ~~~~~^~~~~~~~~~~~~
guard.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 93 ms 28912 KB Output is correct
3 Correct 93 ms 28984 KB Output is correct
4 Correct 163 ms 35260 KB Output is correct
5 Correct 165 ms 35148 KB Output is correct
6 Correct 158 ms 35180 KB Output is correct
7 Correct 173 ms 35184 KB Output is correct
8 Correct 3 ms 6564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 93 ms 28912 KB Output is correct
3 Correct 93 ms 28984 KB Output is correct
4 Correct 163 ms 35260 KB Output is correct
5 Correct 165 ms 35148 KB Output is correct
6 Correct 158 ms 35180 KB Output is correct
7 Correct 173 ms 35184 KB Output is correct
8 Correct 3 ms 6564 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 230 ms 35732 KB Output is correct
11 Correct 96 ms 28904 KB Output is correct
12 Correct 103 ms 28976 KB Output is correct
13 Correct 92 ms 28964 KB Output is correct
14 Correct 266 ms 35720 KB Output is correct
15 Correct 228 ms 35716 KB Output is correct
16 Correct 228 ms 35732 KB Output is correct
17 Correct 234 ms 35688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 93 ms 28912 KB Output is correct
3 Correct 93 ms 28984 KB Output is correct
4 Correct 163 ms 35260 KB Output is correct
5 Correct 165 ms 35148 KB Output is correct
6 Correct 158 ms 35180 KB Output is correct
7 Correct 173 ms 35184 KB Output is correct
8 Correct 3 ms 6564 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 230 ms 35732 KB Output is correct
11 Correct 96 ms 28904 KB Output is correct
12 Correct 103 ms 28976 KB Output is correct
13 Correct 92 ms 28964 KB Output is correct
14 Correct 266 ms 35720 KB Output is correct
15 Correct 228 ms 35716 KB Output is correct
16 Correct 228 ms 35732 KB Output is correct
17 Correct 234 ms 35688 KB Output is correct
18 Correct 3 ms 6484 KB Output is correct
19 Correct 307 ms 36152 KB Output is correct
20 Correct 316 ms 36248 KB Output is correct
21 Correct 344 ms 35920 KB Output is correct
22 Correct 299 ms 36020 KB Output is correct
23 Correct 313 ms 34964 KB Output is correct
24 Correct 290 ms 33828 KB Output is correct
25 Correct 195 ms 29968 KB Output is correct
26 Correct 183 ms 28952 KB Output is correct
27 Correct 143 ms 28920 KB Output is correct
28 Correct 324 ms 34432 KB Output is correct
29 Correct 284 ms 33048 KB Output is correct
30 Correct 236 ms 29900 KB Output is correct
31 Correct 138 ms 28920 KB Output is correct
32 Correct 304 ms 35916 KB Output is correct
33 Correct 271 ms 33340 KB Output is correct
34 Correct 302 ms 72692 KB Output is correct
35 Correct 281 ms 68500 KB Output is correct
36 Correct 219 ms 51236 KB Output is correct
37 Correct 207 ms 46888 KB Output is correct
38 Correct 226 ms 30692 KB Output is correct
39 Correct 145 ms 29344 KB Output is correct
40 Correct 261 ms 29688 KB Output is correct
41 Correct 177 ms 30016 KB Output is correct
42 Correct 298 ms 36832 KB Output is correct
43 Runtime error 313 ms 68084 KB Execution killed with signal 6
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 93 ms 28912 KB Output is correct
3 Correct 93 ms 28984 KB Output is correct
4 Correct 163 ms 35260 KB Output is correct
5 Correct 165 ms 35148 KB Output is correct
6 Correct 158 ms 35180 KB Output is correct
7 Correct 173 ms 35184 KB Output is correct
8 Correct 3 ms 6564 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 230 ms 35732 KB Output is correct
11 Correct 96 ms 28904 KB Output is correct
12 Correct 103 ms 28976 KB Output is correct
13 Correct 92 ms 28964 KB Output is correct
14 Correct 266 ms 35720 KB Output is correct
15 Correct 228 ms 35716 KB Output is correct
16 Correct 228 ms 35732 KB Output is correct
17 Correct 234 ms 35688 KB Output is correct
18 Correct 3 ms 6484 KB Output is correct
19 Correct 307 ms 36152 KB Output is correct
20 Correct 316 ms 36248 KB Output is correct
21 Correct 344 ms 35920 KB Output is correct
22 Correct 299 ms 36020 KB Output is correct
23 Correct 313 ms 34964 KB Output is correct
24 Correct 290 ms 33828 KB Output is correct
25 Correct 195 ms 29968 KB Output is correct
26 Correct 183 ms 28952 KB Output is correct
27 Correct 143 ms 28920 KB Output is correct
28 Correct 324 ms 34432 KB Output is correct
29 Correct 284 ms 33048 KB Output is correct
30 Correct 236 ms 29900 KB Output is correct
31 Correct 138 ms 28920 KB Output is correct
32 Correct 304 ms 35916 KB Output is correct
33 Correct 271 ms 33340 KB Output is correct
34 Correct 302 ms 72692 KB Output is correct
35 Correct 281 ms 68500 KB Output is correct
36 Correct 219 ms 51236 KB Output is correct
37 Correct 207 ms 46888 KB Output is correct
38 Correct 226 ms 30692 KB Output is correct
39 Correct 145 ms 29344 KB Output is correct
40 Correct 261 ms 29688 KB Output is correct
41 Correct 177 ms 30016 KB Output is correct
42 Correct 298 ms 36832 KB Output is correct
43 Runtime error 313 ms 68084 KB Execution killed with signal 6
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Correct 3 ms 6484 KB Output is correct
4 Correct 4 ms 6484 KB Output is correct
5 Correct 3 ms 6484 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 20 ms 8784 KB Output is correct
9 Correct 31 ms 8728 KB Output is correct
10 Correct 19 ms 8532 KB Output is correct
11 Correct 24 ms 8812 KB Output is correct
12 Correct 20 ms 8788 KB Output is correct
13 Correct 19 ms 8772 KB Output is correct
14 Correct 20 ms 8808 KB Output is correct
15 Correct 26 ms 8724 KB Output is correct
16 Correct 19 ms 8744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6484 KB Output is correct
2 Correct 3 ms 6484 KB Output is correct
3 Correct 3 ms 6484 KB Output is correct
4 Correct 4 ms 6484 KB Output is correct
5 Correct 3 ms 6484 KB Output is correct
6 Correct 3 ms 6484 KB Output is correct
7 Correct 3 ms 6484 KB Output is correct
8 Correct 20 ms 8784 KB Output is correct
9 Correct 31 ms 8728 KB Output is correct
10 Correct 19 ms 8532 KB Output is correct
11 Correct 24 ms 8812 KB Output is correct
12 Correct 20 ms 8788 KB Output is correct
13 Correct 19 ms 8772 KB Output is correct
14 Correct 20 ms 8808 KB Output is correct
15 Correct 26 ms 8724 KB Output is correct
16 Correct 19 ms 8744 KB Output is correct
17 Correct 39 ms 12316 KB Output is correct
18 Correct 36 ms 12448 KB Output is correct
19 Correct 71 ms 19184 KB Output is correct
20 Correct 116 ms 29536 KB Output is correct
21 Correct 149 ms 35280 KB Output is correct
22 Correct 112 ms 24424 KB Output is correct
23 Correct 112 ms 36924 KB Output is correct
24 Correct 121 ms 32816 KB Output is correct
25 Correct 24 ms 9292 KB Output is correct
26 Correct 155 ms 37200 KB Output is correct
27 Correct 42 ms 15080 KB Output is correct
28 Correct 22 ms 9244 KB Output is correct
29 Correct 21 ms 9284 KB Output is correct
30 Correct 22 ms 9292 KB Output is correct
31 Correct 19 ms 8132 KB Output is correct
32 Correct 20 ms 8064 KB Output is correct
33 Correct 23 ms 9752 KB Output is correct
34 Correct 27 ms 10276 KB Output is correct
35 Correct 74 ms 22460 KB Output is correct
36 Correct 174 ms 52680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Correct 93 ms 28912 KB Output is correct
3 Correct 93 ms 28984 KB Output is correct
4 Correct 163 ms 35260 KB Output is correct
5 Correct 165 ms 35148 KB Output is correct
6 Correct 158 ms 35180 KB Output is correct
7 Correct 173 ms 35184 KB Output is correct
8 Correct 3 ms 6564 KB Output is correct
9 Correct 3 ms 6484 KB Output is correct
10 Correct 230 ms 35732 KB Output is correct
11 Correct 96 ms 28904 KB Output is correct
12 Correct 103 ms 28976 KB Output is correct
13 Correct 92 ms 28964 KB Output is correct
14 Correct 266 ms 35720 KB Output is correct
15 Correct 228 ms 35716 KB Output is correct
16 Correct 228 ms 35732 KB Output is correct
17 Correct 234 ms 35688 KB Output is correct
18 Correct 3 ms 6484 KB Output is correct
19 Correct 307 ms 36152 KB Output is correct
20 Correct 316 ms 36248 KB Output is correct
21 Correct 344 ms 35920 KB Output is correct
22 Correct 299 ms 36020 KB Output is correct
23 Correct 313 ms 34964 KB Output is correct
24 Correct 290 ms 33828 KB Output is correct
25 Correct 195 ms 29968 KB Output is correct
26 Correct 183 ms 28952 KB Output is correct
27 Correct 143 ms 28920 KB Output is correct
28 Correct 324 ms 34432 KB Output is correct
29 Correct 284 ms 33048 KB Output is correct
30 Correct 236 ms 29900 KB Output is correct
31 Correct 138 ms 28920 KB Output is correct
32 Correct 304 ms 35916 KB Output is correct
33 Correct 271 ms 33340 KB Output is correct
34 Correct 302 ms 72692 KB Output is correct
35 Correct 281 ms 68500 KB Output is correct
36 Correct 219 ms 51236 KB Output is correct
37 Correct 207 ms 46888 KB Output is correct
38 Correct 226 ms 30692 KB Output is correct
39 Correct 145 ms 29344 KB Output is correct
40 Correct 261 ms 29688 KB Output is correct
41 Correct 177 ms 30016 KB Output is correct
42 Correct 298 ms 36832 KB Output is correct
43 Runtime error 313 ms 68084 KB Execution killed with signal 6
44 Halted 0 ms 0 KB -