#include<bits/stdc++.h>
using namespace std;
int n,m,q,w;
pair< int, pair<int,int> > ed[100005];
vector<int> used[100005], used2[100005], v[100005];
int dsu[505];
int find(int x) {
if(dsu[x]==x) return x;
else return dsu[x] = find(dsu[x]);
}
void merge(int x, int y) {
dsu[find(x)] = find(y);
}
bool cmp(int x, int y) {
return abs(w-ed[x].first) < abs(w-ed[y].first);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=0; i<m; i++) {
cin >> ed[i].second.first >> ed[i].second.second >> ed[i].first;
}
sort(ed, ed+m);
for(int i=1; i<=m; i++) {
for(int j=1; j<=n; j++) dsu[j] = j;
used[i].push_back(i-1);
merge(ed[i-1].second.first, ed[i-1].second.second);
for(int j:used[i-1]) {
if(find(ed[j].second.first) != find(ed[j].second.second)) {
merge(ed[j].second.first, ed[j].second.second);
used[i].push_back(j);
}
}
}
for(int i=m-1; i>=0; i--) {
for(int j=1; j<=n; j++) dsu[j] = j;
used2[i].push_back(i);
merge(ed[i].second.first, ed[i].second.second);
for(int j:used2[i+1]) {
if(find(ed[j].second.first) != find(ed[j].second.second)) {
merge(ed[j].second.first, ed[j].second.second);
used2[i].push_back(j);
}
}
}
for(int i=0; i<=m; i++) {
for(int j:used[i]) v[i].push_back(j);
for(int j:used2[i]) v[i].push_back(j);
}
cin >> q;
while(q--) {
cin >> w;
int pos = upper_bound(ed, ed+m, make_pair(w, make_pair((int)1e9, (int)1e9))) - ed;
sort(v[pos].begin(), v[pos].end(), cmp);
for(int i=1; i<=n; i++) dsu[i] = i;
long long ans = 0;
for(int i:v[pos]) {
if(find(ed[i].second.first) != find(ed[i].second.second)) {
ans += abs(w-ed[i].first);
merge(ed[i].second.first, ed[i].second.second);
}
}
cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7348 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7280 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
6 ms |
7280 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7348 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7280 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
6 ms |
7280 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7252 KB |
Output is correct |
20 |
Correct |
3784 ms |
814376 KB |
Output is correct |
21 |
Correct |
2946 ms |
813964 KB |
Output is correct |
22 |
Correct |
3089 ms |
813936 KB |
Output is correct |
23 |
Correct |
3104 ms |
813964 KB |
Output is correct |
24 |
Correct |
3442 ms |
813908 KB |
Output is correct |
25 |
Correct |
3919 ms |
814044 KB |
Output is correct |
26 |
Correct |
3785 ms |
815720 KB |
Output is correct |
27 |
Correct |
3775 ms |
815524 KB |
Output is correct |
28 |
Correct |
3723 ms |
815712 KB |
Output is correct |
29 |
Correct |
3492 ms |
812660 KB |
Output is correct |
30 |
Correct |
3774 ms |
815684 KB |
Output is correct |
31 |
Correct |
3755 ms |
816024 KB |
Output is correct |
32 |
Correct |
3728 ms |
815840 KB |
Output is correct |
33 |
Correct |
3728 ms |
814128 KB |
Output is correct |
34 |
Correct |
2796 ms |
785928 KB |
Output is correct |
35 |
Correct |
3611 ms |
815704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Execution timed out |
5085 ms |
814804 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7348 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7280 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
6 ms |
7280 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
3 ms |
7252 KB |
Output is correct |
20 |
Execution timed out |
5057 ms |
17324 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7348 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7280 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
6 ms |
7280 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7252 KB |
Output is correct |
20 |
Correct |
3784 ms |
814376 KB |
Output is correct |
21 |
Correct |
2946 ms |
813964 KB |
Output is correct |
22 |
Correct |
3089 ms |
813936 KB |
Output is correct |
23 |
Correct |
3104 ms |
813964 KB |
Output is correct |
24 |
Correct |
3442 ms |
813908 KB |
Output is correct |
25 |
Correct |
3919 ms |
814044 KB |
Output is correct |
26 |
Correct |
3785 ms |
815720 KB |
Output is correct |
27 |
Correct |
3775 ms |
815524 KB |
Output is correct |
28 |
Correct |
3723 ms |
815712 KB |
Output is correct |
29 |
Correct |
3492 ms |
812660 KB |
Output is correct |
30 |
Correct |
3774 ms |
815684 KB |
Output is correct |
31 |
Correct |
3755 ms |
816024 KB |
Output is correct |
32 |
Correct |
3728 ms |
815840 KB |
Output is correct |
33 |
Correct |
3728 ms |
814128 KB |
Output is correct |
34 |
Correct |
2796 ms |
785928 KB |
Output is correct |
35 |
Correct |
3611 ms |
815704 KB |
Output is correct |
36 |
Execution timed out |
5085 ms |
814512 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
6 ms |
7348 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7280 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
6 ms |
7280 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7252 KB |
Output is correct |
20 |
Correct |
3784 ms |
814376 KB |
Output is correct |
21 |
Correct |
2946 ms |
813964 KB |
Output is correct |
22 |
Correct |
3089 ms |
813936 KB |
Output is correct |
23 |
Correct |
3104 ms |
813964 KB |
Output is correct |
24 |
Correct |
3442 ms |
813908 KB |
Output is correct |
25 |
Correct |
3919 ms |
814044 KB |
Output is correct |
26 |
Correct |
3785 ms |
815720 KB |
Output is correct |
27 |
Correct |
3775 ms |
815524 KB |
Output is correct |
28 |
Correct |
3723 ms |
815712 KB |
Output is correct |
29 |
Correct |
3492 ms |
812660 KB |
Output is correct |
30 |
Correct |
3774 ms |
815684 KB |
Output is correct |
31 |
Correct |
3755 ms |
816024 KB |
Output is correct |
32 |
Correct |
3728 ms |
815840 KB |
Output is correct |
33 |
Correct |
3728 ms |
814128 KB |
Output is correct |
34 |
Correct |
2796 ms |
785928 KB |
Output is correct |
35 |
Correct |
3611 ms |
815704 KB |
Output is correct |
36 |
Correct |
3 ms |
7252 KB |
Output is correct |
37 |
Correct |
4 ms |
7252 KB |
Output is correct |
38 |
Correct |
4 ms |
7252 KB |
Output is correct |
39 |
Execution timed out |
5085 ms |
814804 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |