#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define al3 array<ll,3>
#define pll pair<ll,ll>
#define fs first
#define sc second
const int mxn = 1e5+10;
int N,M,Q;
array<ll,3> edges[mxn];
struct DSU{
int dsu[550],sz[550];
void init(int n){
for(int i = 0;i<=n;i++){
dsu[i] = i;
sz[i] = 1;
}
}
int root(int k){
return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
void onion(int a,int b){
a = root(a),b = root(b);
if(a == b)return;
if(sz[a]<sz[b])swap(a,b);
dsu[b] = a;
sz[a] += sz[b];
return;
}
};
DSU dsu;
namespace S1{
void solve(){
ll K;
cin>>K;
sort(edges,edges+M,[&](al3 &a,al3 &b){return abs(a[0]-K)<abs(b[0]-K);});
dsu.init(N);
ll ans = 0;
for(int i = 0;i<M;i++){
auto [w,a,b] = edges[i];
if(dsu.root(a) == dsu.root(b))continue;
ans += abs(w-K);
dsu.onion(a,b);
}
cout<<ans<<'\n';
return;
}
}
namespace S2{
vector<pll> req;
ll ans[mxn*10];
vector<int> v[550];
int ptr[550];
void solve(){
for(int i = 0;i<M;i++){
v[min(edges[i][1],edges[i][2])].push_back(edges[i][0]);
}
for(int i = 1;i<=N;i++){
sort(v[i].begin(),v[i].end());
}
for(int i = 1;i<=Q;i++){
ll k;
cin>>k;
req.push_back(pll(k,i));
}
sort(req.begin(),req.end());
for(auto &i:req){
ll tans = 0;
for(int j = 1;j<N;j++){
while(ptr[j]+1<v[j].size()&&abs(v[j][ptr[j]]-i.fs)>=abs(v[j][ptr[j]+1]-i.fs))ptr[j]++;
tans += abs(v[j][ptr[j]]-i.fs);
}
ans[i.sc] = tans;
}
for(int i = 1;i<=Q;i++){
cout<<ans[i]<<'\n';
}
return;
}
}
namespace S3{
vector<pll> req;
ll ans[mxn*10];
void solve(){
sort(edges,edges+M);
int pt = 0;
for(int i = 1;i<=Q;i++){
int K;
cin>>K;
req.push_back(pll(K,i));
}
sort(req.begin(),req.end());
for(auto &i:req){
dsu.init(N);
while(pt<M&&edges[pt][0]<=i.fs)pt++;
int pl = pt-1,pr = pt;
ll tans = 0;
while(pl>=0||pr<M){
bool isl = false;
if(pr >= M)isl = true;
else if(pl < 0)isl = false;
else if(i.fs-edges[pl][0]<edges[pr][0]-i.fs)isl = true;
if(isl){
if(dsu.root(edges[pl][1]) != dsu.root(edges[pl][2])){
dsu.onion(edges[pl][1],edges[pl][2]);
tans += i.fs-edges[pl][0];
}
pl--;
}
else{
if(dsu.root(edges[pr][1]) != dsu.root(edges[pr][2])){
dsu.onion(edges[pr][1],edges[pr][2]);
tans += edges[pr][0]-i.fs;
}
pr++;
}
}
ans[i.sc] = tans;
}
for(int i = 1;i<=Q;i++){
cout<<ans[i]<<'\n';
}
return;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
for(int i = 0;i<M;i++){
int a,b,c;
cin>>a>>b>>c;
edges[i] = array<ll,3>({c,a,b});
}
cin>>Q;
if(Q<=10)while(Q--)S1::solve();
else if(M<=1000)S3::solve();
else S2::solve();
}
Compilation message
reconstruction.cpp: In function 'void S2::solve()':
reconstruction.cpp:76:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | while(ptr[j]+1<v[j].size()&&abs(v[j][ptr[j]]-i.fs)>=abs(v[j][ptr[j]+1]-i.fs))ptr[j]++;
| ~~~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4568 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4572 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4568 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4572 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
69 ms |
7024 KB |
Output is correct |
21 |
Correct |
66 ms |
7008 KB |
Output is correct |
22 |
Correct |
73 ms |
6884 KB |
Output is correct |
23 |
Correct |
84 ms |
7012 KB |
Output is correct |
24 |
Correct |
65 ms |
6884 KB |
Output is correct |
25 |
Correct |
65 ms |
7000 KB |
Output is correct |
26 |
Correct |
74 ms |
7000 KB |
Output is correct |
27 |
Correct |
70 ms |
6996 KB |
Output is correct |
28 |
Correct |
47 ms |
5868 KB |
Output is correct |
29 |
Correct |
44 ms |
5980 KB |
Output is correct |
30 |
Correct |
68 ms |
5996 KB |
Output is correct |
31 |
Correct |
79 ms |
5980 KB |
Output is correct |
32 |
Correct |
69 ms |
5980 KB |
Output is correct |
33 |
Correct |
66 ms |
5984 KB |
Output is correct |
34 |
Correct |
43 ms |
5980 KB |
Output is correct |
35 |
Correct |
48 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1639 ms |
46840 KB |
Output is correct |
5 |
Correct |
1641 ms |
51240 KB |
Output is correct |
6 |
Correct |
1731 ms |
50900 KB |
Output is correct |
7 |
Correct |
1172 ms |
52708 KB |
Output is correct |
8 |
Correct |
1084 ms |
52928 KB |
Output is correct |
9 |
Correct |
1018 ms |
52976 KB |
Output is correct |
10 |
Correct |
1692 ms |
51184 KB |
Output is correct |
11 |
Correct |
1134 ms |
53244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4568 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4572 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Execution timed out |
5065 ms |
29068 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4568 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4572 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
69 ms |
7024 KB |
Output is correct |
21 |
Correct |
66 ms |
7008 KB |
Output is correct |
22 |
Correct |
73 ms |
6884 KB |
Output is correct |
23 |
Correct |
84 ms |
7012 KB |
Output is correct |
24 |
Correct |
65 ms |
6884 KB |
Output is correct |
25 |
Correct |
65 ms |
7000 KB |
Output is correct |
26 |
Correct |
74 ms |
7000 KB |
Output is correct |
27 |
Correct |
70 ms |
6996 KB |
Output is correct |
28 |
Correct |
47 ms |
5868 KB |
Output is correct |
29 |
Correct |
44 ms |
5980 KB |
Output is correct |
30 |
Correct |
68 ms |
5996 KB |
Output is correct |
31 |
Correct |
79 ms |
5980 KB |
Output is correct |
32 |
Correct |
69 ms |
5980 KB |
Output is correct |
33 |
Correct |
66 ms |
5984 KB |
Output is correct |
34 |
Correct |
43 ms |
5980 KB |
Output is correct |
35 |
Correct |
48 ms |
5980 KB |
Output is correct |
36 |
Incorrect |
66 ms |
7116 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4568 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4572 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
69 ms |
7024 KB |
Output is correct |
21 |
Correct |
66 ms |
7008 KB |
Output is correct |
22 |
Correct |
73 ms |
6884 KB |
Output is correct |
23 |
Correct |
84 ms |
7012 KB |
Output is correct |
24 |
Correct |
65 ms |
6884 KB |
Output is correct |
25 |
Correct |
65 ms |
7000 KB |
Output is correct |
26 |
Correct |
74 ms |
7000 KB |
Output is correct |
27 |
Correct |
70 ms |
6996 KB |
Output is correct |
28 |
Correct |
47 ms |
5868 KB |
Output is correct |
29 |
Correct |
44 ms |
5980 KB |
Output is correct |
30 |
Correct |
68 ms |
5996 KB |
Output is correct |
31 |
Correct |
79 ms |
5980 KB |
Output is correct |
32 |
Correct |
69 ms |
5980 KB |
Output is correct |
33 |
Correct |
66 ms |
5984 KB |
Output is correct |
34 |
Correct |
43 ms |
5980 KB |
Output is correct |
35 |
Correct |
48 ms |
5980 KB |
Output is correct |
36 |
Correct |
1 ms |
4440 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1639 ms |
46840 KB |
Output is correct |
40 |
Correct |
1641 ms |
51240 KB |
Output is correct |
41 |
Correct |
1731 ms |
50900 KB |
Output is correct |
42 |
Correct |
1172 ms |
52708 KB |
Output is correct |
43 |
Correct |
1084 ms |
52928 KB |
Output is correct |
44 |
Correct |
1018 ms |
52976 KB |
Output is correct |
45 |
Correct |
1692 ms |
51184 KB |
Output is correct |
46 |
Correct |
1134 ms |
53244 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Execution timed out |
5065 ms |
29068 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |