#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)
using namespace std;
ll n , m , q;
ll dsu[509];
ll sz[509];
ll e1[100009] , e2[100009] , c[100009];
ll gp(ll u)
{
if(dsu[u]==u)
return u;
return dsu[u]=gp(dsu[u]);
}
void merg(ll u , ll v)
{
u = gp(u);
v = gp(v);
if(sz[v]>sz[u])
swap(u,v);
sz[u]+=sz[v];
dsu[v]=u;
}
ll mst(ll x)
{
for(int i = 1 ; n>=i ; i++)
{
dsu[i]=i;
sz[i]=1;
}
vector<pair<ll,pll>> v;
for(int i = 0 ; m>i ; i++)
v.pb({abs(c[i]-x),{e1[i],e2[i]}});
sort(v.begin(),v.end());
ll ans = 0;
for(auto it : v)
{
if(gp(it.S.F)==gp(it.S.S))
continue;
merg(it.S.F,it.S.S);
ans+=it.F;
}
return ans;
}
int main()
{
d3
cin >> n >> m;
for(int i = 0 ; m>i ; i++)
{
ll u , v , w;
cin >> u >> v >> w;
e1[i]=u;
e2[i]=v;
c[i]=w;
}
cin >> q;
while(q--)
{
ll x;
cin >> x;
ll ans = mst(x);
cans
}
}
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2516 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2516 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 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 |
93 ms |
10772 KB |
Output is correct |
21 |
Correct |
94 ms |
10360 KB |
Output is correct |
22 |
Correct |
104 ms |
9956 KB |
Output is correct |
23 |
Correct |
98 ms |
10216 KB |
Output is correct |
24 |
Correct |
98 ms |
9968 KB |
Output is correct |
25 |
Correct |
97 ms |
9952 KB |
Output is correct |
26 |
Correct |
104 ms |
10040 KB |
Output is correct |
27 |
Correct |
101 ms |
9960 KB |
Output is correct |
28 |
Correct |
119 ms |
9952 KB |
Output is correct |
29 |
Correct |
135 ms |
9960 KB |
Output is correct |
30 |
Correct |
101 ms |
9952 KB |
Output is correct |
31 |
Correct |
98 ms |
10300 KB |
Output is correct |
32 |
Correct |
119 ms |
9960 KB |
Output is correct |
33 |
Correct |
103 ms |
9968 KB |
Output is correct |
34 |
Correct |
121 ms |
10160 KB |
Output is correct |
35 |
Correct |
97 ms |
9936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Execution timed out |
5086 ms |
10700 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2516 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 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 |
5083 ms |
7308 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2516 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 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 |
93 ms |
10772 KB |
Output is correct |
21 |
Correct |
94 ms |
10360 KB |
Output is correct |
22 |
Correct |
104 ms |
9956 KB |
Output is correct |
23 |
Correct |
98 ms |
10216 KB |
Output is correct |
24 |
Correct |
98 ms |
9968 KB |
Output is correct |
25 |
Correct |
97 ms |
9952 KB |
Output is correct |
26 |
Correct |
104 ms |
10040 KB |
Output is correct |
27 |
Correct |
101 ms |
9960 KB |
Output is correct |
28 |
Correct |
119 ms |
9952 KB |
Output is correct |
29 |
Correct |
135 ms |
9960 KB |
Output is correct |
30 |
Correct |
101 ms |
9952 KB |
Output is correct |
31 |
Correct |
98 ms |
10300 KB |
Output is correct |
32 |
Correct |
119 ms |
9960 KB |
Output is correct |
33 |
Correct |
103 ms |
9968 KB |
Output is correct |
34 |
Correct |
121 ms |
10160 KB |
Output is correct |
35 |
Correct |
97 ms |
9936 KB |
Output is correct |
36 |
Execution timed out |
5050 ms |
10228 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2516 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 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 |
93 ms |
10772 KB |
Output is correct |
21 |
Correct |
94 ms |
10360 KB |
Output is correct |
22 |
Correct |
104 ms |
9956 KB |
Output is correct |
23 |
Correct |
98 ms |
10216 KB |
Output is correct |
24 |
Correct |
98 ms |
9968 KB |
Output is correct |
25 |
Correct |
97 ms |
9952 KB |
Output is correct |
26 |
Correct |
104 ms |
10040 KB |
Output is correct |
27 |
Correct |
101 ms |
9960 KB |
Output is correct |
28 |
Correct |
119 ms |
9952 KB |
Output is correct |
29 |
Correct |
135 ms |
9960 KB |
Output is correct |
30 |
Correct |
101 ms |
9952 KB |
Output is correct |
31 |
Correct |
98 ms |
10300 KB |
Output is correct |
32 |
Correct |
119 ms |
9960 KB |
Output is correct |
33 |
Correct |
103 ms |
9968 KB |
Output is correct |
34 |
Correct |
121 ms |
10160 KB |
Output is correct |
35 |
Correct |
97 ms |
9936 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Execution timed out |
5086 ms |
10700 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |