#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 500;
const int MAXM = 1e5;
const int MAXQ = 1e6;
const int INF = 1e9+7;
struct Edge
{
int u, v, w;
};
int N, M, Q;
Edge A[MAXM+10];
pii B[MAXQ+10];
vector<int> V[MAXM+10];
struct UF
{
int par[MAXN+10], sz[MAXN+10];
void init()
{
for(int i=1; i<=N; i++) par[i]=i, sz[i]=1;
}
int Find(int x)
{
if(x==par[x]) return x;
return par[x]=Find(par[x]);
}
void Union(int x, int y)
{
x=Find(x); y=Find(y);
if(x==y) return;
if(sz[x]>sz[y]) swap(x, y);
if(sz[x]==sz[y]) sz[y]++;
par[x]=y;
}
};
ll ans[MAXQ+10];
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=M; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
A[i]={u, v, w};
}
sort(A+1, A+M+1, [&](const Edge &p, const Edge &q) { return p.w<q.w; });
scanf("%d", &Q);
for(int i=1; i<=Q; i++) scanf("%d", &B[i].first), B[i].second=i;
sort(B+1, B+Q+1);
UF uf;
for(int i=M; i>=1; i--)
{
uf.init();
uf.Union(A[i].u, A[i].v);
V[i].push_back(i);
for(auto it : V[i+1])
{
if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
uf.Union(A[it].u, A[it].v);
V[i].push_back(it);
}
}
A[M+1].w=INF;
vector<int> V2;
for(int i=1, j=1; i<=M+1; i++)
{
for(; j<=Q && B[j].first<=A[i].w; j++)
{
uf.init();
int l=0, r=0;
while(l<V2.size() || r<V[i].size())
{
int it;
if(l<V2.size() && (r==V[i].size() || B[j].first-A[V2[l]].w<A[V[i][r]].w-B[j].first)) it=V2[l++];
else it=V[i][r++];
if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
uf.Union(A[it].u, A[it].v);
ans[B[j].second]+=abs(A[it].w-B[j].first);
}
}
if(i>M) break;
vector<int> VV;
uf.init();
uf.Union(A[i].u, A[i].v);
VV.push_back(i);
for(auto it : V2)
{
if(uf.Find(A[it].u)==uf.Find(A[it].v)) continue;
uf.Union(A[it].u, A[it].v);
VV.push_back(it);
}
V2=VV;
}
for(int i=1; i<=Q; i++) printf("%lld\n", ans[i]);
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:84:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | while(l<V2.size() || r<V[i].size())
| ~^~~~~~~~~~
reconstruction.cpp:84:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | while(l<V2.size() || r<V[i].size())
| ~^~~~~~~~~~~~
reconstruction.cpp:87:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | if(l<V2.size() && (r==V[i].size() || B[j].first-A[V2[l]].w<A[V[i][r]].w-B[j].first)) it=V2[l++];
| ~^~~~~~~~~~
reconstruction.cpp:87:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | if(l<V2.size() && (r==V[i].size() || B[j].first-A[V2[l]].w<A[V[i][r]].w-B[j].first)) it=V2[l++];
| ~^~~~~~~~~~~~~
reconstruction.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
reconstruction.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d%d%d", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
reconstruction.cpp:59:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | for(int i=1; i<=Q; i++) scanf("%d", &B[i].first), B[i].second=i;
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2720 KB |
Output is correct |
2 |
Correct |
1 ms |
2680 KB |
Output is correct |
3 |
Correct |
1 ms |
2672 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2668 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2668 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2720 KB |
Output is correct |
2 |
Correct |
1 ms |
2680 KB |
Output is correct |
3 |
Correct |
1 ms |
2672 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2668 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2668 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2672 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2490 ms |
207376 KB |
Output is correct |
21 |
Correct |
2318 ms |
207104 KB |
Output is correct |
22 |
Correct |
2361 ms |
207100 KB |
Output is correct |
23 |
Correct |
2316 ms |
207068 KB |
Output is correct |
24 |
Correct |
2376 ms |
207344 KB |
Output is correct |
25 |
Correct |
2460 ms |
207448 KB |
Output is correct |
26 |
Correct |
2469 ms |
207320 KB |
Output is correct |
27 |
Correct |
2467 ms |
207304 KB |
Output is correct |
28 |
Correct |
2474 ms |
207224 KB |
Output is correct |
29 |
Correct |
2459 ms |
207336 KB |
Output is correct |
30 |
Correct |
2537 ms |
207140 KB |
Output is correct |
31 |
Correct |
2452 ms |
207220 KB |
Output is correct |
32 |
Correct |
2458 ms |
207244 KB |
Output is correct |
33 |
Correct |
2456 ms |
207316 KB |
Output is correct |
34 |
Correct |
2463 ms |
207392 KB |
Output is correct |
35 |
Correct |
2447 ms |
207232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2776 KB |
Output is correct |
2 |
Correct |
1 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2664 KB |
Output is correct |
4 |
Execution timed out |
5032 ms |
225988 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2720 KB |
Output is correct |
2 |
Correct |
1 ms |
2680 KB |
Output is correct |
3 |
Correct |
1 ms |
2672 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2668 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2668 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2672 KB |
Output is correct |
19 |
Correct |
2 ms |
2792 KB |
Output is correct |
20 |
Execution timed out |
5017 ms |
24908 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2720 KB |
Output is correct |
2 |
Correct |
1 ms |
2680 KB |
Output is correct |
3 |
Correct |
1 ms |
2672 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2668 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2668 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2672 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2490 ms |
207376 KB |
Output is correct |
21 |
Correct |
2318 ms |
207104 KB |
Output is correct |
22 |
Correct |
2361 ms |
207100 KB |
Output is correct |
23 |
Correct |
2316 ms |
207068 KB |
Output is correct |
24 |
Correct |
2376 ms |
207344 KB |
Output is correct |
25 |
Correct |
2460 ms |
207448 KB |
Output is correct |
26 |
Correct |
2469 ms |
207320 KB |
Output is correct |
27 |
Correct |
2467 ms |
207304 KB |
Output is correct |
28 |
Correct |
2474 ms |
207224 KB |
Output is correct |
29 |
Correct |
2459 ms |
207336 KB |
Output is correct |
30 |
Correct |
2537 ms |
207140 KB |
Output is correct |
31 |
Correct |
2452 ms |
207220 KB |
Output is correct |
32 |
Correct |
2458 ms |
207244 KB |
Output is correct |
33 |
Correct |
2456 ms |
207316 KB |
Output is correct |
34 |
Correct |
2463 ms |
207392 KB |
Output is correct |
35 |
Correct |
2447 ms |
207232 KB |
Output is correct |
36 |
Correct |
3061 ms |
207948 KB |
Output is correct |
37 |
Correct |
2743 ms |
207884 KB |
Output is correct |
38 |
Correct |
2892 ms |
207920 KB |
Output is correct |
39 |
Correct |
2919 ms |
207768 KB |
Output is correct |
40 |
Correct |
2975 ms |
207948 KB |
Output is correct |
41 |
Correct |
3051 ms |
207952 KB |
Output is correct |
42 |
Correct |
3031 ms |
207936 KB |
Output is correct |
43 |
Correct |
3038 ms |
207808 KB |
Output is correct |
44 |
Correct |
3015 ms |
207840 KB |
Output is correct |
45 |
Correct |
2854 ms |
207860 KB |
Output is correct |
46 |
Correct |
3052 ms |
207904 KB |
Output is correct |
47 |
Correct |
3049 ms |
207908 KB |
Output is correct |
48 |
Correct |
3064 ms |
207856 KB |
Output is correct |
49 |
Correct |
3032 ms |
207872 KB |
Output is correct |
50 |
Correct |
2657 ms |
208088 KB |
Output is correct |
51 |
Correct |
2832 ms |
207832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2720 KB |
Output is correct |
2 |
Correct |
1 ms |
2680 KB |
Output is correct |
3 |
Correct |
1 ms |
2672 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2668 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2668 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2672 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2490 ms |
207376 KB |
Output is correct |
21 |
Correct |
2318 ms |
207104 KB |
Output is correct |
22 |
Correct |
2361 ms |
207100 KB |
Output is correct |
23 |
Correct |
2316 ms |
207068 KB |
Output is correct |
24 |
Correct |
2376 ms |
207344 KB |
Output is correct |
25 |
Correct |
2460 ms |
207448 KB |
Output is correct |
26 |
Correct |
2469 ms |
207320 KB |
Output is correct |
27 |
Correct |
2467 ms |
207304 KB |
Output is correct |
28 |
Correct |
2474 ms |
207224 KB |
Output is correct |
29 |
Correct |
2459 ms |
207336 KB |
Output is correct |
30 |
Correct |
2537 ms |
207140 KB |
Output is correct |
31 |
Correct |
2452 ms |
207220 KB |
Output is correct |
32 |
Correct |
2458 ms |
207244 KB |
Output is correct |
33 |
Correct |
2456 ms |
207316 KB |
Output is correct |
34 |
Correct |
2463 ms |
207392 KB |
Output is correct |
35 |
Correct |
2447 ms |
207232 KB |
Output is correct |
36 |
Correct |
1 ms |
2776 KB |
Output is correct |
37 |
Correct |
1 ms |
2668 KB |
Output is correct |
38 |
Correct |
2 ms |
2664 KB |
Output is correct |
39 |
Execution timed out |
5032 ms |
225988 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |