#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma gcc-optimize("o3, unroll-loops")
const int nx=5e2+5, mx=1e5+5;
ll n, m, a[mx], b[mx], w[mx], dsu[nx], x, q;
int find(int x)
{
if (dsu[x]==x) return x;
return dsu[x]=find(dsu[x]);
}
int32_t main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=m; i++) cin>>a[i]>>b[i]>>w[i];
cin>>q;
while (q--)
{
cin>>x;
vector<pair<ll, ll>> edg;
for (int i=1; i<=n; i++) dsu[i]=i;
for (int i=1; i<=m; i++) edg.push_back({abs(w[i]-x), i});
sort(edg.begin(), edg.end());
ll res=0, cnt=0;
for (auto [k, idx]:edg)
{
if (find(a[idx])==find(b[idx])) continue;
res+=k;
cnt++;
dsu[find(a[idx])]=find(b[idx]);
if (cnt==n-1) break;
}
cout<<res<<'\n';
}
}
Compilation message
reconstruction.cpp:6: warning: ignoring '#pragma gcc ' [-Wunknown-pragmas]
6 | #pragma gcc-optimize("o3, unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
100 ms |
6440 KB |
Output is correct |
21 |
Correct |
102 ms |
6428 KB |
Output is correct |
22 |
Correct |
98 ms |
6452 KB |
Output is correct |
23 |
Correct |
97 ms |
6420 KB |
Output is correct |
24 |
Correct |
97 ms |
6412 KB |
Output is correct |
25 |
Correct |
99 ms |
6432 KB |
Output is correct |
26 |
Correct |
103 ms |
6440 KB |
Output is correct |
27 |
Correct |
99 ms |
6428 KB |
Output is correct |
28 |
Correct |
113 ms |
6420 KB |
Output is correct |
29 |
Correct |
105 ms |
6440 KB |
Output is correct |
30 |
Correct |
95 ms |
6424 KB |
Output is correct |
31 |
Correct |
95 ms |
6424 KB |
Output is correct |
32 |
Correct |
96 ms |
6432 KB |
Output is correct |
33 |
Correct |
96 ms |
6436 KB |
Output is correct |
34 |
Correct |
59 ms |
6428 KB |
Output is correct |
35 |
Correct |
103 ms |
6440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
6432 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Execution timed out |
5086 ms |
4792 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
100 ms |
6440 KB |
Output is correct |
21 |
Correct |
102 ms |
6428 KB |
Output is correct |
22 |
Correct |
98 ms |
6452 KB |
Output is correct |
23 |
Correct |
97 ms |
6420 KB |
Output is correct |
24 |
Correct |
97 ms |
6412 KB |
Output is correct |
25 |
Correct |
99 ms |
6432 KB |
Output is correct |
26 |
Correct |
103 ms |
6440 KB |
Output is correct |
27 |
Correct |
99 ms |
6428 KB |
Output is correct |
28 |
Correct |
113 ms |
6420 KB |
Output is correct |
29 |
Correct |
105 ms |
6440 KB |
Output is correct |
30 |
Correct |
95 ms |
6424 KB |
Output is correct |
31 |
Correct |
95 ms |
6424 KB |
Output is correct |
32 |
Correct |
96 ms |
6432 KB |
Output is correct |
33 |
Correct |
96 ms |
6436 KB |
Output is correct |
34 |
Correct |
59 ms |
6428 KB |
Output is correct |
35 |
Correct |
103 ms |
6440 KB |
Output is correct |
36 |
Execution timed out |
5055 ms |
6444 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
100 ms |
6440 KB |
Output is correct |
21 |
Correct |
102 ms |
6428 KB |
Output is correct |
22 |
Correct |
98 ms |
6452 KB |
Output is correct |
23 |
Correct |
97 ms |
6420 KB |
Output is correct |
24 |
Correct |
97 ms |
6412 KB |
Output is correct |
25 |
Correct |
99 ms |
6432 KB |
Output is correct |
26 |
Correct |
103 ms |
6440 KB |
Output is correct |
27 |
Correct |
99 ms |
6428 KB |
Output is correct |
28 |
Correct |
113 ms |
6420 KB |
Output is correct |
29 |
Correct |
105 ms |
6440 KB |
Output is correct |
30 |
Correct |
95 ms |
6424 KB |
Output is correct |
31 |
Correct |
95 ms |
6424 KB |
Output is correct |
32 |
Correct |
96 ms |
6432 KB |
Output is correct |
33 |
Correct |
96 ms |
6436 KB |
Output is correct |
34 |
Correct |
59 ms |
6428 KB |
Output is correct |
35 |
Correct |
103 ms |
6440 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Execution timed out |
5092 ms |
6432 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |