#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.first == dp[p].first.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 |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
60 ms |
26840 KB |
Output is correct |
4 |
Correct |
68 ms |
26836 KB |
Output is correct |
5 |
Correct |
69 ms |
26896 KB |
Output is correct |
6 |
Correct |
72 ms |
26856 KB |
Output is correct |
7 |
Correct |
70 ms |
26912 KB |
Output is correct |
8 |
Correct |
7 ms |
9712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
60 ms |
26840 KB |
Output is correct |
4 |
Correct |
68 ms |
26836 KB |
Output is correct |
5 |
Correct |
69 ms |
26896 KB |
Output is correct |
6 |
Correct |
72 ms |
26856 KB |
Output is correct |
7 |
Correct |
70 ms |
26912 KB |
Output is correct |
8 |
Correct |
7 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
97 ms |
26884 KB |
Output is correct |
11 |
Correct |
67 ms |
26876 KB |
Output is correct |
12 |
Correct |
75 ms |
26880 KB |
Output is correct |
13 |
Correct |
63 ms |
26832 KB |
Output is correct |
14 |
Correct |
93 ms |
26888 KB |
Output is correct |
15 |
Correct |
97 ms |
26852 KB |
Output is correct |
16 |
Correct |
103 ms |
26912 KB |
Output is correct |
17 |
Correct |
93 ms |
26824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
60 ms |
26840 KB |
Output is correct |
4 |
Correct |
68 ms |
26836 KB |
Output is correct |
5 |
Correct |
69 ms |
26896 KB |
Output is correct |
6 |
Correct |
72 ms |
26856 KB |
Output is correct |
7 |
Correct |
70 ms |
26912 KB |
Output is correct |
8 |
Correct |
7 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
97 ms |
26884 KB |
Output is correct |
11 |
Correct |
67 ms |
26876 KB |
Output is correct |
12 |
Correct |
75 ms |
26880 KB |
Output is correct |
13 |
Correct |
63 ms |
26832 KB |
Output is correct |
14 |
Correct |
93 ms |
26888 KB |
Output is correct |
15 |
Correct |
97 ms |
26852 KB |
Output is correct |
16 |
Correct |
103 ms |
26912 KB |
Output is correct |
17 |
Correct |
93 ms |
26824 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
108 ms |
27744 KB |
Output is correct |
20 |
Correct |
104 ms |
27764 KB |
Output is correct |
21 |
Correct |
111 ms |
27812 KB |
Output is correct |
22 |
Correct |
105 ms |
28164 KB |
Output is correct |
23 |
Correct |
92 ms |
28692 KB |
Output is correct |
24 |
Correct |
99 ms |
28700 KB |
Output is correct |
25 |
Correct |
110 ms |
28492 KB |
Output is correct |
26 |
Correct |
94 ms |
28512 KB |
Output is correct |
27 |
Correct |
88 ms |
28488 KB |
Output is correct |
28 |
Correct |
137 ms |
27232 KB |
Output is correct |
29 |
Correct |
157 ms |
27728 KB |
Output is correct |
30 |
Correct |
91 ms |
28244 KB |
Output is correct |
31 |
Correct |
110 ms |
28404 KB |
Output is correct |
32 |
Correct |
111 ms |
28424 KB |
Output is correct |
33 |
Correct |
117 ms |
29212 KB |
Output is correct |
34 |
Correct |
105 ms |
27980 KB |
Output is correct |
35 |
Correct |
110 ms |
29176 KB |
Output is correct |
36 |
Correct |
104 ms |
28920 KB |
Output is correct |
37 |
Correct |
91 ms |
28940 KB |
Output is correct |
38 |
Correct |
103 ms |
28764 KB |
Output is correct |
39 |
Correct |
113 ms |
28272 KB |
Output is correct |
40 |
Correct |
106 ms |
28572 KB |
Output is correct |
41 |
Correct |
98 ms |
26848 KB |
Output is correct |
42 |
Correct |
93 ms |
26944 KB |
Output is correct |
43 |
Correct |
116 ms |
28152 KB |
Output is correct |
44 |
Correct |
144 ms |
28212 KB |
Output is correct |
45 |
Correct |
109 ms |
28136 KB |
Output is correct |
46 |
Correct |
109 ms |
28104 KB |
Output is correct |
47 |
Correct |
104 ms |
28116 KB |
Output is correct |
48 |
Correct |
128 ms |
28048 KB |
Output is correct |
49 |
Correct |
92 ms |
28040 KB |
Output is correct |
50 |
Correct |
97 ms |
28112 KB |
Output is correct |
51 |
Correct |
99 ms |
28132 KB |
Output is correct |
52 |
Correct |
94 ms |
28120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
60 ms |
26840 KB |
Output is correct |
4 |
Correct |
68 ms |
26836 KB |
Output is correct |
5 |
Correct |
69 ms |
26896 KB |
Output is correct |
6 |
Correct |
72 ms |
26856 KB |
Output is correct |
7 |
Correct |
70 ms |
26912 KB |
Output is correct |
8 |
Correct |
7 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
97 ms |
26884 KB |
Output is correct |
11 |
Correct |
67 ms |
26876 KB |
Output is correct |
12 |
Correct |
75 ms |
26880 KB |
Output is correct |
13 |
Correct |
63 ms |
26832 KB |
Output is correct |
14 |
Correct |
93 ms |
26888 KB |
Output is correct |
15 |
Correct |
97 ms |
26852 KB |
Output is correct |
16 |
Correct |
103 ms |
26912 KB |
Output is correct |
17 |
Correct |
93 ms |
26824 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
108 ms |
27744 KB |
Output is correct |
20 |
Correct |
104 ms |
27764 KB |
Output is correct |
21 |
Correct |
111 ms |
27812 KB |
Output is correct |
22 |
Correct |
105 ms |
28164 KB |
Output is correct |
23 |
Correct |
92 ms |
28692 KB |
Output is correct |
24 |
Correct |
99 ms |
28700 KB |
Output is correct |
25 |
Correct |
110 ms |
28492 KB |
Output is correct |
26 |
Correct |
94 ms |
28512 KB |
Output is correct |
27 |
Correct |
88 ms |
28488 KB |
Output is correct |
28 |
Correct |
137 ms |
27232 KB |
Output is correct |
29 |
Correct |
157 ms |
27728 KB |
Output is correct |
30 |
Correct |
91 ms |
28244 KB |
Output is correct |
31 |
Correct |
110 ms |
28404 KB |
Output is correct |
32 |
Correct |
111 ms |
28424 KB |
Output is correct |
33 |
Correct |
117 ms |
29212 KB |
Output is correct |
34 |
Correct |
105 ms |
27980 KB |
Output is correct |
35 |
Correct |
110 ms |
29176 KB |
Output is correct |
36 |
Correct |
104 ms |
28920 KB |
Output is correct |
37 |
Correct |
91 ms |
28940 KB |
Output is correct |
38 |
Correct |
103 ms |
28764 KB |
Output is correct |
39 |
Correct |
113 ms |
28272 KB |
Output is correct |
40 |
Correct |
106 ms |
28572 KB |
Output is correct |
41 |
Correct |
98 ms |
26848 KB |
Output is correct |
42 |
Correct |
93 ms |
26944 KB |
Output is correct |
43 |
Correct |
116 ms |
28152 KB |
Output is correct |
44 |
Correct |
144 ms |
28212 KB |
Output is correct |
45 |
Correct |
109 ms |
28136 KB |
Output is correct |
46 |
Correct |
109 ms |
28104 KB |
Output is correct |
47 |
Correct |
104 ms |
28116 KB |
Output is correct |
48 |
Correct |
128 ms |
28048 KB |
Output is correct |
49 |
Correct |
92 ms |
28040 KB |
Output is correct |
50 |
Correct |
97 ms |
28112 KB |
Output is correct |
51 |
Correct |
99 ms |
28132 KB |
Output is correct |
52 |
Correct |
94 ms |
28120 KB |
Output is correct |
53 |
Correct |
5 ms |
9684 KB |
Output is correct |
54 |
Correct |
119 ms |
27916 KB |
Output is correct |
55 |
Correct |
125 ms |
28064 KB |
Output is correct |
56 |
Correct |
132 ms |
29272 KB |
Output is correct |
57 |
Correct |
168 ms |
32744 KB |
Output is correct |
58 |
Correct |
144 ms |
33408 KB |
Output is correct |
59 |
Correct |
170 ms |
33476 KB |
Output is correct |
60 |
Correct |
114 ms |
30716 KB |
Output is correct |
61 |
Correct |
177 ms |
33164 KB |
Output is correct |
62 |
Correct |
141 ms |
33140 KB |
Output is correct |
63 |
Correct |
106 ms |
27500 KB |
Output is correct |
64 |
Correct |
130 ms |
29764 KB |
Output is correct |
65 |
Correct |
146 ms |
32932 KB |
Output is correct |
66 |
Correct |
146 ms |
33080 KB |
Output is correct |
67 |
Correct |
164 ms |
33224 KB |
Output is correct |
68 |
Correct |
105 ms |
29284 KB |
Output is correct |
69 |
Correct |
151 ms |
30964 KB |
Output is correct |
70 |
Correct |
106 ms |
29720 KB |
Output is correct |
71 |
Correct |
93 ms |
29008 KB |
Output is correct |
72 |
Correct |
89 ms |
29024 KB |
Output is correct |
73 |
Correct |
101 ms |
28756 KB |
Output is correct |
74 |
Correct |
86 ms |
28204 KB |
Output is correct |
75 |
Correct |
93 ms |
28512 KB |
Output is correct |
76 |
Correct |
89 ms |
26912 KB |
Output is correct |
77 |
Correct |
107 ms |
26828 KB |
Output is correct |
78 |
Correct |
115 ms |
28388 KB |
Output is correct |
79 |
Correct |
187 ms |
32748 KB |
Output is correct |
80 |
Correct |
118 ms |
29400 KB |
Output is correct |
81 |
Correct |
156 ms |
31884 KB |
Output is correct |
82 |
Correct |
164 ms |
32844 KB |
Output is correct |
83 |
Correct |
210 ms |
32788 KB |
Output is correct |
84 |
Correct |
150 ms |
32808 KB |
Output is correct |
85 |
Correct |
103 ms |
28052 KB |
Output is correct |
86 |
Correct |
103 ms |
28044 KB |
Output is correct |
87 |
Correct |
100 ms |
28108 KB |
Output is correct |
88 |
Correct |
165 ms |
32804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
4 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 |
Incorrect |
4 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 |
Correct |
58 ms |
26828 KB |
Output is correct |
3 |
Correct |
60 ms |
26840 KB |
Output is correct |
4 |
Correct |
68 ms |
26836 KB |
Output is correct |
5 |
Correct |
69 ms |
26896 KB |
Output is correct |
6 |
Correct |
72 ms |
26856 KB |
Output is correct |
7 |
Correct |
70 ms |
26912 KB |
Output is correct |
8 |
Correct |
7 ms |
9712 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
97 ms |
26884 KB |
Output is correct |
11 |
Correct |
67 ms |
26876 KB |
Output is correct |
12 |
Correct |
75 ms |
26880 KB |
Output is correct |
13 |
Correct |
63 ms |
26832 KB |
Output is correct |
14 |
Correct |
93 ms |
26888 KB |
Output is correct |
15 |
Correct |
97 ms |
26852 KB |
Output is correct |
16 |
Correct |
103 ms |
26912 KB |
Output is correct |
17 |
Correct |
93 ms |
26824 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
108 ms |
27744 KB |
Output is correct |
20 |
Correct |
104 ms |
27764 KB |
Output is correct |
21 |
Correct |
111 ms |
27812 KB |
Output is correct |
22 |
Correct |
105 ms |
28164 KB |
Output is correct |
23 |
Correct |
92 ms |
28692 KB |
Output is correct |
24 |
Correct |
99 ms |
28700 KB |
Output is correct |
25 |
Correct |
110 ms |
28492 KB |
Output is correct |
26 |
Correct |
94 ms |
28512 KB |
Output is correct |
27 |
Correct |
88 ms |
28488 KB |
Output is correct |
28 |
Correct |
137 ms |
27232 KB |
Output is correct |
29 |
Correct |
157 ms |
27728 KB |
Output is correct |
30 |
Correct |
91 ms |
28244 KB |
Output is correct |
31 |
Correct |
110 ms |
28404 KB |
Output is correct |
32 |
Correct |
111 ms |
28424 KB |
Output is correct |
33 |
Correct |
117 ms |
29212 KB |
Output is correct |
34 |
Correct |
105 ms |
27980 KB |
Output is correct |
35 |
Correct |
110 ms |
29176 KB |
Output is correct |
36 |
Correct |
104 ms |
28920 KB |
Output is correct |
37 |
Correct |
91 ms |
28940 KB |
Output is correct |
38 |
Correct |
103 ms |
28764 KB |
Output is correct |
39 |
Correct |
113 ms |
28272 KB |
Output is correct |
40 |
Correct |
106 ms |
28572 KB |
Output is correct |
41 |
Correct |
98 ms |
26848 KB |
Output is correct |
42 |
Correct |
93 ms |
26944 KB |
Output is correct |
43 |
Correct |
116 ms |
28152 KB |
Output is correct |
44 |
Correct |
144 ms |
28212 KB |
Output is correct |
45 |
Correct |
109 ms |
28136 KB |
Output is correct |
46 |
Correct |
109 ms |
28104 KB |
Output is correct |
47 |
Correct |
104 ms |
28116 KB |
Output is correct |
48 |
Correct |
128 ms |
28048 KB |
Output is correct |
49 |
Correct |
92 ms |
28040 KB |
Output is correct |
50 |
Correct |
97 ms |
28112 KB |
Output is correct |
51 |
Correct |
99 ms |
28132 KB |
Output is correct |
52 |
Correct |
94 ms |
28120 KB |
Output is correct |
53 |
Correct |
5 ms |
9684 KB |
Output is correct |
54 |
Correct |
119 ms |
27916 KB |
Output is correct |
55 |
Correct |
125 ms |
28064 KB |
Output is correct |
56 |
Correct |
132 ms |
29272 KB |
Output is correct |
57 |
Correct |
168 ms |
32744 KB |
Output is correct |
58 |
Correct |
144 ms |
33408 KB |
Output is correct |
59 |
Correct |
170 ms |
33476 KB |
Output is correct |
60 |
Correct |
114 ms |
30716 KB |
Output is correct |
61 |
Correct |
177 ms |
33164 KB |
Output is correct |
62 |
Correct |
141 ms |
33140 KB |
Output is correct |
63 |
Correct |
106 ms |
27500 KB |
Output is correct |
64 |
Correct |
130 ms |
29764 KB |
Output is correct |
65 |
Correct |
146 ms |
32932 KB |
Output is correct |
66 |
Correct |
146 ms |
33080 KB |
Output is correct |
67 |
Correct |
164 ms |
33224 KB |
Output is correct |
68 |
Correct |
105 ms |
29284 KB |
Output is correct |
69 |
Correct |
151 ms |
30964 KB |
Output is correct |
70 |
Correct |
106 ms |
29720 KB |
Output is correct |
71 |
Correct |
93 ms |
29008 KB |
Output is correct |
72 |
Correct |
89 ms |
29024 KB |
Output is correct |
73 |
Correct |
101 ms |
28756 KB |
Output is correct |
74 |
Correct |
86 ms |
28204 KB |
Output is correct |
75 |
Correct |
93 ms |
28512 KB |
Output is correct |
76 |
Correct |
89 ms |
26912 KB |
Output is correct |
77 |
Correct |
107 ms |
26828 KB |
Output is correct |
78 |
Correct |
115 ms |
28388 KB |
Output is correct |
79 |
Correct |
187 ms |
32748 KB |
Output is correct |
80 |
Correct |
118 ms |
29400 KB |
Output is correct |
81 |
Correct |
156 ms |
31884 KB |
Output is correct |
82 |
Correct |
164 ms |
32844 KB |
Output is correct |
83 |
Correct |
210 ms |
32788 KB |
Output is correct |
84 |
Correct |
150 ms |
32808 KB |
Output is correct |
85 |
Correct |
103 ms |
28052 KB |
Output is correct |
86 |
Correct |
103 ms |
28044 KB |
Output is correct |
87 |
Correct |
100 ms |
28108 KB |
Output is correct |
88 |
Correct |
165 ms |
32804 KB |
Output is correct |
89 |
Correct |
6 ms |
9684 KB |
Output is correct |
90 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
91 |
Halted |
0 ms |
0 KB |
- |