#include <bits/stdc++.h>
using namespace std;
#define int long long
struct UFDS{
vector<int> p,siz,mini;
UFDS(int n,vector<int> arr):p(n+1),siz(n+1,1),mini(n+1){
iota(p.begin(),p.end(),0);
mini=arr;
}
int findRoot(int x){
return p[x]==x ? x : p[x]=findRoot(p[x]);
}
int unite(int a,int b){
a = findRoot(a);
b = findRoot(b);
if(a==b)return -1;
if(siz[b]>siz[a])swap(a,b);
int ans = mini[a]+mini[b];
int maxi = max(mini[a],mini[b]);
mini[a]=min(mini[a],mini[b]);
siz[a]+=siz[b];
p[b]=a;
return ans;
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,q;
cin >> n >> m >> q;
vector<int> arr(n+1);
for(int i=1;i<=n;i++)cin>>arr[i];
vector<tuple<int,int,int>> edges;
for(int i=1;i<=m;i++){
int a,b;
cin >> a >> b;
edges.emplace_back(arr[a]+arr[b],a,b);
}
sort(edges.begin(),edges.end());
int baseans = (*max_element(arr.begin(),arr.end()));
for(int i=1;i<=n;i++){
baseans-=arr[i];
}
UFDS dsu(n,arr);
vector<int> ans(n);
int minima = *min_element(arr.begin()+1,arr.end());
int iter = 1;
priority_queue<int> qu;
for(int i=0;i<m;i++){
auto [cost,a,b] = edges[i];
auto curr = dsu.unite(a,b);
if(curr==-1)continue;
baseans+=cost;
qu.emplace(curr-cost);
}
for(int i=0;i<=q;i++){
cout << baseans << '\n';
if(!qu.empty()){
baseans+=qu.top();
qu.pop();
}
}
}
Compilation message
guard.cpp: In member function 'long long int UFDS::unite(long long int, long long int)':
guard.cpp:21:7: warning: unused variable 'maxi' [-Wunused-variable]
21 | int maxi = max(mini[a],mini[b]);
| ^~~~
guard.cpp: In function 'int32_t main()':
guard.cpp:50:6: warning: unused variable 'minima' [-Wunused-variable]
50 | int minima = *min_element(arr.begin()+1,arr.end());
| ^~~~~~
guard.cpp:51:6: warning: unused variable 'iter' [-Wunused-variable]
51 | int iter = 1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
41 ms |
16576 KB |
Output is correct |
3 |
Correct |
44 ms |
16568 KB |
Output is correct |
4 |
Correct |
64 ms |
16564 KB |
Output is correct |
5 |
Correct |
50 ms |
16572 KB |
Output is correct |
6 |
Correct |
55 ms |
16568 KB |
Output is correct |
7 |
Correct |
51 ms |
16540 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
41 ms |
16576 KB |
Output is correct |
3 |
Correct |
44 ms |
16568 KB |
Output is correct |
4 |
Correct |
64 ms |
16564 KB |
Output is correct |
5 |
Correct |
50 ms |
16572 KB |
Output is correct |
6 |
Correct |
55 ms |
16568 KB |
Output is correct |
7 |
Correct |
51 ms |
16540 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
59 ms |
16512 KB |
Output is correct |
11 |
Correct |
48 ms |
16564 KB |
Output is correct |
12 |
Correct |
43 ms |
16568 KB |
Output is correct |
13 |
Correct |
44 ms |
16572 KB |
Output is correct |
14 |
Correct |
73 ms |
16572 KB |
Output is correct |
15 |
Correct |
58 ms |
16460 KB |
Output is correct |
16 |
Correct |
60 ms |
16572 KB |
Output is correct |
17 |
Correct |
59 ms |
16572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
41 ms |
16576 KB |
Output is correct |
3 |
Correct |
44 ms |
16568 KB |
Output is correct |
4 |
Correct |
64 ms |
16564 KB |
Output is correct |
5 |
Correct |
50 ms |
16572 KB |
Output is correct |
6 |
Correct |
55 ms |
16568 KB |
Output is correct |
7 |
Correct |
51 ms |
16540 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
59 ms |
16512 KB |
Output is correct |
11 |
Correct |
48 ms |
16564 KB |
Output is correct |
12 |
Correct |
43 ms |
16568 KB |
Output is correct |
13 |
Correct |
44 ms |
16572 KB |
Output is correct |
14 |
Correct |
73 ms |
16572 KB |
Output is correct |
15 |
Correct |
58 ms |
16460 KB |
Output is correct |
16 |
Correct |
60 ms |
16572 KB |
Output is correct |
17 |
Correct |
59 ms |
16572 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
65 ms |
16528 KB |
Output is correct |
20 |
Correct |
63 ms |
16404 KB |
Output is correct |
21 |
Correct |
62 ms |
16568 KB |
Output is correct |
22 |
Correct |
62 ms |
16576 KB |
Output is correct |
23 |
Correct |
65 ms |
16572 KB |
Output is correct |
24 |
Correct |
60 ms |
16572 KB |
Output is correct |
25 |
Correct |
63 ms |
16576 KB |
Output is correct |
26 |
Correct |
60 ms |
16568 KB |
Output is correct |
27 |
Correct |
58 ms |
16568 KB |
Output is correct |
28 |
Correct |
62 ms |
16576 KB |
Output is correct |
29 |
Correct |
61 ms |
16576 KB |
Output is correct |
30 |
Correct |
60 ms |
16568 KB |
Output is correct |
31 |
Correct |
62 ms |
16560 KB |
Output is correct |
32 |
Correct |
60 ms |
16564 KB |
Output is correct |
33 |
Correct |
61 ms |
16568 KB |
Output is correct |
34 |
Correct |
66 ms |
16564 KB |
Output is correct |
35 |
Correct |
63 ms |
16568 KB |
Output is correct |
36 |
Correct |
62 ms |
16396 KB |
Output is correct |
37 |
Correct |
62 ms |
16568 KB |
Output is correct |
38 |
Correct |
62 ms |
16504 KB |
Output is correct |
39 |
Correct |
58 ms |
16576 KB |
Output is correct |
40 |
Correct |
61 ms |
16568 KB |
Output is correct |
41 |
Correct |
56 ms |
16492 KB |
Output is correct |
42 |
Correct |
57 ms |
16572 KB |
Output is correct |
43 |
Correct |
79 ms |
16568 KB |
Output is correct |
44 |
Correct |
65 ms |
16464 KB |
Output is correct |
45 |
Correct |
83 ms |
16568 KB |
Output is correct |
46 |
Correct |
67 ms |
16568 KB |
Output is correct |
47 |
Correct |
67 ms |
16528 KB |
Output is correct |
48 |
Correct |
65 ms |
16364 KB |
Output is correct |
49 |
Correct |
68 ms |
16460 KB |
Output is correct |
50 |
Correct |
62 ms |
16500 KB |
Output is correct |
51 |
Correct |
65 ms |
16504 KB |
Output is correct |
52 |
Correct |
58 ms |
16568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
41 ms |
16576 KB |
Output is correct |
3 |
Correct |
44 ms |
16568 KB |
Output is correct |
4 |
Correct |
64 ms |
16564 KB |
Output is correct |
5 |
Correct |
50 ms |
16572 KB |
Output is correct |
6 |
Correct |
55 ms |
16568 KB |
Output is correct |
7 |
Correct |
51 ms |
16540 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
59 ms |
16512 KB |
Output is correct |
11 |
Correct |
48 ms |
16564 KB |
Output is correct |
12 |
Correct |
43 ms |
16568 KB |
Output is correct |
13 |
Correct |
44 ms |
16572 KB |
Output is correct |
14 |
Correct |
73 ms |
16572 KB |
Output is correct |
15 |
Correct |
58 ms |
16460 KB |
Output is correct |
16 |
Correct |
60 ms |
16572 KB |
Output is correct |
17 |
Correct |
59 ms |
16572 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
65 ms |
16528 KB |
Output is correct |
20 |
Correct |
63 ms |
16404 KB |
Output is correct |
21 |
Correct |
62 ms |
16568 KB |
Output is correct |
22 |
Correct |
62 ms |
16576 KB |
Output is correct |
23 |
Correct |
65 ms |
16572 KB |
Output is correct |
24 |
Correct |
60 ms |
16572 KB |
Output is correct |
25 |
Correct |
63 ms |
16576 KB |
Output is correct |
26 |
Correct |
60 ms |
16568 KB |
Output is correct |
27 |
Correct |
58 ms |
16568 KB |
Output is correct |
28 |
Correct |
62 ms |
16576 KB |
Output is correct |
29 |
Correct |
61 ms |
16576 KB |
Output is correct |
30 |
Correct |
60 ms |
16568 KB |
Output is correct |
31 |
Correct |
62 ms |
16560 KB |
Output is correct |
32 |
Correct |
60 ms |
16564 KB |
Output is correct |
33 |
Correct |
61 ms |
16568 KB |
Output is correct |
34 |
Correct |
66 ms |
16564 KB |
Output is correct |
35 |
Correct |
63 ms |
16568 KB |
Output is correct |
36 |
Correct |
62 ms |
16396 KB |
Output is correct |
37 |
Correct |
62 ms |
16568 KB |
Output is correct |
38 |
Correct |
62 ms |
16504 KB |
Output is correct |
39 |
Correct |
58 ms |
16576 KB |
Output is correct |
40 |
Correct |
61 ms |
16568 KB |
Output is correct |
41 |
Correct |
56 ms |
16492 KB |
Output is correct |
42 |
Correct |
57 ms |
16572 KB |
Output is correct |
43 |
Correct |
79 ms |
16568 KB |
Output is correct |
44 |
Correct |
65 ms |
16464 KB |
Output is correct |
45 |
Correct |
83 ms |
16568 KB |
Output is correct |
46 |
Correct |
67 ms |
16568 KB |
Output is correct |
47 |
Correct |
67 ms |
16528 KB |
Output is correct |
48 |
Correct |
65 ms |
16364 KB |
Output is correct |
49 |
Correct |
68 ms |
16460 KB |
Output is correct |
50 |
Correct |
62 ms |
16500 KB |
Output is correct |
51 |
Correct |
65 ms |
16504 KB |
Output is correct |
52 |
Correct |
58 ms |
16568 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
65 ms |
16680 KB |
Output is correct |
55 |
Correct |
64 ms |
16828 KB |
Output is correct |
56 |
Correct |
76 ms |
17840 KB |
Output is correct |
57 |
Correct |
108 ms |
21160 KB |
Output is correct |
58 |
Correct |
108 ms |
21156 KB |
Output is correct |
59 |
Correct |
105 ms |
21228 KB |
Output is correct |
60 |
Correct |
82 ms |
18852 KB |
Output is correct |
61 |
Correct |
102 ms |
21156 KB |
Output is correct |
62 |
Correct |
101 ms |
21264 KB |
Output is correct |
63 |
Correct |
64 ms |
16824 KB |
Output is correct |
64 |
Correct |
85 ms |
18592 KB |
Output is correct |
65 |
Correct |
100 ms |
21224 KB |
Output is correct |
66 |
Correct |
99 ms |
21160 KB |
Output is correct |
67 |
Correct |
99 ms |
21260 KB |
Output is correct |
68 |
Correct |
70 ms |
16488 KB |
Output is correct |
69 |
Correct |
97 ms |
19364 KB |
Output is correct |
70 |
Correct |
66 ms |
17080 KB |
Output is correct |
71 |
Correct |
67 ms |
16596 KB |
Output is correct |
72 |
Correct |
72 ms |
16572 KB |
Output is correct |
73 |
Correct |
62 ms |
16564 KB |
Output is correct |
74 |
Correct |
60 ms |
16576 KB |
Output is correct |
75 |
Correct |
59 ms |
16464 KB |
Output is correct |
76 |
Correct |
58 ms |
16568 KB |
Output is correct |
77 |
Correct |
58 ms |
16576 KB |
Output is correct |
78 |
Correct |
69 ms |
16820 KB |
Output is correct |
79 |
Correct |
106 ms |
21212 KB |
Output is correct |
80 |
Correct |
95 ms |
17736 KB |
Output is correct |
81 |
Correct |
106 ms |
20304 KB |
Output is correct |
82 |
Correct |
116 ms |
21156 KB |
Output is correct |
83 |
Correct |
107 ms |
21116 KB |
Output is correct |
84 |
Correct |
111 ms |
21160 KB |
Output is correct |
85 |
Correct |
66 ms |
16568 KB |
Output is correct |
86 |
Correct |
65 ms |
16564 KB |
Output is correct |
87 |
Correct |
68 ms |
16548 KB |
Output is correct |
88 |
Correct |
104 ms |
21280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
41 ms |
16576 KB |
Output is correct |
3 |
Correct |
44 ms |
16568 KB |
Output is correct |
4 |
Correct |
64 ms |
16564 KB |
Output is correct |
5 |
Correct |
50 ms |
16572 KB |
Output is correct |
6 |
Correct |
55 ms |
16568 KB |
Output is correct |
7 |
Correct |
51 ms |
16540 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
59 ms |
16512 KB |
Output is correct |
11 |
Correct |
48 ms |
16564 KB |
Output is correct |
12 |
Correct |
43 ms |
16568 KB |
Output is correct |
13 |
Correct |
44 ms |
16572 KB |
Output is correct |
14 |
Correct |
73 ms |
16572 KB |
Output is correct |
15 |
Correct |
58 ms |
16460 KB |
Output is correct |
16 |
Correct |
60 ms |
16572 KB |
Output is correct |
17 |
Correct |
59 ms |
16572 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
65 ms |
16528 KB |
Output is correct |
20 |
Correct |
63 ms |
16404 KB |
Output is correct |
21 |
Correct |
62 ms |
16568 KB |
Output is correct |
22 |
Correct |
62 ms |
16576 KB |
Output is correct |
23 |
Correct |
65 ms |
16572 KB |
Output is correct |
24 |
Correct |
60 ms |
16572 KB |
Output is correct |
25 |
Correct |
63 ms |
16576 KB |
Output is correct |
26 |
Correct |
60 ms |
16568 KB |
Output is correct |
27 |
Correct |
58 ms |
16568 KB |
Output is correct |
28 |
Correct |
62 ms |
16576 KB |
Output is correct |
29 |
Correct |
61 ms |
16576 KB |
Output is correct |
30 |
Correct |
60 ms |
16568 KB |
Output is correct |
31 |
Correct |
62 ms |
16560 KB |
Output is correct |
32 |
Correct |
60 ms |
16564 KB |
Output is correct |
33 |
Correct |
61 ms |
16568 KB |
Output is correct |
34 |
Correct |
66 ms |
16564 KB |
Output is correct |
35 |
Correct |
63 ms |
16568 KB |
Output is correct |
36 |
Correct |
62 ms |
16396 KB |
Output is correct |
37 |
Correct |
62 ms |
16568 KB |
Output is correct |
38 |
Correct |
62 ms |
16504 KB |
Output is correct |
39 |
Correct |
58 ms |
16576 KB |
Output is correct |
40 |
Correct |
61 ms |
16568 KB |
Output is correct |
41 |
Correct |
56 ms |
16492 KB |
Output is correct |
42 |
Correct |
57 ms |
16572 KB |
Output is correct |
43 |
Correct |
79 ms |
16568 KB |
Output is correct |
44 |
Correct |
65 ms |
16464 KB |
Output is correct |
45 |
Correct |
83 ms |
16568 KB |
Output is correct |
46 |
Correct |
67 ms |
16568 KB |
Output is correct |
47 |
Correct |
67 ms |
16528 KB |
Output is correct |
48 |
Correct |
65 ms |
16364 KB |
Output is correct |
49 |
Correct |
68 ms |
16460 KB |
Output is correct |
50 |
Correct |
62 ms |
16500 KB |
Output is correct |
51 |
Correct |
65 ms |
16504 KB |
Output is correct |
52 |
Correct |
58 ms |
16568 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
65 ms |
16680 KB |
Output is correct |
55 |
Correct |
64 ms |
16828 KB |
Output is correct |
56 |
Correct |
76 ms |
17840 KB |
Output is correct |
57 |
Correct |
108 ms |
21160 KB |
Output is correct |
58 |
Correct |
108 ms |
21156 KB |
Output is correct |
59 |
Correct |
105 ms |
21228 KB |
Output is correct |
60 |
Correct |
82 ms |
18852 KB |
Output is correct |
61 |
Correct |
102 ms |
21156 KB |
Output is correct |
62 |
Correct |
101 ms |
21264 KB |
Output is correct |
63 |
Correct |
64 ms |
16824 KB |
Output is correct |
64 |
Correct |
85 ms |
18592 KB |
Output is correct |
65 |
Correct |
100 ms |
21224 KB |
Output is correct |
66 |
Correct |
99 ms |
21160 KB |
Output is correct |
67 |
Correct |
99 ms |
21260 KB |
Output is correct |
68 |
Correct |
70 ms |
16488 KB |
Output is correct |
69 |
Correct |
97 ms |
19364 KB |
Output is correct |
70 |
Correct |
66 ms |
17080 KB |
Output is correct |
71 |
Correct |
67 ms |
16596 KB |
Output is correct |
72 |
Correct |
72 ms |
16572 KB |
Output is correct |
73 |
Correct |
62 ms |
16564 KB |
Output is correct |
74 |
Correct |
60 ms |
16576 KB |
Output is correct |
75 |
Correct |
59 ms |
16464 KB |
Output is correct |
76 |
Correct |
58 ms |
16568 KB |
Output is correct |
77 |
Correct |
58 ms |
16576 KB |
Output is correct |
78 |
Correct |
69 ms |
16820 KB |
Output is correct |
79 |
Correct |
106 ms |
21212 KB |
Output is correct |
80 |
Correct |
95 ms |
17736 KB |
Output is correct |
81 |
Correct |
106 ms |
20304 KB |
Output is correct |
82 |
Correct |
116 ms |
21156 KB |
Output is correct |
83 |
Correct |
107 ms |
21116 KB |
Output is correct |
84 |
Correct |
111 ms |
21160 KB |
Output is correct |
85 |
Correct |
66 ms |
16568 KB |
Output is correct |
86 |
Correct |
65 ms |
16564 KB |
Output is correct |
87 |
Correct |
68 ms |
16548 KB |
Output is correct |
88 |
Correct |
104 ms |
21280 KB |
Output is correct |
89 |
Correct |
0 ms |
348 KB |
Output is correct |
90 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
91 |
Halted |
0 ms |
0 KB |
- |