Submission #796101

# Submission time Handle Problem Language Result Execution time Memory
796101 2023-07-28T06:25:39 Z 박상훈(#10069) Security Guard (JOI23_guard) C++17
50 / 100
418 ms 41140 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr ll INF2 = 4e18;

ll ans;

struct DSU{
	int path[200200];
	void init(int n){for (int i=1;i<=n;i++) path[i] = i;}
	int find(int s){
		if (s==path[s]) return s;
		return path[s] = find(path[s]);
	}
	int merge(int s, int v){
		s = find(s), v = find(v);
		if (s==v) return 0;
		path[v] = s;
		return 1;
	}
}dsu;

vector<int> adj[200200], G[200200];
ll a[200200];

void dfs(int s, int pa){
	ans += a[pa];
	for (auto &v:G[s]) if (v!=pa) dfs(v, s);
}

int main(){
	int n, m, q;
	scanf("%d %d %d", &n, &m, &q);

	for (int i=1;i<=n;i++) scanf("%lld", a+i);
	int root = max_element(a+1, a+n+1) - a;
	
	for (int i=1;i<=m;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	if (q > 0) q = 0;
	
	dsu.init(n);

	for (int i=1;i<=n;i++) if (i!=root){
		ll mn = INF2;
		int idx = -1;

		for (auto &v:adj[i]) if (v!=root){
			if (mn > a[v]){
				mn = a[v];
				idx = v;
			}
		}

		if (idx!=-1 && dsu.merge(i, idx)){
			G[i].push_back(idx);
			G[idx].push_back(i);
			// printf("push %d %d\n", i, idx);
		}

		// printf("ok %d -> %d with %lld\n", i, idx, a[idx]);
	}

	vector<array<ll, 3>> E;

	for (int i=1;i<=n;i++){
		for (auto &v:adj[i]) if (dsu.find(i) != dsu.find(v)){
			E.push_back({a[i] + a[v], i, v});
			// printf(" ok %d %d / %lld\n", i, v, a[i] + a[v]);
		}
	}

	sort(E.begin(), E.end());

	for (auto &[z, x, y]:E) if (dsu.merge(x, y)){
		// printf("push %lld %lld\n", x, y);
		G[x].push_back(y);
		G[y].push_back(x);
	}

	ans = 0;
	for (auto &x:G[root]) dfs(x, root);

	printf("%lld\n", ans);
}

Compilation message

guard.cpp: In function 'int main()':
guard.cpp:35:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |  scanf("%d %d %d", &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
guard.cpp:37:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |  for (int i=1;i<=n;i++) scanf("%lld", a+i);
      |                         ~~~~~^~~~~~~~~~~~~
guard.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 83 ms 30772 KB Output is correct
3 Correct 85 ms 30736 KB Output is correct
4 Correct 95 ms 32600 KB Output is correct
5 Correct 100 ms 32700 KB Output is correct
6 Correct 95 ms 32600 KB Output is correct
7 Correct 95 ms 32664 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 83 ms 30772 KB Output is correct
3 Correct 85 ms 30736 KB Output is correct
4 Correct 95 ms 32600 KB Output is correct
5 Correct 100 ms 32700 KB Output is correct
6 Correct 95 ms 32600 KB Output is correct
7 Correct 95 ms 32664 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 109 ms 30880 KB Output is correct
11 Correct 88 ms 30720 KB Output is correct
12 Correct 101 ms 30716 KB Output is correct
13 Correct 87 ms 30712 KB Output is correct
14 Correct 115 ms 33244 KB Output is correct
15 Correct 116 ms 33236 KB Output is correct
16 Correct 115 ms 32408 KB Output is correct
17 Correct 117 ms 32872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 83 ms 30772 KB Output is correct
3 Correct 85 ms 30736 KB Output is correct
4 Correct 95 ms 32600 KB Output is correct
5 Correct 100 ms 32700 KB Output is correct
6 Correct 95 ms 32600 KB Output is correct
7 Correct 95 ms 32664 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 109 ms 30880 KB Output is correct
11 Correct 88 ms 30720 KB Output is correct
12 Correct 101 ms 30716 KB Output is correct
13 Correct 87 ms 30712 KB Output is correct
14 Correct 115 ms 33244 KB Output is correct
15 Correct 116 ms 33236 KB Output is correct
16 Correct 115 ms 32408 KB Output is correct
17 Correct 117 ms 32872 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 213 ms 30012 KB Output is correct
20 Correct 220 ms 29464 KB Output is correct
21 Correct 195 ms 27884 KB Output is correct
22 Correct 178 ms 28008 KB Output is correct
23 Correct 131 ms 26444 KB Output is correct
24 Correct 116 ms 26740 KB Output is correct
25 Correct 137 ms 26756 KB Output is correct
26 Correct 109 ms 27164 KB Output is correct
27 Correct 102 ms 28568 KB Output is correct
28 Correct 187 ms 31840 KB Output is correct
29 Correct 170 ms 29008 KB Output is correct
30 Correct 121 ms 27888 KB Output is correct
31 Correct 110 ms 27468 KB Output is correct
32 Correct 160 ms 27312 KB Output is correct
33 Correct 173 ms 27712 KB Output is correct
34 Correct 166 ms 25460 KB Output is correct
35 Correct 180 ms 27388 KB Output is correct
36 Correct 128 ms 26604 KB Output is correct
37 Correct 126 ms 26572 KB Output is correct
38 Correct 185 ms 31900 KB Output is correct
39 Correct 144 ms 28424 KB Output is correct
40 Correct 153 ms 28820 KB Output is correct
41 Correct 186 ms 30816 KB Output is correct
42 Correct 201 ms 30720 KB Output is correct
43 Correct 178 ms 27972 KB Output is correct
44 Correct 174 ms 27976 KB Output is correct
45 Correct 176 ms 27964 KB Output is correct
46 Correct 186 ms 27960 KB Output is correct
47 Correct 193 ms 27956 KB Output is correct
48 Correct 228 ms 27960 KB Output is correct
49 Correct 169 ms 24904 KB Output is correct
50 Correct 178 ms 30976 KB Output is correct
51 Correct 188 ms 27940 KB Output is correct
52 Correct 176 ms 28012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 83 ms 30772 KB Output is correct
3 Correct 85 ms 30736 KB Output is correct
4 Correct 95 ms 32600 KB Output is correct
5 Correct 100 ms 32700 KB Output is correct
6 Correct 95 ms 32600 KB Output is correct
7 Correct 95 ms 32664 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 109 ms 30880 KB Output is correct
11 Correct 88 ms 30720 KB Output is correct
12 Correct 101 ms 30716 KB Output is correct
13 Correct 87 ms 30712 KB Output is correct
14 Correct 115 ms 33244 KB Output is correct
15 Correct 116 ms 33236 KB Output is correct
16 Correct 115 ms 32408 KB Output is correct
17 Correct 117 ms 32872 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 213 ms 30012 KB Output is correct
20 Correct 220 ms 29464 KB Output is correct
21 Correct 195 ms 27884 KB Output is correct
22 Correct 178 ms 28008 KB Output is correct
23 Correct 131 ms 26444 KB Output is correct
24 Correct 116 ms 26740 KB Output is correct
25 Correct 137 ms 26756 KB Output is correct
26 Correct 109 ms 27164 KB Output is correct
27 Correct 102 ms 28568 KB Output is correct
28 Correct 187 ms 31840 KB Output is correct
29 Correct 170 ms 29008 KB Output is correct
30 Correct 121 ms 27888 KB Output is correct
31 Correct 110 ms 27468 KB Output is correct
32 Correct 160 ms 27312 KB Output is correct
33 Correct 173 ms 27712 KB Output is correct
34 Correct 166 ms 25460 KB Output is correct
35 Correct 180 ms 27388 KB Output is correct
36 Correct 128 ms 26604 KB Output is correct
37 Correct 126 ms 26572 KB Output is correct
38 Correct 185 ms 31900 KB Output is correct
39 Correct 144 ms 28424 KB Output is correct
40 Correct 153 ms 28820 KB Output is correct
41 Correct 186 ms 30816 KB Output is correct
42 Correct 201 ms 30720 KB Output is correct
43 Correct 178 ms 27972 KB Output is correct
44 Correct 174 ms 27976 KB Output is correct
45 Correct 176 ms 27964 KB Output is correct
46 Correct 186 ms 27960 KB Output is correct
47 Correct 193 ms 27956 KB Output is correct
48 Correct 228 ms 27960 KB Output is correct
49 Correct 169 ms 24904 KB Output is correct
50 Correct 178 ms 30976 KB Output is correct
51 Correct 188 ms 27940 KB Output is correct
52 Correct 176 ms 28012 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Correct 182 ms 30548 KB Output is correct
55 Correct 264 ms 30376 KB Output is correct
56 Correct 306 ms 31788 KB Output is correct
57 Correct 418 ms 39560 KB Output is correct
58 Correct 330 ms 41016 KB Output is correct
59 Correct 294 ms 41140 KB Output is correct
60 Correct 208 ms 31276 KB Output is correct
61 Correct 297 ms 34788 KB Output is correct
62 Correct 246 ms 28952 KB Output is correct
63 Correct 261 ms 32648 KB Output is correct
64 Correct 254 ms 31268 KB Output is correct
65 Correct 282 ms 29252 KB Output is correct
66 Correct 255 ms 29552 KB Output is correct
67 Correct 352 ms 39512 KB Output is correct
68 Correct 218 ms 27968 KB Output is correct
69 Correct 290 ms 38800 KB Output is correct
70 Correct 275 ms 28484 KB Output is correct
71 Correct 227 ms 27000 KB Output is correct
72 Correct 178 ms 26944 KB Output is correct
73 Correct 206 ms 32248 KB Output is correct
74 Correct 184 ms 28816 KB Output is correct
75 Correct 195 ms 29076 KB Output is correct
76 Correct 234 ms 31132 KB Output is correct
77 Correct 238 ms 31052 KB Output is correct
78 Correct 221 ms 28592 KB Output is correct
79 Correct 351 ms 39540 KB Output is correct
80 Correct 220 ms 31876 KB Output is correct
81 Correct 334 ms 39204 KB Output is correct
82 Correct 326 ms 39652 KB Output is correct
83 Correct 330 ms 39556 KB Output is correct
84 Correct 281 ms 26648 KB Output is correct
85 Correct 192 ms 31296 KB Output is correct
86 Correct 224 ms 28352 KB Output is correct
87 Correct 207 ms 28172 KB Output is correct
88 Correct 361 ms 38912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Incorrect 4 ms 9672 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Incorrect 4 ms 9672 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 83 ms 30772 KB Output is correct
3 Correct 85 ms 30736 KB Output is correct
4 Correct 95 ms 32600 KB Output is correct
5 Correct 100 ms 32700 KB Output is correct
6 Correct 95 ms 32600 KB Output is correct
7 Correct 95 ms 32664 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 109 ms 30880 KB Output is correct
11 Correct 88 ms 30720 KB Output is correct
12 Correct 101 ms 30716 KB Output is correct
13 Correct 87 ms 30712 KB Output is correct
14 Correct 115 ms 33244 KB Output is correct
15 Correct 116 ms 33236 KB Output is correct
16 Correct 115 ms 32408 KB Output is correct
17 Correct 117 ms 32872 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 213 ms 30012 KB Output is correct
20 Correct 220 ms 29464 KB Output is correct
21 Correct 195 ms 27884 KB Output is correct
22 Correct 178 ms 28008 KB Output is correct
23 Correct 131 ms 26444 KB Output is correct
24 Correct 116 ms 26740 KB Output is correct
25 Correct 137 ms 26756 KB Output is correct
26 Correct 109 ms 27164 KB Output is correct
27 Correct 102 ms 28568 KB Output is correct
28 Correct 187 ms 31840 KB Output is correct
29 Correct 170 ms 29008 KB Output is correct
30 Correct 121 ms 27888 KB Output is correct
31 Correct 110 ms 27468 KB Output is correct
32 Correct 160 ms 27312 KB Output is correct
33 Correct 173 ms 27712 KB Output is correct
34 Correct 166 ms 25460 KB Output is correct
35 Correct 180 ms 27388 KB Output is correct
36 Correct 128 ms 26604 KB Output is correct
37 Correct 126 ms 26572 KB Output is correct
38 Correct 185 ms 31900 KB Output is correct
39 Correct 144 ms 28424 KB Output is correct
40 Correct 153 ms 28820 KB Output is correct
41 Correct 186 ms 30816 KB Output is correct
42 Correct 201 ms 30720 KB Output is correct
43 Correct 178 ms 27972 KB Output is correct
44 Correct 174 ms 27976 KB Output is correct
45 Correct 176 ms 27964 KB Output is correct
46 Correct 186 ms 27960 KB Output is correct
47 Correct 193 ms 27956 KB Output is correct
48 Correct 228 ms 27960 KB Output is correct
49 Correct 169 ms 24904 KB Output is correct
50 Correct 178 ms 30976 KB Output is correct
51 Correct 188 ms 27940 KB Output is correct
52 Correct 176 ms 28012 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Correct 182 ms 30548 KB Output is correct
55 Correct 264 ms 30376 KB Output is correct
56 Correct 306 ms 31788 KB Output is correct
57 Correct 418 ms 39560 KB Output is correct
58 Correct 330 ms 41016 KB Output is correct
59 Correct 294 ms 41140 KB Output is correct
60 Correct 208 ms 31276 KB Output is correct
61 Correct 297 ms 34788 KB Output is correct
62 Correct 246 ms 28952 KB Output is correct
63 Correct 261 ms 32648 KB Output is correct
64 Correct 254 ms 31268 KB Output is correct
65 Correct 282 ms 29252 KB Output is correct
66 Correct 255 ms 29552 KB Output is correct
67 Correct 352 ms 39512 KB Output is correct
68 Correct 218 ms 27968 KB Output is correct
69 Correct 290 ms 38800 KB Output is correct
70 Correct 275 ms 28484 KB Output is correct
71 Correct 227 ms 27000 KB Output is correct
72 Correct 178 ms 26944 KB Output is correct
73 Correct 206 ms 32248 KB Output is correct
74 Correct 184 ms 28816 KB Output is correct
75 Correct 195 ms 29076 KB Output is correct
76 Correct 234 ms 31132 KB Output is correct
77 Correct 238 ms 31052 KB Output is correct
78 Correct 221 ms 28592 KB Output is correct
79 Correct 351 ms 39540 KB Output is correct
80 Correct 220 ms 31876 KB Output is correct
81 Correct 334 ms 39204 KB Output is correct
82 Correct 326 ms 39652 KB Output is correct
83 Correct 330 ms 39556 KB Output is correct
84 Correct 281 ms 26648 KB Output is correct
85 Correct 192 ms 31296 KB Output is correct
86 Correct 224 ms 28352 KB Output is correct
87 Correct 207 ms 28172 KB Output is correct
88 Correct 361 ms 38912 KB Output is correct
89 Correct 4 ms 9684 KB Output is correct
90 Incorrect 4 ms 9672 KB Output isn't correct
91 Halted 0 ms 0 KB -