#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
vector<int> adj[200005];
pair<int, pair<int, int>> edge[400005];
int S[200005];
int N, M, Q;
struct UnionFind
{
vector<int> par;
UnionFind(int n){par.resize(n+1); for(int i=1;i<=n;i++) par[i] = i;}
int fin(int i){return i==par[i]?i:(par[i] = fin(par[i]));}
void uni(int u, int v){par[fin(u)] = fin(v);}
bool isuni(int u, int v){return fin(u) == fin(v);}
};
using pii = pair<int, int>;
vector<int> gph[200005];
pair<int, int> now[200005];
pair<pii, pii> dp[200005];
pii dp2[200005];
int par[200005];
const int inf = 1e18;
void dfs(int v, int p)
{
par[v] = p;
dp[v] = make_pair(make_pair(S[v], v), make_pair(inf, -1));
for (int i:gph[v]) {
if (i != p) {
dfs(i, v);
if (dp[v].first > dp[i].first) {
dp[v] = make_pair(dp[i].first, min(dp[v].first, dp[i].second));
}
else {
dp[v].second = min(dp[v].second, dp[i].first);
}
}
}
}
void dfs2(int v, int p)
{
if (v != 1) {
dp2[v] = dp2[p];
if (dp[v].first == dp[p].first) dp2[v] = min(dp2[v], dp[p].second);
else dp2[v] = min(dp2[v], dp[p].first);
}
else {
dp2[1] = make_pair(inf, -1);
}
for (int i:gph[v]) {
if (i != p) {
dfs2(i, v);
}
}
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N >> M >> Q;
for (int i = 1; i <= N; i++) cin >> S[i];
for (int i = 0; i < M; i++) {
int u, v; cin >> u >> v;
edge[i] = make_pair(S[u] + S[v], make_pair(u, v));
}
int ans = 0;
sort(edge, edge + M);
UnionFind uf(N);
int pv = 0;
for (int i = 0; i < M; i++) {
int u = edge[i].second.first, v = edge[i].second.second;
if (!uf.isuni(u, v)) {
uf.uni(u, v);
ans += S[u] + S[v];
gph[u].push_back(v);
gph[v].push_back(u);
now[pv++] = make_pair(u, v);
}
}
int df = *max_element(S+1, S+N+1) - accumulate(S+1, S+N+1, 0LL);
cout << ans + df << '\n';
return 0;
for (int tt = 0; tt < Q; tt++) {
if (tt < N - 1) {
dfs(1, 0);
dfs2(1, 0);
int cur = inf, id = -1;
for (int k = 0; k < N - 1; k++) {
int u = now[k].first, v = now[k].second;
if (u == par[v]) swap(u, v);
int dec = dp[u].first.first + dp2[u].first - S[u] - S[v];
if (cur > dec) {
cur = dec;
id = k;
}
}
int u = now[id].first, v = now[id].second;
if (u == par[v]) swap(u, v);
ans += cur;
gph[u].erase(remove(gph[u].begin(), gph[u].end(), v), gph[u].end());
gph[v].erase(remove(gph[v].begin(), gph[v].end(), u), gph[v].end());
int nu = dp[u].first.second, nv = dp2[u].second;
now[id] = make_pair(nu, nv);
gph[nu].push_back(nv);
gph[nv].push_back(nu);
}
cout << ans + df << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
59 ms |
26864 KB |
Output is correct |
4 |
Correct |
69 ms |
26896 KB |
Output is correct |
5 |
Correct |
70 ms |
26940 KB |
Output is correct |
6 |
Correct |
69 ms |
26828 KB |
Output is correct |
7 |
Correct |
72 ms |
26852 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
59 ms |
26864 KB |
Output is correct |
4 |
Correct |
69 ms |
26896 KB |
Output is correct |
5 |
Correct |
70 ms |
26940 KB |
Output is correct |
6 |
Correct |
69 ms |
26828 KB |
Output is correct |
7 |
Correct |
72 ms |
26852 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
94 ms |
26948 KB |
Output is correct |
11 |
Correct |
67 ms |
26828 KB |
Output is correct |
12 |
Correct |
66 ms |
26832 KB |
Output is correct |
13 |
Correct |
66 ms |
26832 KB |
Output is correct |
14 |
Correct |
113 ms |
26844 KB |
Output is correct |
15 |
Correct |
93 ms |
26936 KB |
Output is correct |
16 |
Correct |
103 ms |
26920 KB |
Output is correct |
17 |
Correct |
133 ms |
26916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
59 ms |
26864 KB |
Output is correct |
4 |
Correct |
69 ms |
26896 KB |
Output is correct |
5 |
Correct |
70 ms |
26940 KB |
Output is correct |
6 |
Correct |
69 ms |
26828 KB |
Output is correct |
7 |
Correct |
72 ms |
26852 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
94 ms |
26948 KB |
Output is correct |
11 |
Correct |
67 ms |
26828 KB |
Output is correct |
12 |
Correct |
66 ms |
26832 KB |
Output is correct |
13 |
Correct |
66 ms |
26832 KB |
Output is correct |
14 |
Correct |
113 ms |
26844 KB |
Output is correct |
15 |
Correct |
93 ms |
26936 KB |
Output is correct |
16 |
Correct |
103 ms |
26920 KB |
Output is correct |
17 |
Correct |
133 ms |
26916 KB |
Output is correct |
18 |
Correct |
5 ms |
9628 KB |
Output is correct |
19 |
Correct |
116 ms |
27764 KB |
Output is correct |
20 |
Correct |
125 ms |
27764 KB |
Output is correct |
21 |
Correct |
129 ms |
27884 KB |
Output is correct |
22 |
Correct |
123 ms |
28136 KB |
Output is correct |
23 |
Correct |
103 ms |
28648 KB |
Output is correct |
24 |
Correct |
99 ms |
28560 KB |
Output is correct |
25 |
Correct |
104 ms |
28512 KB |
Output is correct |
26 |
Correct |
93 ms |
28472 KB |
Output is correct |
27 |
Correct |
96 ms |
28500 KB |
Output is correct |
28 |
Correct |
111 ms |
27168 KB |
Output is correct |
29 |
Correct |
138 ms |
27632 KB |
Output is correct |
30 |
Correct |
118 ms |
28316 KB |
Output is correct |
31 |
Correct |
104 ms |
28400 KB |
Output is correct |
32 |
Correct |
112 ms |
28512 KB |
Output is correct |
33 |
Correct |
99 ms |
29260 KB |
Output is correct |
34 |
Correct |
113 ms |
28108 KB |
Output is correct |
35 |
Correct |
98 ms |
29104 KB |
Output is correct |
36 |
Correct |
91 ms |
29032 KB |
Output is correct |
37 |
Correct |
91 ms |
29032 KB |
Output is correct |
38 |
Correct |
98 ms |
28672 KB |
Output is correct |
39 |
Correct |
91 ms |
28244 KB |
Output is correct |
40 |
Correct |
93 ms |
28508 KB |
Output is correct |
41 |
Correct |
87 ms |
26856 KB |
Output is correct |
42 |
Correct |
85 ms |
26844 KB |
Output is correct |
43 |
Correct |
107 ms |
28128 KB |
Output is correct |
44 |
Correct |
115 ms |
28132 KB |
Output is correct |
45 |
Correct |
106 ms |
28076 KB |
Output is correct |
46 |
Correct |
119 ms |
28076 KB |
Output is correct |
47 |
Correct |
109 ms |
28052 KB |
Output is correct |
48 |
Correct |
121 ms |
28120 KB |
Output is correct |
49 |
Correct |
101 ms |
28112 KB |
Output is correct |
50 |
Correct |
94 ms |
28124 KB |
Output is correct |
51 |
Correct |
100 ms |
28128 KB |
Output is correct |
52 |
Correct |
91 ms |
28144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
59 ms |
26864 KB |
Output is correct |
4 |
Correct |
69 ms |
26896 KB |
Output is correct |
5 |
Correct |
70 ms |
26940 KB |
Output is correct |
6 |
Correct |
69 ms |
26828 KB |
Output is correct |
7 |
Correct |
72 ms |
26852 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
94 ms |
26948 KB |
Output is correct |
11 |
Correct |
67 ms |
26828 KB |
Output is correct |
12 |
Correct |
66 ms |
26832 KB |
Output is correct |
13 |
Correct |
66 ms |
26832 KB |
Output is correct |
14 |
Correct |
113 ms |
26844 KB |
Output is correct |
15 |
Correct |
93 ms |
26936 KB |
Output is correct |
16 |
Correct |
103 ms |
26920 KB |
Output is correct |
17 |
Correct |
133 ms |
26916 KB |
Output is correct |
18 |
Correct |
5 ms |
9628 KB |
Output is correct |
19 |
Correct |
116 ms |
27764 KB |
Output is correct |
20 |
Correct |
125 ms |
27764 KB |
Output is correct |
21 |
Correct |
129 ms |
27884 KB |
Output is correct |
22 |
Correct |
123 ms |
28136 KB |
Output is correct |
23 |
Correct |
103 ms |
28648 KB |
Output is correct |
24 |
Correct |
99 ms |
28560 KB |
Output is correct |
25 |
Correct |
104 ms |
28512 KB |
Output is correct |
26 |
Correct |
93 ms |
28472 KB |
Output is correct |
27 |
Correct |
96 ms |
28500 KB |
Output is correct |
28 |
Correct |
111 ms |
27168 KB |
Output is correct |
29 |
Correct |
138 ms |
27632 KB |
Output is correct |
30 |
Correct |
118 ms |
28316 KB |
Output is correct |
31 |
Correct |
104 ms |
28400 KB |
Output is correct |
32 |
Correct |
112 ms |
28512 KB |
Output is correct |
33 |
Correct |
99 ms |
29260 KB |
Output is correct |
34 |
Correct |
113 ms |
28108 KB |
Output is correct |
35 |
Correct |
98 ms |
29104 KB |
Output is correct |
36 |
Correct |
91 ms |
29032 KB |
Output is correct |
37 |
Correct |
91 ms |
29032 KB |
Output is correct |
38 |
Correct |
98 ms |
28672 KB |
Output is correct |
39 |
Correct |
91 ms |
28244 KB |
Output is correct |
40 |
Correct |
93 ms |
28508 KB |
Output is correct |
41 |
Correct |
87 ms |
26856 KB |
Output is correct |
42 |
Correct |
85 ms |
26844 KB |
Output is correct |
43 |
Correct |
107 ms |
28128 KB |
Output is correct |
44 |
Correct |
115 ms |
28132 KB |
Output is correct |
45 |
Correct |
106 ms |
28076 KB |
Output is correct |
46 |
Correct |
119 ms |
28076 KB |
Output is correct |
47 |
Correct |
109 ms |
28052 KB |
Output is correct |
48 |
Correct |
121 ms |
28120 KB |
Output is correct |
49 |
Correct |
101 ms |
28112 KB |
Output is correct |
50 |
Correct |
94 ms |
28124 KB |
Output is correct |
51 |
Correct |
100 ms |
28128 KB |
Output is correct |
52 |
Correct |
91 ms |
28144 KB |
Output is correct |
53 |
Correct |
4 ms |
9684 KB |
Output is correct |
54 |
Correct |
107 ms |
27996 KB |
Output is correct |
55 |
Correct |
109 ms |
28040 KB |
Output is correct |
56 |
Correct |
122 ms |
29288 KB |
Output is correct |
57 |
Correct |
155 ms |
32852 KB |
Output is correct |
58 |
Correct |
143 ms |
33352 KB |
Output is correct |
59 |
Correct |
154 ms |
33400 KB |
Output is correct |
60 |
Correct |
116 ms |
30676 KB |
Output is correct |
61 |
Correct |
155 ms |
33196 KB |
Output is correct |
62 |
Correct |
152 ms |
33084 KB |
Output is correct |
63 |
Correct |
113 ms |
27540 KB |
Output is correct |
64 |
Correct |
124 ms |
29692 KB |
Output is correct |
65 |
Correct |
152 ms |
32944 KB |
Output is correct |
66 |
Correct |
141 ms |
33196 KB |
Output is correct |
67 |
Correct |
168 ms |
33176 KB |
Output is correct |
68 |
Correct |
101 ms |
29284 KB |
Output is correct |
69 |
Correct |
143 ms |
30900 KB |
Output is correct |
70 |
Correct |
104 ms |
29684 KB |
Output is correct |
71 |
Correct |
92 ms |
28956 KB |
Output is correct |
72 |
Correct |
95 ms |
29008 KB |
Output is correct |
73 |
Correct |
102 ms |
28740 KB |
Output is correct |
74 |
Correct |
90 ms |
28272 KB |
Output is correct |
75 |
Correct |
94 ms |
28612 KB |
Output is correct |
76 |
Correct |
94 ms |
26988 KB |
Output is correct |
77 |
Correct |
104 ms |
26968 KB |
Output is correct |
78 |
Correct |
111 ms |
28428 KB |
Output is correct |
79 |
Correct |
178 ms |
32820 KB |
Output is correct |
80 |
Correct |
121 ms |
29364 KB |
Output is correct |
81 |
Correct |
157 ms |
31936 KB |
Output is correct |
82 |
Correct |
166 ms |
32852 KB |
Output is correct |
83 |
Correct |
168 ms |
32764 KB |
Output is correct |
84 |
Correct |
150 ms |
32820 KB |
Output is correct |
85 |
Correct |
107 ms |
28132 KB |
Output is correct |
86 |
Correct |
103 ms |
28092 KB |
Output is correct |
87 |
Correct |
91 ms |
28156 KB |
Output is correct |
88 |
Correct |
162 ms |
32816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
59 ms |
26864 KB |
Output is correct |
4 |
Correct |
69 ms |
26896 KB |
Output is correct |
5 |
Correct |
70 ms |
26940 KB |
Output is correct |
6 |
Correct |
69 ms |
26828 KB |
Output is correct |
7 |
Correct |
72 ms |
26852 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
94 ms |
26948 KB |
Output is correct |
11 |
Correct |
67 ms |
26828 KB |
Output is correct |
12 |
Correct |
66 ms |
26832 KB |
Output is correct |
13 |
Correct |
66 ms |
26832 KB |
Output is correct |
14 |
Correct |
113 ms |
26844 KB |
Output is correct |
15 |
Correct |
93 ms |
26936 KB |
Output is correct |
16 |
Correct |
103 ms |
26920 KB |
Output is correct |
17 |
Correct |
133 ms |
26916 KB |
Output is correct |
18 |
Correct |
5 ms |
9628 KB |
Output is correct |
19 |
Correct |
116 ms |
27764 KB |
Output is correct |
20 |
Correct |
125 ms |
27764 KB |
Output is correct |
21 |
Correct |
129 ms |
27884 KB |
Output is correct |
22 |
Correct |
123 ms |
28136 KB |
Output is correct |
23 |
Correct |
103 ms |
28648 KB |
Output is correct |
24 |
Correct |
99 ms |
28560 KB |
Output is correct |
25 |
Correct |
104 ms |
28512 KB |
Output is correct |
26 |
Correct |
93 ms |
28472 KB |
Output is correct |
27 |
Correct |
96 ms |
28500 KB |
Output is correct |
28 |
Correct |
111 ms |
27168 KB |
Output is correct |
29 |
Correct |
138 ms |
27632 KB |
Output is correct |
30 |
Correct |
118 ms |
28316 KB |
Output is correct |
31 |
Correct |
104 ms |
28400 KB |
Output is correct |
32 |
Correct |
112 ms |
28512 KB |
Output is correct |
33 |
Correct |
99 ms |
29260 KB |
Output is correct |
34 |
Correct |
113 ms |
28108 KB |
Output is correct |
35 |
Correct |
98 ms |
29104 KB |
Output is correct |
36 |
Correct |
91 ms |
29032 KB |
Output is correct |
37 |
Correct |
91 ms |
29032 KB |
Output is correct |
38 |
Correct |
98 ms |
28672 KB |
Output is correct |
39 |
Correct |
91 ms |
28244 KB |
Output is correct |
40 |
Correct |
93 ms |
28508 KB |
Output is correct |
41 |
Correct |
87 ms |
26856 KB |
Output is correct |
42 |
Correct |
85 ms |
26844 KB |
Output is correct |
43 |
Correct |
107 ms |
28128 KB |
Output is correct |
44 |
Correct |
115 ms |
28132 KB |
Output is correct |
45 |
Correct |
106 ms |
28076 KB |
Output is correct |
46 |
Correct |
119 ms |
28076 KB |
Output is correct |
47 |
Correct |
109 ms |
28052 KB |
Output is correct |
48 |
Correct |
121 ms |
28120 KB |
Output is correct |
49 |
Correct |
101 ms |
28112 KB |
Output is correct |
50 |
Correct |
94 ms |
28124 KB |
Output is correct |
51 |
Correct |
100 ms |
28128 KB |
Output is correct |
52 |
Correct |
91 ms |
28144 KB |
Output is correct |
53 |
Correct |
4 ms |
9684 KB |
Output is correct |
54 |
Correct |
107 ms |
27996 KB |
Output is correct |
55 |
Correct |
109 ms |
28040 KB |
Output is correct |
56 |
Correct |
122 ms |
29288 KB |
Output is correct |
57 |
Correct |
155 ms |
32852 KB |
Output is correct |
58 |
Correct |
143 ms |
33352 KB |
Output is correct |
59 |
Correct |
154 ms |
33400 KB |
Output is correct |
60 |
Correct |
116 ms |
30676 KB |
Output is correct |
61 |
Correct |
155 ms |
33196 KB |
Output is correct |
62 |
Correct |
152 ms |
33084 KB |
Output is correct |
63 |
Correct |
113 ms |
27540 KB |
Output is correct |
64 |
Correct |
124 ms |
29692 KB |
Output is correct |
65 |
Correct |
152 ms |
32944 KB |
Output is correct |
66 |
Correct |
141 ms |
33196 KB |
Output is correct |
67 |
Correct |
168 ms |
33176 KB |
Output is correct |
68 |
Correct |
101 ms |
29284 KB |
Output is correct |
69 |
Correct |
143 ms |
30900 KB |
Output is correct |
70 |
Correct |
104 ms |
29684 KB |
Output is correct |
71 |
Correct |
92 ms |
28956 KB |
Output is correct |
72 |
Correct |
95 ms |
29008 KB |
Output is correct |
73 |
Correct |
102 ms |
28740 KB |
Output is correct |
74 |
Correct |
90 ms |
28272 KB |
Output is correct |
75 |
Correct |
94 ms |
28612 KB |
Output is correct |
76 |
Correct |
94 ms |
26988 KB |
Output is correct |
77 |
Correct |
104 ms |
26968 KB |
Output is correct |
78 |
Correct |
111 ms |
28428 KB |
Output is correct |
79 |
Correct |
178 ms |
32820 KB |
Output is correct |
80 |
Correct |
121 ms |
29364 KB |
Output is correct |
81 |
Correct |
157 ms |
31936 KB |
Output is correct |
82 |
Correct |
166 ms |
32852 KB |
Output is correct |
83 |
Correct |
168 ms |
32764 KB |
Output is correct |
84 |
Correct |
150 ms |
32820 KB |
Output is correct |
85 |
Correct |
107 ms |
28132 KB |
Output is correct |
86 |
Correct |
103 ms |
28092 KB |
Output is correct |
87 |
Correct |
91 ms |
28156 KB |
Output is correct |
88 |
Correct |
162 ms |
32816 KB |
Output is correct |
89 |
Correct |
5 ms |
9684 KB |
Output is correct |
90 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
91 |
Halted |
0 ms |
0 KB |
- |