#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int n, m;
vector<pair<int, pair<int, int>>> E;
int cl[maxn];
set<pair<int, int>> tree[maxn];
pair<int, int> X[maxn];
long long fans[maxn];
int pos = -1;
bool dfs(int x, int p, int t, int md){
if(x == t)return true;
for(auto v: tree[x]){
if(v.first == p)continue;
if(dfs(v.first, x, t, md)){
if(pos == -1 || (!md && v.second < pos) || (md && v.second > pos))pos = v.second;
return 1;
}
}
return false;
}
int up[maxn];
int Find(int x){
return up[x] ? up[x] = Find(up[x]) : x;
}
bool check(int a, int b){
return Find(a) != Find(b);
}
void Union(int a, int b){
a = Find(a);
b = Find(b);
up[a] = b;
}
int main() {
cin >> n >> m;
E.resize(m);
for(auto &v: E)cin >> v.second.first >> v.second.second >> v.first;
sort(E.begin(), E.end());
set<int, greater<int>> lside;
set<int> rside;
for(int i = 0; i < m; i++){
pos = -1;
dfs(E[i].second.first, 0, E[i].second.second, 0);
cl[i] = pos;
if(pos != -1){
lside.erase(pos);
tree[E[pos].second.first].erase({E[pos].second.second, pos});
tree[E[pos].second.second].erase({E[pos].second.first, pos});
}
lside.insert(i);
tree[E[i].second.first].insert({E[i].second.second, i});
tree[E[i].second.second].insert({E[i].second.first, i});
}
for(int i = 1; i <= n; i++)tree[i].clear();
int q;
cin >> q;
for(int i = 0; i < q; i++){
cin >> X[i].first;
X[i].second = i;
}
sort(X, X+q);
int ptr = m-1;
for(int i = q-1; i >= 0; i--){
while(ptr >= 0 && E[ptr].first >= X[i].first){
pos = -1;
dfs(E[ptr].second.first, 0, E[ptr].second.second, 1);
if(pos != -1){
rside.erase(pos);
tree[E[pos].second.first].erase({E[pos].second.second, pos});
tree[E[pos].second.second].erase({E[pos].second.first, pos});
}
rside.insert(ptr);
tree[E[ptr].second.first].insert({E[ptr].second.second, ptr});
tree[E[ptr].second.second].insert({E[ptr].second.first, ptr});
lside.erase(ptr);
if(cl[ptr] != -1)lside.insert(cl[ptr]);
ptr--;
}
auto itl = lside.begin(), itr = rside.begin();
long long ans = 0;
for(int j = 1; j <= n; j++)up[j] = 0;
for(int j = 0; j < n-1; j++){
while(itl != lside.end() && !check(E[*itl].second.first, E[*itl].second.second))itl++;
while(itr != rside.end() && !check(E[*itr].second.first, E[*itr].second.second))itr++;
if(itr == rside.end() || (itl != lside.end() && X[i].first - E[*itl].first < E[*itr].first - X[i].first)){
Union(E[*itl].second.first, E[*itl].second.second);
ans += X[i].first - E[*itl].first;
itl++;
}
else {
Union(E[*itr].second.first, E[*itr].second.second);
ans += E[*itr].first - X[i].first;
itr++;
}
}
fans[X[i].second] = ans;
}
for(int i = 0; i < q; i++)cout << fans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6936 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6836 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6936 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6836 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
838 ms |
10124 KB |
Output is correct |
21 |
Correct |
438 ms |
10064 KB |
Output is correct |
22 |
Correct |
587 ms |
10400 KB |
Output is correct |
23 |
Correct |
650 ms |
10264 KB |
Output is correct |
24 |
Correct |
713 ms |
10432 KB |
Output is correct |
25 |
Correct |
810 ms |
10068 KB |
Output is correct |
26 |
Correct |
843 ms |
10256 KB |
Output is correct |
27 |
Correct |
900 ms |
10068 KB |
Output is correct |
28 |
Correct |
880 ms |
10384 KB |
Output is correct |
29 |
Correct |
702 ms |
10292 KB |
Output is correct |
30 |
Correct |
866 ms |
10268 KB |
Output is correct |
31 |
Correct |
819 ms |
10380 KB |
Output is correct |
32 |
Correct |
652 ms |
10164 KB |
Output is correct |
33 |
Correct |
857 ms |
10560 KB |
Output is correct |
34 |
Correct |
464 ms |
10576 KB |
Output is correct |
35 |
Correct |
645 ms |
10516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Runtime error |
710 ms |
26004 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6936 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6836 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
6744 KB |
Output is correct |
20 |
Runtime error |
217 ms |
21840 KB |
Execution killed with signal 11 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6936 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6836 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
838 ms |
10124 KB |
Output is correct |
21 |
Correct |
438 ms |
10064 KB |
Output is correct |
22 |
Correct |
587 ms |
10400 KB |
Output is correct |
23 |
Correct |
650 ms |
10264 KB |
Output is correct |
24 |
Correct |
713 ms |
10432 KB |
Output is correct |
25 |
Correct |
810 ms |
10068 KB |
Output is correct |
26 |
Correct |
843 ms |
10256 KB |
Output is correct |
27 |
Correct |
900 ms |
10068 KB |
Output is correct |
28 |
Correct |
880 ms |
10384 KB |
Output is correct |
29 |
Correct |
702 ms |
10292 KB |
Output is correct |
30 |
Correct |
866 ms |
10268 KB |
Output is correct |
31 |
Correct |
819 ms |
10380 KB |
Output is correct |
32 |
Correct |
652 ms |
10164 KB |
Output is correct |
33 |
Correct |
857 ms |
10560 KB |
Output is correct |
34 |
Correct |
464 ms |
10576 KB |
Output is correct |
35 |
Correct |
645 ms |
10516 KB |
Output is correct |
36 |
Correct |
1687 ms |
10584 KB |
Output is correct |
37 |
Correct |
978 ms |
10724 KB |
Output is correct |
38 |
Correct |
1216 ms |
10580 KB |
Output is correct |
39 |
Correct |
1362 ms |
10580 KB |
Output is correct |
40 |
Correct |
1519 ms |
10516 KB |
Output is correct |
41 |
Correct |
1750 ms |
10644 KB |
Output is correct |
42 |
Correct |
1712 ms |
10476 KB |
Output is correct |
43 |
Correct |
1747 ms |
10468 KB |
Output is correct |
44 |
Correct |
1280 ms |
10476 KB |
Output is correct |
45 |
Correct |
829 ms |
10832 KB |
Output is correct |
46 |
Correct |
1722 ms |
10636 KB |
Output is correct |
47 |
Correct |
1696 ms |
10480 KB |
Output is correct |
48 |
Correct |
1476 ms |
10576 KB |
Output is correct |
49 |
Correct |
1655 ms |
10512 KB |
Output is correct |
50 |
Correct |
636 ms |
10672 KB |
Output is correct |
51 |
Correct |
929 ms |
10688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6936 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6836 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
2 ms |
7004 KB |
Output is correct |
20 |
Correct |
838 ms |
10124 KB |
Output is correct |
21 |
Correct |
438 ms |
10064 KB |
Output is correct |
22 |
Correct |
587 ms |
10400 KB |
Output is correct |
23 |
Correct |
650 ms |
10264 KB |
Output is correct |
24 |
Correct |
713 ms |
10432 KB |
Output is correct |
25 |
Correct |
810 ms |
10068 KB |
Output is correct |
26 |
Correct |
843 ms |
10256 KB |
Output is correct |
27 |
Correct |
900 ms |
10068 KB |
Output is correct |
28 |
Correct |
880 ms |
10384 KB |
Output is correct |
29 |
Correct |
702 ms |
10292 KB |
Output is correct |
30 |
Correct |
866 ms |
10268 KB |
Output is correct |
31 |
Correct |
819 ms |
10380 KB |
Output is correct |
32 |
Correct |
652 ms |
10164 KB |
Output is correct |
33 |
Correct |
857 ms |
10560 KB |
Output is correct |
34 |
Correct |
464 ms |
10576 KB |
Output is correct |
35 |
Correct |
645 ms |
10516 KB |
Output is correct |
36 |
Correct |
2 ms |
6748 KB |
Output is correct |
37 |
Correct |
2 ms |
6748 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Runtime error |
710 ms |
26004 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |