#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=505;
int N,M,Q;
struct DSU {
int par[MX], sz[MX];
int fd(int v) {
return par[v]==v?v:par[v]=fd(par[v]);
}
bool merge(int u, int v) {
u=fd(u),v=fd(v);
if(u==v) return false;
if(sz[u]>sz[v]) swap(u,v);
par[u]=v;
sz[v]+=sz[u];
return true;
}
void reset() {
for(int i=1;i<=N;i++) par[i]=i,sz[i]=1;
}
} ds;
set<pair<int,int>> adj[MX];
set<array<int,3>> E;
array<int,3> bfs(int S, int T) {
queue<int> q;
q.push(S);
vector<array<int,3>> dist(N+1,{-1,-1,-1});
dist[S]={(int)1e9,0,0};
while(!q.empty()) {
auto v=q.front(); q.pop();
for(auto [u,w]:adj[v]) {
if(dist[u][0]!=-1) continue;
array<int,3> cur={w,u,v};
dist[u]=min(cur,dist[v]);
q.push(u);
}
}
return dist[T];
}
array<int,3> BFS(int S, int T) {
queue<int> q;
q.push(S);
vector<array<int,3>> dist(N+1,{-1,-1,-1});
dist[S]={0,0,0};
while(!q.empty()) {
auto v=q.front(); q.pop();
for(auto [u,w]:adj[v]) {
if(dist[u][0]!=-1) continue;
array<int,3> cur={w,u,v};
dist[u]=max(cur,dist[v]);
q.push(u);
}
}
return dist[T];
}
vector<array<int,3>> PF[100000+5], SF[100000+5];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>M;
vector<array<int,3>> edges;
for(int i=0;i<M;i++) {
int u,v,w;
cin>>u>>v>>w;
edges.push_back({w,u,v});
}
sort(edges.begin(),edges.end());
int i=0;
for(auto [w,u,v]:edges) {
auto [ww,uu,vv]=bfs(u,v);
if(ww!=-1) {
adj[uu].erase({vv,ww});
adj[vv].erase({uu,ww});
E.erase({ww,min(uu,vv),max(uu,vv)});
}
adj[u].insert({v,w});
adj[v].insert({u,w});
E.insert({w,min(u,v),max(u,v)});
for(auto [ww,uu,vv]:E) {
PF[i].push_back({ww,uu,vv});
}
reverse(PF[i].begin(),PF[i].end());
i++;
}
reverse(edges.begin(),edges.end());
E.clear();
for(int i=1;i<=N;i++) adj[i].clear();
i=M-1;
for(auto [w,u,v]:edges) {
auto [ww,uu,vv]=BFS(u,v);
if(ww!=-1) {
adj[uu].erase({vv,ww});
adj[vv].erase({uu,ww});
E.erase({ww,min(uu,vv),max(uu,vv)});
}
adj[u].insert({v,w});
adj[v].insert({u,w});
E.insert({w,min(u,v),max(u,v)});
for(auto [ww,uu,vv]:E) {
SF[i].push_back({ww,uu,vv});
}
i--;
}
reverse(edges.begin(),edges.end());
cin>>Q;
int p=-1;
for(int i=0;i<Q;i++) {
int x;
cin>>x;
while(p+1<edges.size() && x>=edges[p+1][0]) {
p++;
}
ds.reset();
ll res=0;
if(p==(int)edges.size()-1) {
// PF back()
for(auto [w,u,v]:PF[p]) {
res+=x-w;
}
} else if(p==-1) {
// SF[0]
for(auto [w,u,v]:SF[0]) {
res+=w-x;
}
} else {
// PF[p], Sf[p+1]
int i=0,j=0; // 2 pointers
while(i<PF[p].size()&&j<SF[p+1].size()) {
auto [w,u,v]=PF[p][i];
auto [ww,uu,vv]=SF[p+1][j];
if(x-w<ww-x) {
if(ds.merge(u,v))
res+=x-w;
i++;
} else {
if(ds.merge(uu,vv))
res+=ww-x;
j++;
}
}
while(i<PF[p].size()) {
auto [w,u,v]=PF[p][i];
if(ds.merge(u,v))
res+=x-w;
i++;
}
while(j<SF[p+1].size()) {
auto [w,u,v]=SF[p+1][j];
if(ds.merge(u,v))
res+=w-x;
j++;
}
}
cout<<res<<'\n';
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:141:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | while(p+1<edges.size() && x>=edges[p+1][0]) {
| ~~~^~~~~~~~~~~~~
reconstruction.cpp:160:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | while(i<PF[p].size()&&j<SF[p+1].size()) {
| ~^~~~~~~~~~~~~
reconstruction.cpp:160:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | while(i<PF[p].size()&&j<SF[p+1].size()) {
| ~^~~~~~~~~~~~~~~
reconstruction.cpp:174:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
174 | while(i<PF[p].size()) {
| ~^~~~~~~~~~~~~
reconstruction.cpp:181:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
181 | while(j<SF[p+1].size()) {
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5040 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5040 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4952 KB |
Output is correct |
20 |
Runtime error |
3556 ms |
1048576 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Runtime error |
2657 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5040 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4956 KB |
Output is correct |
20 |
Correct |
4285 ms |
37092 KB |
Output is correct |
21 |
Correct |
4234 ms |
37096 KB |
Output is correct |
22 |
Correct |
4295 ms |
36996 KB |
Output is correct |
23 |
Correct |
4158 ms |
37096 KB |
Output is correct |
24 |
Correct |
4225 ms |
37136 KB |
Output is correct |
25 |
Correct |
4258 ms |
37320 KB |
Output is correct |
26 |
Correct |
4182 ms |
37200 KB |
Output is correct |
27 |
Correct |
3418 ms |
37100 KB |
Output is correct |
28 |
Correct |
4277 ms |
37108 KB |
Output is correct |
29 |
Correct |
4190 ms |
37088 KB |
Output is correct |
30 |
Correct |
4148 ms |
37496 KB |
Output is correct |
31 |
Correct |
4271 ms |
36984 KB |
Output is correct |
32 |
Correct |
819 ms |
40516 KB |
Output is correct |
33 |
Execution timed out |
5005 ms |
36624 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5040 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4952 KB |
Output is correct |
20 |
Runtime error |
3556 ms |
1048576 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5040 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
1 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4952 KB |
Output is correct |
17 |
Correct |
1 ms |
4956 KB |
Output is correct |
18 |
Correct |
1 ms |
4952 KB |
Output is correct |
19 |
Correct |
1 ms |
4952 KB |
Output is correct |
20 |
Runtime error |
3556 ms |
1048576 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |