#include <bits/stdc++.h>
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define m_p make_pair
#define fast_prep ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=501;
const ll inf=1e9+10;
int p[N],sz[N];
void make(int v){
p[v]=v;sz[v]=1;
}
int get(int v){
return p[v]=(p[v]==v?v:get(p[v]));
}
bool mg(int a,int b){
a=get(a),b=get(b);
if(a==b) return 0;
if(sz[a]<sz[b]) swap(a,b);
p[b]=a;sz[a]+=sz[b];
return 1;
}
signed main(){
fast_prep;
int n,m;
cin>>n>>m;
vec<array<int,3>> arr;
for(int i=0;i<m;i++){
int v,u,w;
cin>>v>>u>>w;--v;--u;
arr.pb({w,v,u});
}
sort(all(arr));
vec<array<ll,3>> change;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++) make(j);
int j=i+1;
while(j<m){
mg(arr[j][1],arr[j][2]);
if(get(arr[i][1])==get(arr[i][2])) break;
++j;
}
ll x=(j==m?inf+2:(arr[j][0]+arr[i][0]+1)/2);
///
change.pb({x,-1,arr[i][0]});
for(int j=0;j<n;j++) make(j);
j=i-1;
while(j>=0){
mg(arr[j][1],arr[j][2]);
if(get(arr[i][1])==get(arr[i][2])) break;
--j;
}
x=(j==-1?-inf:(arr[j][0]+arr[i][0]+1)/2);
change.pb({x,-1,arr[i][0]});
change.pb({arr[i][0],2,-2ll*arr[i][0]});
}
sort(all(change));
int q;
cin>>q;
ll a=0,b=0;
int j=0;
for(int i=0;i<q;i++){
int x;
cin>>x;
while(j<sz(change) && x>=change[j][0]){
a+=change[j][1];b+=change[j][2];
++j;
}
cout<<1ll*a*x+b<<'\n';
}
return 0;
}
/*
5 10
1 2 8
1 3 13
1 4 5
1 5 11
1 5 3
2 3 7
2 4 15
3 4 6
3 5 6
4 5 2
1
10
17
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
2474 ms |
13980 KB |
Output is correct |
21 |
Correct |
2087 ms |
13940 KB |
Output is correct |
22 |
Correct |
2135 ms |
14144 KB |
Output is correct |
23 |
Correct |
2272 ms |
14084 KB |
Output is correct |
24 |
Correct |
2464 ms |
13932 KB |
Output is correct |
25 |
Correct |
2440 ms |
14040 KB |
Output is correct |
26 |
Correct |
2464 ms |
13956 KB |
Output is correct |
27 |
Correct |
2444 ms |
13908 KB |
Output is correct |
28 |
Correct |
2346 ms |
13908 KB |
Output is correct |
29 |
Correct |
1824 ms |
13996 KB |
Output is correct |
30 |
Correct |
2359 ms |
14080 KB |
Output is correct |
31 |
Correct |
2343 ms |
14100 KB |
Output is correct |
32 |
Correct |
2350 ms |
13900 KB |
Output is correct |
33 |
Correct |
2348 ms |
13864 KB |
Output is correct |
34 |
Correct |
1301 ms |
14028 KB |
Output is correct |
35 |
Correct |
2346 ms |
13988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
2351 ms |
19840 KB |
Output is correct |
5 |
Correct |
2356 ms |
19728 KB |
Output is correct |
6 |
Correct |
2397 ms |
19784 KB |
Output is correct |
7 |
Correct |
1343 ms |
21640 KB |
Output is correct |
8 |
Correct |
1097 ms |
22008 KB |
Output is correct |
9 |
Correct |
1027 ms |
21780 KB |
Output is correct |
10 |
Correct |
2356 ms |
19836 KB |
Output is correct |
11 |
Correct |
1068 ms |
21708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
196 ms |
13680 KB |
Output is correct |
21 |
Correct |
193 ms |
12840 KB |
Output is correct |
22 |
Correct |
189 ms |
12728 KB |
Output is correct |
23 |
Correct |
189 ms |
12752 KB |
Output is correct |
24 |
Correct |
196 ms |
12732 KB |
Output is correct |
25 |
Correct |
188 ms |
12696 KB |
Output is correct |
26 |
Correct |
191 ms |
12684 KB |
Output is correct |
27 |
Correct |
203 ms |
12792 KB |
Output is correct |
28 |
Correct |
193 ms |
12756 KB |
Output is correct |
29 |
Correct |
187 ms |
12716 KB |
Output is correct |
30 |
Correct |
199 ms |
13580 KB |
Output is correct |
31 |
Correct |
193 ms |
12660 KB |
Output is correct |
32 |
Correct |
190 ms |
13300 KB |
Output is correct |
33 |
Correct |
190 ms |
12456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
2474 ms |
13980 KB |
Output is correct |
21 |
Correct |
2087 ms |
13940 KB |
Output is correct |
22 |
Correct |
2135 ms |
14144 KB |
Output is correct |
23 |
Correct |
2272 ms |
14084 KB |
Output is correct |
24 |
Correct |
2464 ms |
13932 KB |
Output is correct |
25 |
Correct |
2440 ms |
14040 KB |
Output is correct |
26 |
Correct |
2464 ms |
13956 KB |
Output is correct |
27 |
Correct |
2444 ms |
13908 KB |
Output is correct |
28 |
Correct |
2346 ms |
13908 KB |
Output is correct |
29 |
Correct |
1824 ms |
13996 KB |
Output is correct |
30 |
Correct |
2359 ms |
14080 KB |
Output is correct |
31 |
Correct |
2343 ms |
14100 KB |
Output is correct |
32 |
Correct |
2350 ms |
13900 KB |
Output is correct |
33 |
Correct |
2348 ms |
13864 KB |
Output is correct |
34 |
Correct |
1301 ms |
14028 KB |
Output is correct |
35 |
Correct |
2346 ms |
13988 KB |
Output is correct |
36 |
Correct |
2379 ms |
14132 KB |
Output is correct |
37 |
Correct |
1976 ms |
13888 KB |
Output is correct |
38 |
Correct |
2075 ms |
13832 KB |
Output is correct |
39 |
Correct |
2209 ms |
13868 KB |
Output is correct |
40 |
Correct |
2389 ms |
13872 KB |
Output is correct |
41 |
Correct |
2380 ms |
13960 KB |
Output is correct |
42 |
Correct |
2383 ms |
14032 KB |
Output is correct |
43 |
Correct |
2378 ms |
13996 KB |
Output is correct |
44 |
Correct |
2370 ms |
14192 KB |
Output is correct |
45 |
Correct |
1848 ms |
13896 KB |
Output is correct |
46 |
Correct |
2387 ms |
13888 KB |
Output is correct |
47 |
Correct |
2367 ms |
14180 KB |
Output is correct |
48 |
Correct |
2362 ms |
13900 KB |
Output is correct |
49 |
Correct |
2382 ms |
14108 KB |
Output is correct |
50 |
Correct |
1307 ms |
14168 KB |
Output is correct |
51 |
Correct |
2368 ms |
13928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
2474 ms |
13980 KB |
Output is correct |
21 |
Correct |
2087 ms |
13940 KB |
Output is correct |
22 |
Correct |
2135 ms |
14144 KB |
Output is correct |
23 |
Correct |
2272 ms |
14084 KB |
Output is correct |
24 |
Correct |
2464 ms |
13932 KB |
Output is correct |
25 |
Correct |
2440 ms |
14040 KB |
Output is correct |
26 |
Correct |
2464 ms |
13956 KB |
Output is correct |
27 |
Correct |
2444 ms |
13908 KB |
Output is correct |
28 |
Correct |
2346 ms |
13908 KB |
Output is correct |
29 |
Correct |
1824 ms |
13996 KB |
Output is correct |
30 |
Correct |
2359 ms |
14080 KB |
Output is correct |
31 |
Correct |
2343 ms |
14100 KB |
Output is correct |
32 |
Correct |
2350 ms |
13900 KB |
Output is correct |
33 |
Correct |
2348 ms |
13864 KB |
Output is correct |
34 |
Correct |
1301 ms |
14028 KB |
Output is correct |
35 |
Correct |
2346 ms |
13988 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
2351 ms |
19840 KB |
Output is correct |
40 |
Correct |
2356 ms |
19728 KB |
Output is correct |
41 |
Correct |
2397 ms |
19784 KB |
Output is correct |
42 |
Correct |
1343 ms |
21640 KB |
Output is correct |
43 |
Correct |
1097 ms |
22008 KB |
Output is correct |
44 |
Correct |
1027 ms |
21780 KB |
Output is correct |
45 |
Correct |
2356 ms |
19836 KB |
Output is correct |
46 |
Correct |
1068 ms |
21708 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
196 ms |
13680 KB |
Output is correct |
49 |
Correct |
193 ms |
12840 KB |
Output is correct |
50 |
Correct |
189 ms |
12728 KB |
Output is correct |
51 |
Correct |
189 ms |
12752 KB |
Output is correct |
52 |
Correct |
196 ms |
12732 KB |
Output is correct |
53 |
Correct |
188 ms |
12696 KB |
Output is correct |
54 |
Correct |
191 ms |
12684 KB |
Output is correct |
55 |
Correct |
203 ms |
12792 KB |
Output is correct |
56 |
Correct |
193 ms |
12756 KB |
Output is correct |
57 |
Correct |
187 ms |
12716 KB |
Output is correct |
58 |
Correct |
199 ms |
13580 KB |
Output is correct |
59 |
Correct |
193 ms |
12660 KB |
Output is correct |
60 |
Correct |
190 ms |
13300 KB |
Output is correct |
61 |
Correct |
190 ms |
12456 KB |
Output is correct |
62 |
Correct |
2379 ms |
14132 KB |
Output is correct |
63 |
Correct |
1976 ms |
13888 KB |
Output is correct |
64 |
Correct |
2075 ms |
13832 KB |
Output is correct |
65 |
Correct |
2209 ms |
13868 KB |
Output is correct |
66 |
Correct |
2389 ms |
13872 KB |
Output is correct |
67 |
Correct |
2380 ms |
13960 KB |
Output is correct |
68 |
Correct |
2383 ms |
14032 KB |
Output is correct |
69 |
Correct |
2378 ms |
13996 KB |
Output is correct |
70 |
Correct |
2370 ms |
14192 KB |
Output is correct |
71 |
Correct |
1848 ms |
13896 KB |
Output is correct |
72 |
Correct |
2387 ms |
13888 KB |
Output is correct |
73 |
Correct |
2367 ms |
14180 KB |
Output is correct |
74 |
Correct |
2362 ms |
13900 KB |
Output is correct |
75 |
Correct |
2382 ms |
14108 KB |
Output is correct |
76 |
Correct |
1307 ms |
14168 KB |
Output is correct |
77 |
Correct |
2368 ms |
13928 KB |
Output is correct |
78 |
Correct |
2536 ms |
18644 KB |
Output is correct |
79 |
Correct |
2125 ms |
20624 KB |
Output is correct |
80 |
Correct |
2237 ms |
19660 KB |
Output is correct |
81 |
Correct |
2369 ms |
19768 KB |
Output is correct |
82 |
Correct |
2501 ms |
18912 KB |
Output is correct |
83 |
Correct |
2502 ms |
18660 KB |
Output is correct |
84 |
Correct |
2535 ms |
18632 KB |
Output is correct |
85 |
Correct |
2497 ms |
18692 KB |
Output is correct |
86 |
Correct |
2514 ms |
18732 KB |
Output is correct |
87 |
Correct |
1979 ms |
20240 KB |
Output is correct |
88 |
Correct |
2537 ms |
18888 KB |
Output is correct |
89 |
Correct |
2535 ms |
19020 KB |
Output is correct |
90 |
Correct |
2553 ms |
18712 KB |
Output is correct |
91 |
Correct |
2506 ms |
18720 KB |
Output is correct |
92 |
Correct |
1497 ms |
21448 KB |
Output is correct |
93 |
Correct |
2532 ms |
19676 KB |
Output is correct |