#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
struct edge{
int from,to,cost;
};
bool cmp(edge fr,edge sc){
return fr.cost<sc.cost;
}
int n,m,q,a,b,s[MAXN],c,d;
vector<edge> w;
unordered_set<int> tree[MAXN];
long long ans;
bool ok;
int dsu[MAXN],sz[MAXN],maxs;
int root(int x){
if(dsu[x]==x)return x;
dsu[x]=root(dsu[x]);
return dsu[x];
}
void mergev(int x,int y){
int rootx=root(x);
int rooty=root(y);
if(sz[rooty]>sz[rootx])swap(rootx,rooty);
sz[rootx]+=sz[rooty];
dsu[rooty]=dsu[rootx];
}
int best(int x,int y){
if(s[x]<s[y])return x;
return y;
}
int dfs(int x,int p){
int res=x;
for(int e:tree[x]){
if(e==p)continue;
res=best(res,dfs(e,x));
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=n;i++){
cin>>s[i];
ans-=s[i];
maxs=max(maxs,s[i]);
dsu[i]=i; sz[i]=1;
}
for(int i=1;i<=m;i++){
cin>>a>>b;
w.push_back({a,b,s[a]+s[b]});
}
sort(w.begin(),w.end(),cmp);
for(int i=0;i<w.size();i++){
if(root(w[i].from)==root(w[i].to))continue;
mergev(w[i].from,w[i].to);
tree[w[i].from].insert(w[i].to);
tree[w[i].to].insert(w[i].from);
ans+=s[w[i].from]+s[w[i].to];
}
ans+=maxs;
cout<<ans<<"\n";
for(int i=1;i<=q;i++){
maxs=0;
for(int f=1;f<=n and !ok;f++){
for(int e:tree[f]){
if(e<f)continue;
int sx=dfs(f,e);
int sy=dfs(e,f);
if(s[f]+s[e]-s[sx]-s[sy]>maxs){
maxs=s[f]+s[e]-s[sx]-s[sy];
a=f; b=e; c=sx; d=sy;
}
}
}
if(maxs!=0){
ans-=maxs;
tree[a].erase(b);
tree[b].erase(a);
tree[c].insert(d);
tree[d].insert(c);
}else ok=true;
cout<<ans<<"\n";
}
return 0;
}
Compilation message
guard.cpp: In function 'int main()':
guard.cpp:75:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i=0;i<w.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
89 ms |
50476 KB |
Output is correct |
3 |
Correct |
86 ms |
50304 KB |
Output is correct |
4 |
Correct |
113 ms |
50364 KB |
Output is correct |
5 |
Correct |
112 ms |
50764 KB |
Output is correct |
6 |
Correct |
124 ms |
50412 KB |
Output is correct |
7 |
Correct |
112 ms |
50344 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
89 ms |
50476 KB |
Output is correct |
3 |
Correct |
86 ms |
50304 KB |
Output is correct |
4 |
Correct |
113 ms |
50364 KB |
Output is correct |
5 |
Correct |
112 ms |
50764 KB |
Output is correct |
6 |
Correct |
124 ms |
50412 KB |
Output is correct |
7 |
Correct |
112 ms |
50344 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
173 ms |
50524 KB |
Output is correct |
11 |
Correct |
102 ms |
50364 KB |
Output is correct |
12 |
Correct |
88 ms |
50684 KB |
Output is correct |
13 |
Correct |
84 ms |
50316 KB |
Output is correct |
14 |
Correct |
169 ms |
50364 KB |
Output is correct |
15 |
Correct |
180 ms |
51132 KB |
Output is correct |
16 |
Correct |
166 ms |
50884 KB |
Output is correct |
17 |
Correct |
165 ms |
50292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
89 ms |
50476 KB |
Output is correct |
3 |
Correct |
86 ms |
50304 KB |
Output is correct |
4 |
Correct |
113 ms |
50364 KB |
Output is correct |
5 |
Correct |
112 ms |
50764 KB |
Output is correct |
6 |
Correct |
124 ms |
50412 KB |
Output is correct |
7 |
Correct |
112 ms |
50344 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
173 ms |
50524 KB |
Output is correct |
11 |
Correct |
102 ms |
50364 KB |
Output is correct |
12 |
Correct |
88 ms |
50684 KB |
Output is correct |
13 |
Correct |
84 ms |
50316 KB |
Output is correct |
14 |
Correct |
169 ms |
50364 KB |
Output is correct |
15 |
Correct |
180 ms |
51132 KB |
Output is correct |
16 |
Correct |
166 ms |
50884 KB |
Output is correct |
17 |
Correct |
165 ms |
50292 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
216 ms |
50644 KB |
Output is correct |
20 |
Correct |
199 ms |
50412 KB |
Output is correct |
21 |
Correct |
199 ms |
50356 KB |
Output is correct |
22 |
Correct |
225 ms |
50852 KB |
Output is correct |
23 |
Correct |
205 ms |
52148 KB |
Output is correct |
24 |
Correct |
231 ms |
52768 KB |
Output is correct |
25 |
Correct |
304 ms |
52748 KB |
Output is correct |
26 |
Correct |
282 ms |
53696 KB |
Output is correct |
27 |
Correct |
195 ms |
53116 KB |
Output is correct |
28 |
Correct |
221 ms |
50876 KB |
Output is correct |
29 |
Correct |
240 ms |
51208 KB |
Output is correct |
30 |
Correct |
266 ms |
52160 KB |
Output is correct |
31 |
Correct |
198 ms |
53064 KB |
Output is correct |
32 |
Correct |
226 ms |
51128 KB |
Output is correct |
33 |
Correct |
193 ms |
50632 KB |
Output is correct |
34 |
Correct |
180 ms |
50364 KB |
Output is correct |
35 |
Correct |
181 ms |
50624 KB |
Output is correct |
36 |
Correct |
220 ms |
52664 KB |
Output is correct |
37 |
Correct |
262 ms |
52668 KB |
Output is correct |
38 |
Correct |
208 ms |
51132 KB |
Output is correct |
39 |
Correct |
209 ms |
51916 KB |
Output is correct |
40 |
Correct |
185 ms |
50976 KB |
Output is correct |
41 |
Correct |
157 ms |
50620 KB |
Output is correct |
42 |
Correct |
170 ms |
50412 KB |
Output is correct |
43 |
Correct |
205 ms |
50364 KB |
Output is correct |
44 |
Correct |
207 ms |
50624 KB |
Output is correct |
45 |
Correct |
201 ms |
50624 KB |
Output is correct |
46 |
Correct |
191 ms |
51132 KB |
Output is correct |
47 |
Correct |
192 ms |
50356 KB |
Output is correct |
48 |
Correct |
202 ms |
50924 KB |
Output is correct |
49 |
Correct |
162 ms |
50876 KB |
Output is correct |
50 |
Correct |
168 ms |
50360 KB |
Output is correct |
51 |
Correct |
202 ms |
50552 KB |
Output is correct |
52 |
Correct |
189 ms |
50360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
89 ms |
50476 KB |
Output is correct |
3 |
Correct |
86 ms |
50304 KB |
Output is correct |
4 |
Correct |
113 ms |
50364 KB |
Output is correct |
5 |
Correct |
112 ms |
50764 KB |
Output is correct |
6 |
Correct |
124 ms |
50412 KB |
Output is correct |
7 |
Correct |
112 ms |
50344 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
173 ms |
50524 KB |
Output is correct |
11 |
Correct |
102 ms |
50364 KB |
Output is correct |
12 |
Correct |
88 ms |
50684 KB |
Output is correct |
13 |
Correct |
84 ms |
50316 KB |
Output is correct |
14 |
Correct |
169 ms |
50364 KB |
Output is correct |
15 |
Correct |
180 ms |
51132 KB |
Output is correct |
16 |
Correct |
166 ms |
50884 KB |
Output is correct |
17 |
Correct |
165 ms |
50292 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
216 ms |
50644 KB |
Output is correct |
20 |
Correct |
199 ms |
50412 KB |
Output is correct |
21 |
Correct |
199 ms |
50356 KB |
Output is correct |
22 |
Correct |
225 ms |
50852 KB |
Output is correct |
23 |
Correct |
205 ms |
52148 KB |
Output is correct |
24 |
Correct |
231 ms |
52768 KB |
Output is correct |
25 |
Correct |
304 ms |
52748 KB |
Output is correct |
26 |
Correct |
282 ms |
53696 KB |
Output is correct |
27 |
Correct |
195 ms |
53116 KB |
Output is correct |
28 |
Correct |
221 ms |
50876 KB |
Output is correct |
29 |
Correct |
240 ms |
51208 KB |
Output is correct |
30 |
Correct |
266 ms |
52160 KB |
Output is correct |
31 |
Correct |
198 ms |
53064 KB |
Output is correct |
32 |
Correct |
226 ms |
51128 KB |
Output is correct |
33 |
Correct |
193 ms |
50632 KB |
Output is correct |
34 |
Correct |
180 ms |
50364 KB |
Output is correct |
35 |
Correct |
181 ms |
50624 KB |
Output is correct |
36 |
Correct |
220 ms |
52664 KB |
Output is correct |
37 |
Correct |
262 ms |
52668 KB |
Output is correct |
38 |
Correct |
208 ms |
51132 KB |
Output is correct |
39 |
Correct |
209 ms |
51916 KB |
Output is correct |
40 |
Correct |
185 ms |
50976 KB |
Output is correct |
41 |
Correct |
157 ms |
50620 KB |
Output is correct |
42 |
Correct |
170 ms |
50412 KB |
Output is correct |
43 |
Correct |
205 ms |
50364 KB |
Output is correct |
44 |
Correct |
207 ms |
50624 KB |
Output is correct |
45 |
Correct |
201 ms |
50624 KB |
Output is correct |
46 |
Correct |
191 ms |
51132 KB |
Output is correct |
47 |
Correct |
192 ms |
50356 KB |
Output is correct |
48 |
Correct |
202 ms |
50924 KB |
Output is correct |
49 |
Correct |
162 ms |
50876 KB |
Output is correct |
50 |
Correct |
168 ms |
50360 KB |
Output is correct |
51 |
Correct |
202 ms |
50552 KB |
Output is correct |
52 |
Correct |
189 ms |
50360 KB |
Output is correct |
53 |
Correct |
3 ms |
12632 KB |
Output is correct |
54 |
Correct |
208 ms |
50612 KB |
Output is correct |
55 |
Correct |
216 ms |
50624 KB |
Output is correct |
56 |
Correct |
220 ms |
52620 KB |
Output is correct |
57 |
Correct |
261 ms |
52660 KB |
Output is correct |
58 |
Correct |
254 ms |
55732 KB |
Output is correct |
59 |
Correct |
277 ms |
55044 KB |
Output is correct |
60 |
Correct |
310 ms |
54792 KB |
Output is correct |
61 |
Correct |
304 ms |
55476 KB |
Output is correct |
62 |
Correct |
261 ms |
56848 KB |
Output is correct |
63 |
Correct |
259 ms |
50876 KB |
Output is correct |
64 |
Correct |
260 ms |
52404 KB |
Output is correct |
65 |
Correct |
301 ms |
54352 KB |
Output is correct |
66 |
Correct |
297 ms |
56308 KB |
Output is correct |
67 |
Correct |
309 ms |
53432 KB |
Output is correct |
68 |
Correct |
176 ms |
50392 KB |
Output is correct |
69 |
Correct |
212 ms |
53192 KB |
Output is correct |
70 |
Correct |
182 ms |
51132 KB |
Output is correct |
71 |
Correct |
206 ms |
52436 KB |
Output is correct |
72 |
Correct |
255 ms |
52964 KB |
Output is correct |
73 |
Correct |
216 ms |
51136 KB |
Output is correct |
74 |
Correct |
195 ms |
51896 KB |
Output is correct |
75 |
Correct |
211 ms |
51648 KB |
Output is correct |
76 |
Correct |
155 ms |
50492 KB |
Output is correct |
77 |
Correct |
155 ms |
50640 KB |
Output is correct |
78 |
Correct |
192 ms |
50712 KB |
Output is correct |
79 |
Correct |
254 ms |
52856 KB |
Output is correct |
80 |
Correct |
214 ms |
51036 KB |
Output is correct |
81 |
Correct |
225 ms |
54012 KB |
Output is correct |
82 |
Correct |
228 ms |
53292 KB |
Output is correct |
83 |
Correct |
251 ms |
53828 KB |
Output is correct |
84 |
Correct |
217 ms |
53684 KB |
Output is correct |
85 |
Correct |
162 ms |
50364 KB |
Output is correct |
86 |
Correct |
199 ms |
50576 KB |
Output is correct |
87 |
Correct |
183 ms |
50368 KB |
Output is correct |
88 |
Correct |
252 ms |
53684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12740 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
15 ms |
14940 KB |
Output is correct |
9 |
Correct |
15 ms |
14940 KB |
Output is correct |
10 |
Correct |
14 ms |
14684 KB |
Output is correct |
11 |
Correct |
15 ms |
14940 KB |
Output is correct |
12 |
Correct |
15 ms |
14936 KB |
Output is correct |
13 |
Correct |
15 ms |
14940 KB |
Output is correct |
14 |
Correct |
15 ms |
14936 KB |
Output is correct |
15 |
Correct |
15 ms |
14936 KB |
Output is correct |
16 |
Correct |
15 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
3 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12740 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
15 ms |
14940 KB |
Output is correct |
9 |
Correct |
15 ms |
14940 KB |
Output is correct |
10 |
Correct |
14 ms |
14684 KB |
Output is correct |
11 |
Correct |
15 ms |
14940 KB |
Output is correct |
12 |
Correct |
15 ms |
14936 KB |
Output is correct |
13 |
Correct |
15 ms |
14940 KB |
Output is correct |
14 |
Correct |
15 ms |
14936 KB |
Output is correct |
15 |
Correct |
15 ms |
14936 KB |
Output is correct |
16 |
Correct |
15 ms |
14940 KB |
Output is correct |
17 |
Execution timed out |
4009 ms |
13652 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
89 ms |
50476 KB |
Output is correct |
3 |
Correct |
86 ms |
50304 KB |
Output is correct |
4 |
Correct |
113 ms |
50364 KB |
Output is correct |
5 |
Correct |
112 ms |
50764 KB |
Output is correct |
6 |
Correct |
124 ms |
50412 KB |
Output is correct |
7 |
Correct |
112 ms |
50344 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
173 ms |
50524 KB |
Output is correct |
11 |
Correct |
102 ms |
50364 KB |
Output is correct |
12 |
Correct |
88 ms |
50684 KB |
Output is correct |
13 |
Correct |
84 ms |
50316 KB |
Output is correct |
14 |
Correct |
169 ms |
50364 KB |
Output is correct |
15 |
Correct |
180 ms |
51132 KB |
Output is correct |
16 |
Correct |
166 ms |
50884 KB |
Output is correct |
17 |
Correct |
165 ms |
50292 KB |
Output is correct |
18 |
Correct |
4 ms |
12636 KB |
Output is correct |
19 |
Correct |
216 ms |
50644 KB |
Output is correct |
20 |
Correct |
199 ms |
50412 KB |
Output is correct |
21 |
Correct |
199 ms |
50356 KB |
Output is correct |
22 |
Correct |
225 ms |
50852 KB |
Output is correct |
23 |
Correct |
205 ms |
52148 KB |
Output is correct |
24 |
Correct |
231 ms |
52768 KB |
Output is correct |
25 |
Correct |
304 ms |
52748 KB |
Output is correct |
26 |
Correct |
282 ms |
53696 KB |
Output is correct |
27 |
Correct |
195 ms |
53116 KB |
Output is correct |
28 |
Correct |
221 ms |
50876 KB |
Output is correct |
29 |
Correct |
240 ms |
51208 KB |
Output is correct |
30 |
Correct |
266 ms |
52160 KB |
Output is correct |
31 |
Correct |
198 ms |
53064 KB |
Output is correct |
32 |
Correct |
226 ms |
51128 KB |
Output is correct |
33 |
Correct |
193 ms |
50632 KB |
Output is correct |
34 |
Correct |
180 ms |
50364 KB |
Output is correct |
35 |
Correct |
181 ms |
50624 KB |
Output is correct |
36 |
Correct |
220 ms |
52664 KB |
Output is correct |
37 |
Correct |
262 ms |
52668 KB |
Output is correct |
38 |
Correct |
208 ms |
51132 KB |
Output is correct |
39 |
Correct |
209 ms |
51916 KB |
Output is correct |
40 |
Correct |
185 ms |
50976 KB |
Output is correct |
41 |
Correct |
157 ms |
50620 KB |
Output is correct |
42 |
Correct |
170 ms |
50412 KB |
Output is correct |
43 |
Correct |
205 ms |
50364 KB |
Output is correct |
44 |
Correct |
207 ms |
50624 KB |
Output is correct |
45 |
Correct |
201 ms |
50624 KB |
Output is correct |
46 |
Correct |
191 ms |
51132 KB |
Output is correct |
47 |
Correct |
192 ms |
50356 KB |
Output is correct |
48 |
Correct |
202 ms |
50924 KB |
Output is correct |
49 |
Correct |
162 ms |
50876 KB |
Output is correct |
50 |
Correct |
168 ms |
50360 KB |
Output is correct |
51 |
Correct |
202 ms |
50552 KB |
Output is correct |
52 |
Correct |
189 ms |
50360 KB |
Output is correct |
53 |
Correct |
3 ms |
12632 KB |
Output is correct |
54 |
Correct |
208 ms |
50612 KB |
Output is correct |
55 |
Correct |
216 ms |
50624 KB |
Output is correct |
56 |
Correct |
220 ms |
52620 KB |
Output is correct |
57 |
Correct |
261 ms |
52660 KB |
Output is correct |
58 |
Correct |
254 ms |
55732 KB |
Output is correct |
59 |
Correct |
277 ms |
55044 KB |
Output is correct |
60 |
Correct |
310 ms |
54792 KB |
Output is correct |
61 |
Correct |
304 ms |
55476 KB |
Output is correct |
62 |
Correct |
261 ms |
56848 KB |
Output is correct |
63 |
Correct |
259 ms |
50876 KB |
Output is correct |
64 |
Correct |
260 ms |
52404 KB |
Output is correct |
65 |
Correct |
301 ms |
54352 KB |
Output is correct |
66 |
Correct |
297 ms |
56308 KB |
Output is correct |
67 |
Correct |
309 ms |
53432 KB |
Output is correct |
68 |
Correct |
176 ms |
50392 KB |
Output is correct |
69 |
Correct |
212 ms |
53192 KB |
Output is correct |
70 |
Correct |
182 ms |
51132 KB |
Output is correct |
71 |
Correct |
206 ms |
52436 KB |
Output is correct |
72 |
Correct |
255 ms |
52964 KB |
Output is correct |
73 |
Correct |
216 ms |
51136 KB |
Output is correct |
74 |
Correct |
195 ms |
51896 KB |
Output is correct |
75 |
Correct |
211 ms |
51648 KB |
Output is correct |
76 |
Correct |
155 ms |
50492 KB |
Output is correct |
77 |
Correct |
155 ms |
50640 KB |
Output is correct |
78 |
Correct |
192 ms |
50712 KB |
Output is correct |
79 |
Correct |
254 ms |
52856 KB |
Output is correct |
80 |
Correct |
214 ms |
51036 KB |
Output is correct |
81 |
Correct |
225 ms |
54012 KB |
Output is correct |
82 |
Correct |
228 ms |
53292 KB |
Output is correct |
83 |
Correct |
251 ms |
53828 KB |
Output is correct |
84 |
Correct |
217 ms |
53684 KB |
Output is correct |
85 |
Correct |
162 ms |
50364 KB |
Output is correct |
86 |
Correct |
199 ms |
50576 KB |
Output is correct |
87 |
Correct |
183 ms |
50368 KB |
Output is correct |
88 |
Correct |
252 ms |
53684 KB |
Output is correct |
89 |
Correct |
3 ms |
12632 KB |
Output is correct |
90 |
Correct |
3 ms |
12636 KB |
Output is correct |
91 |
Correct |
3 ms |
12636 KB |
Output is correct |
92 |
Correct |
3 ms |
12636 KB |
Output is correct |
93 |
Correct |
3 ms |
12636 KB |
Output is correct |
94 |
Correct |
3 ms |
12740 KB |
Output is correct |
95 |
Correct |
3 ms |
12636 KB |
Output is correct |
96 |
Correct |
15 ms |
14940 KB |
Output is correct |
97 |
Correct |
15 ms |
14940 KB |
Output is correct |
98 |
Correct |
14 ms |
14684 KB |
Output is correct |
99 |
Correct |
15 ms |
14940 KB |
Output is correct |
100 |
Correct |
15 ms |
14936 KB |
Output is correct |
101 |
Correct |
15 ms |
14940 KB |
Output is correct |
102 |
Correct |
15 ms |
14936 KB |
Output is correct |
103 |
Correct |
15 ms |
14936 KB |
Output is correct |
104 |
Correct |
15 ms |
14940 KB |
Output is correct |
105 |
Execution timed out |
4009 ms |
13652 KB |
Time limit exceeded |
106 |
Halted |
0 ms |
0 KB |
- |