#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+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() {
ios::sync_with_stdio(false);
cin.tie(0);
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
53596 KB |
Output is correct |
2 |
Correct |
11 ms |
53728 KB |
Output is correct |
3 |
Correct |
11 ms |
53596 KB |
Output is correct |
4 |
Correct |
11 ms |
53740 KB |
Output is correct |
5 |
Correct |
10 ms |
53592 KB |
Output is correct |
6 |
Correct |
11 ms |
53596 KB |
Output is correct |
7 |
Correct |
11 ms |
53596 KB |
Output is correct |
8 |
Correct |
11 ms |
53596 KB |
Output is correct |
9 |
Correct |
11 ms |
53596 KB |
Output is correct |
10 |
Correct |
11 ms |
53812 KB |
Output is correct |
11 |
Correct |
11 ms |
53592 KB |
Output is correct |
12 |
Correct |
11 ms |
53592 KB |
Output is correct |
13 |
Correct |
11 ms |
53596 KB |
Output is correct |
14 |
Correct |
11 ms |
53848 KB |
Output is correct |
15 |
Correct |
11 ms |
53592 KB |
Output is correct |
16 |
Correct |
11 ms |
53596 KB |
Output is correct |
17 |
Correct |
11 ms |
53596 KB |
Output is correct |
18 |
Correct |
11 ms |
53728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
53596 KB |
Output is correct |
2 |
Correct |
11 ms |
53728 KB |
Output is correct |
3 |
Correct |
11 ms |
53596 KB |
Output is correct |
4 |
Correct |
11 ms |
53740 KB |
Output is correct |
5 |
Correct |
10 ms |
53592 KB |
Output is correct |
6 |
Correct |
11 ms |
53596 KB |
Output is correct |
7 |
Correct |
11 ms |
53596 KB |
Output is correct |
8 |
Correct |
11 ms |
53596 KB |
Output is correct |
9 |
Correct |
11 ms |
53596 KB |
Output is correct |
10 |
Correct |
11 ms |
53812 KB |
Output is correct |
11 |
Correct |
11 ms |
53592 KB |
Output is correct |
12 |
Correct |
11 ms |
53592 KB |
Output is correct |
13 |
Correct |
11 ms |
53596 KB |
Output is correct |
14 |
Correct |
11 ms |
53848 KB |
Output is correct |
15 |
Correct |
11 ms |
53592 KB |
Output is correct |
16 |
Correct |
11 ms |
53596 KB |
Output is correct |
17 |
Correct |
11 ms |
53596 KB |
Output is correct |
18 |
Correct |
11 ms |
53728 KB |
Output is correct |
19 |
Correct |
11 ms |
53596 KB |
Output is correct |
20 |
Correct |
817 ms |
54980 KB |
Output is correct |
21 |
Correct |
402 ms |
54864 KB |
Output is correct |
22 |
Correct |
549 ms |
55052 KB |
Output is correct |
23 |
Correct |
623 ms |
55044 KB |
Output is correct |
24 |
Correct |
722 ms |
55044 KB |
Output is correct |
25 |
Correct |
796 ms |
54864 KB |
Output is correct |
26 |
Correct |
842 ms |
55048 KB |
Output is correct |
27 |
Correct |
815 ms |
54988 KB |
Output is correct |
28 |
Correct |
804 ms |
54868 KB |
Output is correct |
29 |
Correct |
613 ms |
55024 KB |
Output is correct |
30 |
Correct |
792 ms |
54868 KB |
Output is correct |
31 |
Correct |
739 ms |
55124 KB |
Output is correct |
32 |
Correct |
613 ms |
55040 KB |
Output is correct |
33 |
Correct |
798 ms |
55044 KB |
Output is correct |
34 |
Correct |
428 ms |
54868 KB |
Output is correct |
35 |
Correct |
603 ms |
54864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
53812 KB |
Output is correct |
2 |
Correct |
11 ms |
53712 KB |
Output is correct |
3 |
Correct |
10 ms |
53592 KB |
Output is correct |
4 |
Execution timed out |
5038 ms |
63552 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
53596 KB |
Output is correct |
2 |
Correct |
11 ms |
53728 KB |
Output is correct |
3 |
Correct |
11 ms |
53596 KB |
Output is correct |
4 |
Correct |
11 ms |
53740 KB |
Output is correct |
5 |
Correct |
10 ms |
53592 KB |
Output is correct |
6 |
Correct |
11 ms |
53596 KB |
Output is correct |
7 |
Correct |
11 ms |
53596 KB |
Output is correct |
8 |
Correct |
11 ms |
53596 KB |
Output is correct |
9 |
Correct |
11 ms |
53596 KB |
Output is correct |
10 |
Correct |
11 ms |
53812 KB |
Output is correct |
11 |
Correct |
11 ms |
53592 KB |
Output is correct |
12 |
Correct |
11 ms |
53592 KB |
Output is correct |
13 |
Correct |
11 ms |
53596 KB |
Output is correct |
14 |
Correct |
11 ms |
53848 KB |
Output is correct |
15 |
Correct |
11 ms |
53592 KB |
Output is correct |
16 |
Correct |
11 ms |
53596 KB |
Output is correct |
17 |
Correct |
11 ms |
53596 KB |
Output is correct |
18 |
Correct |
11 ms |
53728 KB |
Output is correct |
19 |
Correct |
11 ms |
53592 KB |
Output is correct |
20 |
Execution timed out |
5095 ms |
64116 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
53596 KB |
Output is correct |
2 |
Correct |
11 ms |
53728 KB |
Output is correct |
3 |
Correct |
11 ms |
53596 KB |
Output is correct |
4 |
Correct |
11 ms |
53740 KB |
Output is correct |
5 |
Correct |
10 ms |
53592 KB |
Output is correct |
6 |
Correct |
11 ms |
53596 KB |
Output is correct |
7 |
Correct |
11 ms |
53596 KB |
Output is correct |
8 |
Correct |
11 ms |
53596 KB |
Output is correct |
9 |
Correct |
11 ms |
53596 KB |
Output is correct |
10 |
Correct |
11 ms |
53812 KB |
Output is correct |
11 |
Correct |
11 ms |
53592 KB |
Output is correct |
12 |
Correct |
11 ms |
53592 KB |
Output is correct |
13 |
Correct |
11 ms |
53596 KB |
Output is correct |
14 |
Correct |
11 ms |
53848 KB |
Output is correct |
15 |
Correct |
11 ms |
53592 KB |
Output is correct |
16 |
Correct |
11 ms |
53596 KB |
Output is correct |
17 |
Correct |
11 ms |
53596 KB |
Output is correct |
18 |
Correct |
11 ms |
53728 KB |
Output is correct |
19 |
Correct |
11 ms |
53596 KB |
Output is correct |
20 |
Correct |
817 ms |
54980 KB |
Output is correct |
21 |
Correct |
402 ms |
54864 KB |
Output is correct |
22 |
Correct |
549 ms |
55052 KB |
Output is correct |
23 |
Correct |
623 ms |
55044 KB |
Output is correct |
24 |
Correct |
722 ms |
55044 KB |
Output is correct |
25 |
Correct |
796 ms |
54864 KB |
Output is correct |
26 |
Correct |
842 ms |
55048 KB |
Output is correct |
27 |
Correct |
815 ms |
54988 KB |
Output is correct |
28 |
Correct |
804 ms |
54868 KB |
Output is correct |
29 |
Correct |
613 ms |
55024 KB |
Output is correct |
30 |
Correct |
792 ms |
54868 KB |
Output is correct |
31 |
Correct |
739 ms |
55124 KB |
Output is correct |
32 |
Correct |
613 ms |
55040 KB |
Output is correct |
33 |
Correct |
798 ms |
55044 KB |
Output is correct |
34 |
Correct |
428 ms |
54868 KB |
Output is correct |
35 |
Correct |
603 ms |
54864 KB |
Output is correct |
36 |
Correct |
1721 ms |
55380 KB |
Output is correct |
37 |
Correct |
925 ms |
55072 KB |
Output is correct |
38 |
Correct |
1182 ms |
55320 KB |
Output is correct |
39 |
Correct |
1340 ms |
55072 KB |
Output is correct |
40 |
Correct |
1507 ms |
55076 KB |
Output is correct |
41 |
Correct |
1661 ms |
55072 KB |
Output is correct |
42 |
Correct |
1666 ms |
55072 KB |
Output is correct |
43 |
Correct |
1681 ms |
55124 KB |
Output is correct |
44 |
Correct |
1248 ms |
55068 KB |
Output is correct |
45 |
Correct |
805 ms |
55300 KB |
Output is correct |
46 |
Correct |
1670 ms |
55068 KB |
Output is correct |
47 |
Correct |
1653 ms |
55292 KB |
Output is correct |
48 |
Correct |
1426 ms |
55120 KB |
Output is correct |
49 |
Correct |
1628 ms |
55076 KB |
Output is correct |
50 |
Correct |
578 ms |
55380 KB |
Output is correct |
51 |
Correct |
889 ms |
55072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
53596 KB |
Output is correct |
2 |
Correct |
11 ms |
53728 KB |
Output is correct |
3 |
Correct |
11 ms |
53596 KB |
Output is correct |
4 |
Correct |
11 ms |
53740 KB |
Output is correct |
5 |
Correct |
10 ms |
53592 KB |
Output is correct |
6 |
Correct |
11 ms |
53596 KB |
Output is correct |
7 |
Correct |
11 ms |
53596 KB |
Output is correct |
8 |
Correct |
11 ms |
53596 KB |
Output is correct |
9 |
Correct |
11 ms |
53596 KB |
Output is correct |
10 |
Correct |
11 ms |
53812 KB |
Output is correct |
11 |
Correct |
11 ms |
53592 KB |
Output is correct |
12 |
Correct |
11 ms |
53592 KB |
Output is correct |
13 |
Correct |
11 ms |
53596 KB |
Output is correct |
14 |
Correct |
11 ms |
53848 KB |
Output is correct |
15 |
Correct |
11 ms |
53592 KB |
Output is correct |
16 |
Correct |
11 ms |
53596 KB |
Output is correct |
17 |
Correct |
11 ms |
53596 KB |
Output is correct |
18 |
Correct |
11 ms |
53728 KB |
Output is correct |
19 |
Correct |
11 ms |
53596 KB |
Output is correct |
20 |
Correct |
817 ms |
54980 KB |
Output is correct |
21 |
Correct |
402 ms |
54864 KB |
Output is correct |
22 |
Correct |
549 ms |
55052 KB |
Output is correct |
23 |
Correct |
623 ms |
55044 KB |
Output is correct |
24 |
Correct |
722 ms |
55044 KB |
Output is correct |
25 |
Correct |
796 ms |
54864 KB |
Output is correct |
26 |
Correct |
842 ms |
55048 KB |
Output is correct |
27 |
Correct |
815 ms |
54988 KB |
Output is correct |
28 |
Correct |
804 ms |
54868 KB |
Output is correct |
29 |
Correct |
613 ms |
55024 KB |
Output is correct |
30 |
Correct |
792 ms |
54868 KB |
Output is correct |
31 |
Correct |
739 ms |
55124 KB |
Output is correct |
32 |
Correct |
613 ms |
55040 KB |
Output is correct |
33 |
Correct |
798 ms |
55044 KB |
Output is correct |
34 |
Correct |
428 ms |
54868 KB |
Output is correct |
35 |
Correct |
603 ms |
54864 KB |
Output is correct |
36 |
Correct |
11 ms |
53812 KB |
Output is correct |
37 |
Correct |
11 ms |
53712 KB |
Output is correct |
38 |
Correct |
10 ms |
53592 KB |
Output is correct |
39 |
Execution timed out |
5038 ms |
63552 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |