#pragma GCC optimize("Ofast","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
struct UFDS{
vector<int> p,siz;
UFDS(int n):p(n+1),siz(n+1,1){iota(p.begin(),p.end(),0);}
int findRoot(int x){
return p[x]==x ? x : p[x]=findRoot(p[x]);
}
bool unite(int a,int b){
a = findRoot(a);
b = findRoot(b);
if(a==b)return false;
if(siz[a]<siz[b])swap(a,b);
siz[a]+=siz[b];
p[b]=a;
return true;
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin >> n >> m;
vector<pair<int,pair<int,int>>> edges(m);
for(auto&[a,b]:edges)cin>>b.first>>b.second>>a;
sort(edges.begin(),edges.end());
vector<set<pair<int,int>>> adj;
vector<int> replaces_front(m,-1);
{
// Calculation from the front
adj = vector<set<pair<int,int>>>(n+1);
int tar;
function<pair<bool,int>(int,int)> dfs = [&](int x,int p){
if(x==tar)return make_pair(true,(int)1e15);
for(auto[v,i]:adj[x])if(v!=p){
auto res = dfs(v,x);
if(res.first)return make_pair(true,min(res.second,i));
}
return make_pair(false,(int)0);
};
for(int i=0;i<m;i++){
tar = edges[i].second.second;
auto res = dfs(edges[i].second.first,-1);
if(res.first){
adj[edges[res.second].second.first].erase({edges[res.second].second.second,res.second});
adj[edges[res.second].second.second].erase({edges[res.second].second.first,res.second});
replaces_front[i] = res.second;
}
adj[edges[i].second.first].insert({edges[i].second.second,i});
adj[edges[i].second.second].insert({edges[i].second.first,i});
}
}
vector<int> replaces_back(m,-1);
{
// Calculation from the back
adj = vector<set<pair<int,int>>>(n+1);
int tar;
function<pair<bool,int>(int,int)> dfs = [&](int x,int p){
if(x==tar)return make_pair(true,(int)-1);
for(auto[v,i]:adj[x])if(v!=p){
auto res = dfs(v,x);
if(res.first)return make_pair(true,max(res.second,i));
}
return make_pair(false,(int)0);
};
for(int i=m-1;i>=0;i--){
tar = edges[i].second.second;
auto res = dfs(edges[i].second.first,-1);
if(res.first){
adj[edges[res.second].second.first].erase({edges[res.second].second.second,res.second});
adj[edges[res.second].second.second].erase({edges[res.second].second.first,res.second});
replaces_back[i] = res.second;
}
adj[edges[i].second.first].insert({edges[i].second.second,i});
adj[edges[i].second.second].insert({edges[i].second.first,i});
}
}
set<int> curr_left;
set<int> curr_right;
for(auto&i:adj)for(auto[a,b]:i)curr_right.insert(b);
auto solve = [&](int x){
vector<pair<int,int>> curredges;
for(int i:curr_left){
curredges.emplace_back(abs(edges[i].first-x),i);
}
for(int i:curr_right){
curredges.emplace_back(abs(edges[i].first-x),i);
}
sort(curredges.begin(),curredges.end());
int ans = 0;
UFDS dsu(n);
for(auto[c,i]:curredges){
if(dsu.unite(edges[i].second.first,edges[i].second.second))ans+=c;
}
return ans;
};
int iter = 0;
int q;
cin >> q;
for(int i=1;i<=q;i++){
int query;
cin >> query;
while(iter!=m and edges[iter].first<query){
curr_right.erase(iter);
if(replaces_back[iter]!=-1){
curr_right.insert(replaces_back[iter]);
}
curr_left.insert(iter);
if(replaces_front[iter]!=-1){
curr_left.erase(replaces_front[iter]);
}
iter++;
}
cout << solve(query) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
828 ms |
4348 KB |
Output is correct |
21 |
Correct |
403 ms |
4436 KB |
Output is correct |
22 |
Correct |
615 ms |
4440 KB |
Output is correct |
23 |
Correct |
623 ms |
4692 KB |
Output is correct |
24 |
Correct |
715 ms |
4432 KB |
Output is correct |
25 |
Correct |
804 ms |
4452 KB |
Output is correct |
26 |
Correct |
841 ms |
4344 KB |
Output is correct |
27 |
Correct |
839 ms |
4432 KB |
Output is correct |
28 |
Correct |
857 ms |
4476 KB |
Output is correct |
29 |
Correct |
640 ms |
4528 KB |
Output is correct |
30 |
Correct |
825 ms |
4428 KB |
Output is correct |
31 |
Correct |
831 ms |
4440 KB |
Output is correct |
32 |
Correct |
840 ms |
4436 KB |
Output is correct |
33 |
Correct |
841 ms |
4436 KB |
Output is correct |
34 |
Correct |
392 ms |
4344 KB |
Output is correct |
35 |
Correct |
813 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5089 ms |
5816 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
5082 ms |
5072 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
828 ms |
4348 KB |
Output is correct |
21 |
Correct |
403 ms |
4436 KB |
Output is correct |
22 |
Correct |
615 ms |
4440 KB |
Output is correct |
23 |
Correct |
623 ms |
4692 KB |
Output is correct |
24 |
Correct |
715 ms |
4432 KB |
Output is correct |
25 |
Correct |
804 ms |
4452 KB |
Output is correct |
26 |
Correct |
841 ms |
4344 KB |
Output is correct |
27 |
Correct |
839 ms |
4432 KB |
Output is correct |
28 |
Correct |
857 ms |
4476 KB |
Output is correct |
29 |
Correct |
640 ms |
4528 KB |
Output is correct |
30 |
Correct |
825 ms |
4428 KB |
Output is correct |
31 |
Correct |
831 ms |
4440 KB |
Output is correct |
32 |
Correct |
840 ms |
4436 KB |
Output is correct |
33 |
Correct |
841 ms |
4436 KB |
Output is correct |
34 |
Correct |
392 ms |
4344 KB |
Output is correct |
35 |
Correct |
813 ms |
4432 KB |
Output is correct |
36 |
Correct |
1791 ms |
4728 KB |
Output is correct |
37 |
Correct |
1193 ms |
4688 KB |
Output is correct |
38 |
Correct |
1527 ms |
4688 KB |
Output is correct |
39 |
Correct |
1674 ms |
4688 KB |
Output is correct |
40 |
Correct |
1678 ms |
4692 KB |
Output is correct |
41 |
Correct |
1747 ms |
4688 KB |
Output is correct |
42 |
Correct |
1736 ms |
4648 KB |
Output is correct |
43 |
Correct |
1787 ms |
4684 KB |
Output is correct |
44 |
Correct |
1390 ms |
4688 KB |
Output is correct |
45 |
Correct |
1051 ms |
4688 KB |
Output is correct |
46 |
Correct |
1738 ms |
4696 KB |
Output is correct |
47 |
Correct |
1842 ms |
4692 KB |
Output is correct |
48 |
Correct |
1841 ms |
4692 KB |
Output is correct |
49 |
Correct |
1717 ms |
4588 KB |
Output is correct |
50 |
Correct |
721 ms |
4688 KB |
Output is correct |
51 |
Correct |
1376 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
828 ms |
4348 KB |
Output is correct |
21 |
Correct |
403 ms |
4436 KB |
Output is correct |
22 |
Correct |
615 ms |
4440 KB |
Output is correct |
23 |
Correct |
623 ms |
4692 KB |
Output is correct |
24 |
Correct |
715 ms |
4432 KB |
Output is correct |
25 |
Correct |
804 ms |
4452 KB |
Output is correct |
26 |
Correct |
841 ms |
4344 KB |
Output is correct |
27 |
Correct |
839 ms |
4432 KB |
Output is correct |
28 |
Correct |
857 ms |
4476 KB |
Output is correct |
29 |
Correct |
640 ms |
4528 KB |
Output is correct |
30 |
Correct |
825 ms |
4428 KB |
Output is correct |
31 |
Correct |
831 ms |
4440 KB |
Output is correct |
32 |
Correct |
840 ms |
4436 KB |
Output is correct |
33 |
Correct |
841 ms |
4436 KB |
Output is correct |
34 |
Correct |
392 ms |
4344 KB |
Output is correct |
35 |
Correct |
813 ms |
4432 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5089 ms |
5816 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |