#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx","avx2")
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=505;
const int M=1000005;
const ll INF=1e18;
struct DSU{
vector<int> pa,sz;
DSU(int n){
pa.resize(n);
sz.resize(n,1);
iota(all(pa),0);
}
int Find(int k){
return (k==pa[k]?k:pa[k]=Find(pa[k]));
}
void onion(int a,int b){
a=Find(a),b=Find(b);
if(a==b)return;
if(sz[a]>sz[b]){
swap(a,b);
}
pa[a]=b;
sz[b]+=sz[a];
}
int same(int a,int b){
return Find(a)==Find(b);
}
};
int32_t main() {
LCBorz;
int n,m;cin>>n>>m;
vector<array<int,3>> v(m);
for(int i=0;i<m;i++){
cin>>v[i][1]>>v[i][2]>>v[i][0];
}
sort(all(v));
int ptr=0;
int q;cin>>q;
for(int q1=0;q1<q;q1++){
int x;cin>>x;
while(ptr<m&&v[ptr][0]<=x){
ptr++;
}
DSU dsu(n+1);
ll ans=0;
int l=ptr-1,r=ptr,cnt=0;
while(l>=0&&r<m&&cnt<n-1){
if(abs(x-v[l][0])<abs(x-v[r][0])){
if(!dsu.same(v[l][1],v[l][2])){
dsu.onion(v[l][1],v[l][2]);
ans+=abs(x-v[l][0]);
cnt++;
}
l--;
}
else{
if(!dsu.same(v[r][1],v[r][2])){
dsu.onion(v[r][1],v[r][2]);
ans+=abs(x-v[r][0]);
cnt++;
}
r++;
}
}
while(cnt<n-1&&l>=0){
if(!dsu.same(v[l][1],v[l][2])){
dsu.onion(v[l][1],v[l][2]);
ans+=abs(x-v[l][0]);
cnt++;
}
l--;
}
while(cnt<n-1&&r<m){
if(!dsu.same(v[r][1],v[r][2])){
dsu.onion(v[r][1],v[r][2]);
ans+=abs(x-v[r][0]);
cnt++;
}
r++;
}
cout<<ans<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
27 ms |
1640 KB |
Output is correct |
21 |
Correct |
34 ms |
1628 KB |
Output is correct |
22 |
Correct |
30 ms |
1884 KB |
Output is correct |
23 |
Correct |
28 ms |
1644 KB |
Output is correct |
24 |
Correct |
27 ms |
1628 KB |
Output is correct |
25 |
Correct |
28 ms |
1628 KB |
Output is correct |
26 |
Correct |
28 ms |
1628 KB |
Output is correct |
27 |
Correct |
27 ms |
1880 KB |
Output is correct |
28 |
Correct |
30 ms |
1624 KB |
Output is correct |
29 |
Correct |
32 ms |
1624 KB |
Output is correct |
30 |
Correct |
31 ms |
1884 KB |
Output is correct |
31 |
Correct |
27 ms |
1628 KB |
Output is correct |
32 |
Correct |
28 ms |
1628 KB |
Output is correct |
33 |
Correct |
28 ms |
1628 KB |
Output is correct |
34 |
Correct |
31 ms |
1628 KB |
Output is correct |
35 |
Correct |
32 ms |
1624 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 |
5070 ms |
4192 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
5061 ms |
10116 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
27 ms |
1640 KB |
Output is correct |
21 |
Correct |
34 ms |
1628 KB |
Output is correct |
22 |
Correct |
30 ms |
1884 KB |
Output is correct |
23 |
Correct |
28 ms |
1644 KB |
Output is correct |
24 |
Correct |
27 ms |
1628 KB |
Output is correct |
25 |
Correct |
28 ms |
1628 KB |
Output is correct |
26 |
Correct |
28 ms |
1628 KB |
Output is correct |
27 |
Correct |
27 ms |
1880 KB |
Output is correct |
28 |
Correct |
30 ms |
1624 KB |
Output is correct |
29 |
Correct |
32 ms |
1624 KB |
Output is correct |
30 |
Correct |
31 ms |
1884 KB |
Output is correct |
31 |
Correct |
27 ms |
1628 KB |
Output is correct |
32 |
Correct |
28 ms |
1628 KB |
Output is correct |
33 |
Correct |
28 ms |
1628 KB |
Output is correct |
34 |
Correct |
31 ms |
1628 KB |
Output is correct |
35 |
Correct |
32 ms |
1624 KB |
Output is correct |
36 |
Correct |
538 ms |
1628 KB |
Output is correct |
37 |
Execution timed out |
5035 ms |
3612 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
27 ms |
1640 KB |
Output is correct |
21 |
Correct |
34 ms |
1628 KB |
Output is correct |
22 |
Correct |
30 ms |
1884 KB |
Output is correct |
23 |
Correct |
28 ms |
1644 KB |
Output is correct |
24 |
Correct |
27 ms |
1628 KB |
Output is correct |
25 |
Correct |
28 ms |
1628 KB |
Output is correct |
26 |
Correct |
28 ms |
1628 KB |
Output is correct |
27 |
Correct |
27 ms |
1880 KB |
Output is correct |
28 |
Correct |
30 ms |
1624 KB |
Output is correct |
29 |
Correct |
32 ms |
1624 KB |
Output is correct |
30 |
Correct |
31 ms |
1884 KB |
Output is correct |
31 |
Correct |
27 ms |
1628 KB |
Output is correct |
32 |
Correct |
28 ms |
1628 KB |
Output is correct |
33 |
Correct |
28 ms |
1628 KB |
Output is correct |
34 |
Correct |
31 ms |
1628 KB |
Output is correct |
35 |
Correct |
32 ms |
1624 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 |
5070 ms |
4192 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |