#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void input();
void solve();
int main(){
input();
solve();
}
struct Edge{
int a, b; ll w;
Edge(){}
Edge(int a, int b, ll w): a(a), b(b), w(w){}
bool operator<(const Edge &r)const{
return w<r.w;
}
};
int n, m, q;
Edge edge[100002];
ll queries[1000002];
void input(){
scanf("%d %d", &n, &m);
for(int i=1; i<=m; i++) scanf("%d %d %lld", &edge[i].a, &edge[i].b, &edge[i].w);
sort(edge+1, edge+m+1);
scanf("%d", &q);
for(int i=1; i<=q; i++) scanf("%lld", &queries[i]);
}
struct unionFind{
int par[100002];
void init(int n){
for(int i=1; i<=n; i++) par[i] = i;
}
int find(int x){
if(x == par[x]) return x;
return par[x] = find(par[x]);
}
void merge(int x, int y){
x = find(x), y = find(y);
par[y] = x;
}
} dsu;
bool used[100002], visited[100002];
vector<int> link[100002];
ll ans[100002];
int getMin(int x, int p, int y, int minV = INT_MAX){
if(x == y) return minV;
for(int nxt: link[x]){
int nxtV = edge[nxt].a ^ edge[nxt].b ^ x;
if(nxtV == p) continue;
int tmp = getMin(nxtV, x, y, min(minV, nxt));
if(tmp != -1) return tmp;
}
return -1;
}
void solve(){
/// get first mst
dsu.init(n);
for(int i=1; i<=m; i++){
if(dsu.find(edge[i].a) == dsu.find(edge[i].b)) continue;
dsu.merge(edge[i].a, edge[i].b);
link[edge[i].a].push_back(i);
link[edge[i].b].push_back(i);
used[i] = 1;
}
for(int tq=1; tq<=q; tq++){
ll W = queries[tq];
for(int i=1; i<=m; i++){
if(used[i]) continue;
int tmp = getMin(edge[i].a, -1, edge[i].b);
assert(tmp != -1);
if(abs(W - edge[tmp].w) > abs(W - edge[i].w)){
used[tmp] = 0, used[i] = 1;
link[edge[tmp].a].erase(find(link[edge[tmp].a].begin(), link[edge[tmp].a].end(), tmp));
link[edge[tmp].b].erase(find(link[edge[tmp].b].begin(), link[edge[tmp].b].end(), tmp));
link[edge[i].a].push_back(i);
link[edge[i].b].push_back(i);
}
}
ll sum = 0;
for(int i=1; i<=m; i++) if(used[i]) sum += abs(W - edge[i].w);
printf("%lld\n", sum);
}
}
Compilation message
reconstruction.cpp: In function 'void input()':
reconstruction.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
reconstruction.cpp:30:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | for(int i=1; i<=m; i++) scanf("%d %d %lld", &edge[i].a, &edge[i].b, &edge[i].w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
reconstruction.cpp:33:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | for(int i=1; i<=q; i++) scanf("%lld", &queries[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2105 ms |
8916 KB |
Output is correct |
21 |
Correct |
2273 ms |
8936 KB |
Output is correct |
22 |
Correct |
1791 ms |
8932 KB |
Output is correct |
23 |
Correct |
1598 ms |
8940 KB |
Output is correct |
24 |
Correct |
1739 ms |
8928 KB |
Output is correct |
25 |
Correct |
1957 ms |
8920 KB |
Output is correct |
26 |
Correct |
1992 ms |
8948 KB |
Output is correct |
27 |
Correct |
2099 ms |
9048 KB |
Output is correct |
28 |
Correct |
2062 ms |
8944 KB |
Output is correct |
29 |
Correct |
2067 ms |
8792 KB |
Output is correct |
30 |
Correct |
2187 ms |
8944 KB |
Output is correct |
31 |
Correct |
2228 ms |
8948 KB |
Output is correct |
32 |
Correct |
2258 ms |
8944 KB |
Output is correct |
33 |
Correct |
2205 ms |
8944 KB |
Output is correct |
34 |
Correct |
1983 ms |
9024 KB |
Output is correct |
35 |
Correct |
2017 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Execution timed out |
5063 ms |
24660 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Execution timed out |
5086 ms |
22460 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2105 ms |
8916 KB |
Output is correct |
21 |
Correct |
2273 ms |
8936 KB |
Output is correct |
22 |
Correct |
1791 ms |
8932 KB |
Output is correct |
23 |
Correct |
1598 ms |
8940 KB |
Output is correct |
24 |
Correct |
1739 ms |
8928 KB |
Output is correct |
25 |
Correct |
1957 ms |
8920 KB |
Output is correct |
26 |
Correct |
1992 ms |
8948 KB |
Output is correct |
27 |
Correct |
2099 ms |
9048 KB |
Output is correct |
28 |
Correct |
2062 ms |
8944 KB |
Output is correct |
29 |
Correct |
2067 ms |
8792 KB |
Output is correct |
30 |
Correct |
2187 ms |
8944 KB |
Output is correct |
31 |
Correct |
2228 ms |
8948 KB |
Output is correct |
32 |
Correct |
2258 ms |
8944 KB |
Output is correct |
33 |
Correct |
2205 ms |
8944 KB |
Output is correct |
34 |
Correct |
1983 ms |
9024 KB |
Output is correct |
35 |
Correct |
2017 ms |
8952 KB |
Output is correct |
36 |
Execution timed out |
5067 ms |
9008 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2105 ms |
8916 KB |
Output is correct |
21 |
Correct |
2273 ms |
8936 KB |
Output is correct |
22 |
Correct |
1791 ms |
8932 KB |
Output is correct |
23 |
Correct |
1598 ms |
8940 KB |
Output is correct |
24 |
Correct |
1739 ms |
8928 KB |
Output is correct |
25 |
Correct |
1957 ms |
8920 KB |
Output is correct |
26 |
Correct |
1992 ms |
8948 KB |
Output is correct |
27 |
Correct |
2099 ms |
9048 KB |
Output is correct |
28 |
Correct |
2062 ms |
8944 KB |
Output is correct |
29 |
Correct |
2067 ms |
8792 KB |
Output is correct |
30 |
Correct |
2187 ms |
8944 KB |
Output is correct |
31 |
Correct |
2228 ms |
8948 KB |
Output is correct |
32 |
Correct |
2258 ms |
8944 KB |
Output is correct |
33 |
Correct |
2205 ms |
8944 KB |
Output is correct |
34 |
Correct |
1983 ms |
9024 KB |
Output is correct |
35 |
Correct |
2017 ms |
8952 KB |
Output is correct |
36 |
Correct |
2 ms |
6744 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Execution timed out |
5063 ms |
24660 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |