#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3xxEYjy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
struct dsu{
int n,cmps;
vi par,siz;
dsu(int m=0){init(m);}
void init(int m){
n=m,cmps=m;
par=siz=vi(n,0);
rep(i,n){
par[i]=i;
siz[i]=1;
}
}
void merge(int v,int u){
v=parent(v),u=parent(u);
if(v==u)return;
cmps--;
if(siz[v]<siz[u])swap(v,u);
siz[v]+=siz[u];
par[u]=v;
}
int parent(int v){
return par[v]==v?v:parent(par[v]);
}
bool same(int v, int u){
return parent(v)==parent(u);
}
int size(int v=-1){
return (v==-1?cmps:siz[parent(v)]);
}
};
signed main(){
_3xxEYjy;
int n,m;
cin>>n>>m;
using tpii=pair<int,pii>;
vec(tpii) edges;
rep(i,m){
int u,v,w;
cin>>u>>v>>w;
u-=1,v-=1;
edges.pb({w,{u,v}});
}
sort(edges.begin(),edges.end());
const int inf=1e10;
vec(tpii) evts;
rep(i,m){
dsu uf(n);
int r=inf;
rng(j,i+1,m){
uf.merge(edges[j].se.fi,edges[j].se.se);
if(uf.same(edges[i].se.fi,edges[i].se.se)){
r=edges[j].fi;
break;
}
}
uf.init(n);
int l=-inf;
per(j,i){
uf.merge(edges[j].se.fi,edges[j].se.se);
if(uf.same(edges[i].se.fi,edges[i].se.se)){
l=edges[j].fi;
break;
}
}
int w=edges[i].fi;
evts.pb({l==-inf?-1:(l+w)/2+(l+w)%2,{-1,+w}});
evts.pb({w,{+2,-2ll*w}});
evts.pb({r==inf?inf:(r+w)/2+(r+w)%2,{-1,+w}});
}
sort(evts.begin(),evts.end(),[&](tpii l,tpii r){
if(l.fi!=r.fi){
return l.fi<r.fi;
}else{
return l.se.fi<r.se.fi;
}
});
int q;
cin>>q;
int i=-1;
pii p={0,0};
rep(_,q){
int x;
cin>>x;
while(i+1<sz(evts) and evts[i+1].fi<=x){
i+=1;
// print(evts[i].fi,evts[i].se.fi,evts[i].se.se);
p.fi+=evts[i].se.fi;
p.se+=evts[i].se.se;
}
// print(p.fi,p.se);
cout<<p.fi*x+p.se<<"\n";
// break;
}
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 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 |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1964 ms |
15216 KB |
Output is correct |
21 |
Correct |
1253 ms |
15148 KB |
Output is correct |
22 |
Correct |
1369 ms |
15124 KB |
Output is correct |
23 |
Correct |
1545 ms |
15128 KB |
Output is correct |
24 |
Correct |
1871 ms |
15172 KB |
Output is correct |
25 |
Correct |
1981 ms |
15272 KB |
Output is correct |
26 |
Correct |
1942 ms |
15136 KB |
Output is correct |
27 |
Correct |
1956 ms |
15128 KB |
Output is correct |
28 |
Correct |
1915 ms |
15188 KB |
Output is correct |
29 |
Correct |
843 ms |
15180 KB |
Output is correct |
30 |
Correct |
1943 ms |
15108 KB |
Output is correct |
31 |
Correct |
1927 ms |
15040 KB |
Output is correct |
32 |
Correct |
1939 ms |
15080 KB |
Output is correct |
33 |
Correct |
1929 ms |
15152 KB |
Output is correct |
34 |
Correct |
622 ms |
15068 KB |
Output is correct |
35 |
Correct |
1932 ms |
15124 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 |
1718 ms |
21708 KB |
Output is correct |
5 |
Correct |
1676 ms |
31700 KB |
Output is correct |
6 |
Correct |
1667 ms |
31648 KB |
Output is correct |
7 |
Correct |
839 ms |
33952 KB |
Output is correct |
8 |
Correct |
613 ms |
34040 KB |
Output is correct |
9 |
Correct |
535 ms |
34328 KB |
Output is correct |
10 |
Correct |
1673 ms |
32340 KB |
Output is correct |
11 |
Correct |
584 ms |
34212 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 |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
185 ms |
12748 KB |
Output is correct |
21 |
Correct |
180 ms |
12876 KB |
Output is correct |
22 |
Correct |
179 ms |
12792 KB |
Output is correct |
23 |
Correct |
183 ms |
12928 KB |
Output is correct |
24 |
Correct |
182 ms |
12876 KB |
Output is correct |
25 |
Correct |
189 ms |
12720 KB |
Output is correct |
26 |
Correct |
180 ms |
12708 KB |
Output is correct |
27 |
Correct |
180 ms |
12748 KB |
Output is correct |
28 |
Correct |
180 ms |
12716 KB |
Output is correct |
29 |
Correct |
177 ms |
12712 KB |
Output is correct |
30 |
Correct |
175 ms |
12752 KB |
Output is correct |
31 |
Correct |
176 ms |
12732 KB |
Output is correct |
32 |
Correct |
175 ms |
13392 KB |
Output is correct |
33 |
Correct |
178 ms |
12656 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 |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1964 ms |
15216 KB |
Output is correct |
21 |
Correct |
1253 ms |
15148 KB |
Output is correct |
22 |
Correct |
1369 ms |
15124 KB |
Output is correct |
23 |
Correct |
1545 ms |
15128 KB |
Output is correct |
24 |
Correct |
1871 ms |
15172 KB |
Output is correct |
25 |
Correct |
1981 ms |
15272 KB |
Output is correct |
26 |
Correct |
1942 ms |
15136 KB |
Output is correct |
27 |
Correct |
1956 ms |
15128 KB |
Output is correct |
28 |
Correct |
1915 ms |
15188 KB |
Output is correct |
29 |
Correct |
843 ms |
15180 KB |
Output is correct |
30 |
Correct |
1943 ms |
15108 KB |
Output is correct |
31 |
Correct |
1927 ms |
15040 KB |
Output is correct |
32 |
Correct |
1939 ms |
15080 KB |
Output is correct |
33 |
Correct |
1929 ms |
15152 KB |
Output is correct |
34 |
Correct |
622 ms |
15068 KB |
Output is correct |
35 |
Correct |
1932 ms |
15124 KB |
Output is correct |
36 |
Correct |
1952 ms |
15388 KB |
Output is correct |
37 |
Correct |
1263 ms |
15236 KB |
Output is correct |
38 |
Correct |
1381 ms |
15272 KB |
Output is correct |
39 |
Correct |
1533 ms |
15080 KB |
Output is correct |
40 |
Correct |
1877 ms |
15076 KB |
Output is correct |
41 |
Correct |
1976 ms |
15144 KB |
Output is correct |
42 |
Correct |
1969 ms |
15088 KB |
Output is correct |
43 |
Correct |
1988 ms |
15048 KB |
Output is correct |
44 |
Correct |
1901 ms |
15124 KB |
Output is correct |
45 |
Correct |
829 ms |
15272 KB |
Output is correct |
46 |
Correct |
1957 ms |
15160 KB |
Output is correct |
47 |
Correct |
1932 ms |
15056 KB |
Output is correct |
48 |
Correct |
1940 ms |
15212 KB |
Output is correct |
49 |
Correct |
1955 ms |
15368 KB |
Output is correct |
50 |
Correct |
613 ms |
15164 KB |
Output is correct |
51 |
Correct |
1939 ms |
15416 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 |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1964 ms |
15216 KB |
Output is correct |
21 |
Correct |
1253 ms |
15148 KB |
Output is correct |
22 |
Correct |
1369 ms |
15124 KB |
Output is correct |
23 |
Correct |
1545 ms |
15128 KB |
Output is correct |
24 |
Correct |
1871 ms |
15172 KB |
Output is correct |
25 |
Correct |
1981 ms |
15272 KB |
Output is correct |
26 |
Correct |
1942 ms |
15136 KB |
Output is correct |
27 |
Correct |
1956 ms |
15128 KB |
Output is correct |
28 |
Correct |
1915 ms |
15188 KB |
Output is correct |
29 |
Correct |
843 ms |
15180 KB |
Output is correct |
30 |
Correct |
1943 ms |
15108 KB |
Output is correct |
31 |
Correct |
1927 ms |
15040 KB |
Output is correct |
32 |
Correct |
1939 ms |
15080 KB |
Output is correct |
33 |
Correct |
1929 ms |
15152 KB |
Output is correct |
34 |
Correct |
622 ms |
15068 KB |
Output is correct |
35 |
Correct |
1932 ms |
15124 KB |
Output is correct |
36 |
Correct |
0 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 |
1718 ms |
21708 KB |
Output is correct |
40 |
Correct |
1676 ms |
31700 KB |
Output is correct |
41 |
Correct |
1667 ms |
31648 KB |
Output is correct |
42 |
Correct |
839 ms |
33952 KB |
Output is correct |
43 |
Correct |
613 ms |
34040 KB |
Output is correct |
44 |
Correct |
535 ms |
34328 KB |
Output is correct |
45 |
Correct |
1673 ms |
32340 KB |
Output is correct |
46 |
Correct |
584 ms |
34212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
185 ms |
12748 KB |
Output is correct |
49 |
Correct |
180 ms |
12876 KB |
Output is correct |
50 |
Correct |
179 ms |
12792 KB |
Output is correct |
51 |
Correct |
183 ms |
12928 KB |
Output is correct |
52 |
Correct |
182 ms |
12876 KB |
Output is correct |
53 |
Correct |
189 ms |
12720 KB |
Output is correct |
54 |
Correct |
180 ms |
12708 KB |
Output is correct |
55 |
Correct |
180 ms |
12748 KB |
Output is correct |
56 |
Correct |
180 ms |
12716 KB |
Output is correct |
57 |
Correct |
177 ms |
12712 KB |
Output is correct |
58 |
Correct |
175 ms |
12752 KB |
Output is correct |
59 |
Correct |
176 ms |
12732 KB |
Output is correct |
60 |
Correct |
175 ms |
13392 KB |
Output is correct |
61 |
Correct |
178 ms |
12656 KB |
Output is correct |
62 |
Correct |
1952 ms |
15388 KB |
Output is correct |
63 |
Correct |
1263 ms |
15236 KB |
Output is correct |
64 |
Correct |
1381 ms |
15272 KB |
Output is correct |
65 |
Correct |
1533 ms |
15080 KB |
Output is correct |
66 |
Correct |
1877 ms |
15076 KB |
Output is correct |
67 |
Correct |
1976 ms |
15144 KB |
Output is correct |
68 |
Correct |
1969 ms |
15088 KB |
Output is correct |
69 |
Correct |
1988 ms |
15048 KB |
Output is correct |
70 |
Correct |
1901 ms |
15124 KB |
Output is correct |
71 |
Correct |
829 ms |
15272 KB |
Output is correct |
72 |
Correct |
1957 ms |
15160 KB |
Output is correct |
73 |
Correct |
1932 ms |
15056 KB |
Output is correct |
74 |
Correct |
1940 ms |
15212 KB |
Output is correct |
75 |
Correct |
1955 ms |
15368 KB |
Output is correct |
76 |
Correct |
613 ms |
15164 KB |
Output is correct |
77 |
Correct |
1939 ms |
15416 KB |
Output is correct |
78 |
Correct |
2108 ms |
23312 KB |
Output is correct |
79 |
Correct |
1400 ms |
30840 KB |
Output is correct |
80 |
Correct |
1566 ms |
32236 KB |
Output is correct |
81 |
Correct |
1697 ms |
32100 KB |
Output is correct |
82 |
Correct |
2023 ms |
31228 KB |
Output is correct |
83 |
Correct |
2119 ms |
23808 KB |
Output is correct |
84 |
Correct |
2086 ms |
19864 KB |
Output is correct |
85 |
Correct |
2116 ms |
19996 KB |
Output is correct |
86 |
Correct |
2079 ms |
20120 KB |
Output is correct |
87 |
Correct |
1006 ms |
21588 KB |
Output is correct |
88 |
Correct |
2092 ms |
19924 KB |
Output is correct |
89 |
Correct |
2095 ms |
20196 KB |
Output is correct |
90 |
Correct |
2090 ms |
19944 KB |
Output is correct |
91 |
Correct |
2116 ms |
19956 KB |
Output is correct |
92 |
Correct |
766 ms |
22772 KB |
Output is correct |
93 |
Correct |
2091 ms |
20936 KB |
Output is correct |