#include<bits/stdc++.h>
using namespace std;
#define int long long
struct DSURollback{
vector<int> lab;
int cc;
void init(int n){
lab.assign(n+1, -1);
cc=n;
}
int find_set(int u){
return lab[u]<0?u:lab[u]=find_set(lab[u]);
}
bool union_sets(int u, int v){
u=find_set(u); v=find_set(v);
if (u!=v){
if (lab[u]>lab[v]) swap(u, v);
lab[u]+=lab[v];
lab[v]=u;
--cc;
return 1;
}
return 0;
}
bool same(int u, int v){
return find_set(u)==find_set(v);
}
} dsu;
struct Edge{
int u, v, w;
Edge (int _u=0, int _v=0, int _w=0){
u=_u, v=_v, w=_w;
}
bool operator<(const Edge &b){
return make_tuple(w, u, v)<make_tuple(b.w, b.u, b.v);
}
};
const int N=510, M=1e5+10, Q=1e6+10;
int n, m, q, nxt[M];
Edge a[M];
int b[Q];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i=1; i<=m; ++i) cin >> a[i].u >> a[i].v >> a[i].w;
dsu.init(n);
sort(a+1, a+m+1);
cin >> q;
for (int i=1; i<=q; ++i) cin >> b[i];
vector<pair<int, pair<int, int>>> v;
for (int i=1; i<=m; ++i){
dsu.init(n);
int j;
for (j=i+1; j<=m; ++j){
dsu.union_sets(a[j].u, a[j].v);
if (dsu.same(a[i].u, a[i].v)) break;
}
int rval=j==m+1?((int)1e9)+1:a[i].w+(a[j].w-a[i].w+1)/2;
dsu.init(n);
for (j=i-1; j>=1; --j){
dsu.union_sets(a[j].u, a[j].v);
if (dsu.same(a[i].u, a[i].v)) break;
}
int lval=j==0?-1:a[i].w-(a[i].w-a[j].w)/2;
v.push_back({lval, {i, 1}});
v.push_back({a[i].w, {i, 2}});
v.push_back({rval, {i, 3}});
}
sort(v.begin(), v.end());
int add=0, mul=0;
for (int i=1, j=0; i<=q; ++i){
while (j<(int)v.size() && v[j].first<=b[i]){
if (v[j].second.second==1){
add+=a[v[j].second.first].w;
mul-=1;
}else if (v[j].second.second==2){
add-=a[v[j].second.first].w*2;
mul+=2;
}else{
add+=a[v[j].second.first].w;
mul-=1;
}
++j;
}
cout << add+mul*b[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4544 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4576 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4544 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4576 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1123 ms |
19280 KB |
Output is correct |
21 |
Correct |
816 ms |
19664 KB |
Output is correct |
22 |
Correct |
867 ms |
20660 KB |
Output is correct |
23 |
Correct |
1020 ms |
19160 KB |
Output is correct |
24 |
Correct |
1170 ms |
18984 KB |
Output is correct |
25 |
Correct |
1113 ms |
20360 KB |
Output is correct |
26 |
Correct |
1162 ms |
18932 KB |
Output is correct |
27 |
Correct |
1137 ms |
19356 KB |
Output is correct |
28 |
Correct |
1111 ms |
20748 KB |
Output is correct |
29 |
Correct |
605 ms |
18864 KB |
Output is correct |
30 |
Correct |
1109 ms |
20316 KB |
Output is correct |
31 |
Correct |
1138 ms |
20268 KB |
Output is correct |
32 |
Correct |
1154 ms |
20876 KB |
Output is correct |
33 |
Correct |
1144 ms |
20184 KB |
Output is correct |
34 |
Correct |
435 ms |
20856 KB |
Output is correct |
35 |
Correct |
1171 ms |
18620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4452 KB |
Output is correct |
4 |
Correct |
1115 ms |
38872 KB |
Output is correct |
5 |
Correct |
1102 ms |
38240 KB |
Output is correct |
6 |
Correct |
1139 ms |
38284 KB |
Output is correct |
7 |
Correct |
673 ms |
39892 KB |
Output is correct |
8 |
Correct |
526 ms |
40184 KB |
Output is correct |
9 |
Correct |
423 ms |
40108 KB |
Output is correct |
10 |
Correct |
1106 ms |
38996 KB |
Output is correct |
11 |
Correct |
519 ms |
40244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4544 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4576 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4572 KB |
Output is correct |
20 |
Correct |
155 ms |
32776 KB |
Output is correct |
21 |
Correct |
149 ms |
32956 KB |
Output is correct |
22 |
Correct |
152 ms |
32852 KB |
Output is correct |
23 |
Correct |
156 ms |
32932 KB |
Output is correct |
24 |
Correct |
167 ms |
32868 KB |
Output is correct |
25 |
Correct |
144 ms |
32888 KB |
Output is correct |
26 |
Correct |
158 ms |
32616 KB |
Output is correct |
27 |
Correct |
192 ms |
32884 KB |
Output is correct |
28 |
Correct |
142 ms |
32896 KB |
Output is correct |
29 |
Correct |
143 ms |
32776 KB |
Output is correct |
30 |
Correct |
152 ms |
32904 KB |
Output is correct |
31 |
Correct |
152 ms |
32664 KB |
Output is correct |
32 |
Correct |
158 ms |
33580 KB |
Output is correct |
33 |
Correct |
161 ms |
32888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4544 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4576 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1123 ms |
19280 KB |
Output is correct |
21 |
Correct |
816 ms |
19664 KB |
Output is correct |
22 |
Correct |
867 ms |
20660 KB |
Output is correct |
23 |
Correct |
1020 ms |
19160 KB |
Output is correct |
24 |
Correct |
1170 ms |
18984 KB |
Output is correct |
25 |
Correct |
1113 ms |
20360 KB |
Output is correct |
26 |
Correct |
1162 ms |
18932 KB |
Output is correct |
27 |
Correct |
1137 ms |
19356 KB |
Output is correct |
28 |
Correct |
1111 ms |
20748 KB |
Output is correct |
29 |
Correct |
605 ms |
18864 KB |
Output is correct |
30 |
Correct |
1109 ms |
20316 KB |
Output is correct |
31 |
Correct |
1138 ms |
20268 KB |
Output is correct |
32 |
Correct |
1154 ms |
20876 KB |
Output is correct |
33 |
Correct |
1144 ms |
20184 KB |
Output is correct |
34 |
Correct |
435 ms |
20856 KB |
Output is correct |
35 |
Correct |
1171 ms |
18620 KB |
Output is correct |
36 |
Correct |
1179 ms |
20708 KB |
Output is correct |
37 |
Correct |
862 ms |
19084 KB |
Output is correct |
38 |
Correct |
910 ms |
20020 KB |
Output is correct |
39 |
Correct |
990 ms |
20284 KB |
Output is correct |
40 |
Correct |
1142 ms |
20288 KB |
Output is correct |
41 |
Correct |
1169 ms |
19988 KB |
Output is correct |
42 |
Correct |
1139 ms |
19888 KB |
Output is correct |
43 |
Correct |
1132 ms |
20036 KB |
Output is correct |
44 |
Correct |
1153 ms |
20516 KB |
Output is correct |
45 |
Correct |
634 ms |
20488 KB |
Output is correct |
46 |
Correct |
1198 ms |
20560 KB |
Output is correct |
47 |
Correct |
1182 ms |
19148 KB |
Output is correct |
48 |
Correct |
1124 ms |
20284 KB |
Output is correct |
49 |
Correct |
1171 ms |
20712 KB |
Output is correct |
50 |
Correct |
464 ms |
19112 KB |
Output is correct |
51 |
Correct |
1200 ms |
20656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4544 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4576 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1123 ms |
19280 KB |
Output is correct |
21 |
Correct |
816 ms |
19664 KB |
Output is correct |
22 |
Correct |
867 ms |
20660 KB |
Output is correct |
23 |
Correct |
1020 ms |
19160 KB |
Output is correct |
24 |
Correct |
1170 ms |
18984 KB |
Output is correct |
25 |
Correct |
1113 ms |
20360 KB |
Output is correct |
26 |
Correct |
1162 ms |
18932 KB |
Output is correct |
27 |
Correct |
1137 ms |
19356 KB |
Output is correct |
28 |
Correct |
1111 ms |
20748 KB |
Output is correct |
29 |
Correct |
605 ms |
18864 KB |
Output is correct |
30 |
Correct |
1109 ms |
20316 KB |
Output is correct |
31 |
Correct |
1138 ms |
20268 KB |
Output is correct |
32 |
Correct |
1154 ms |
20876 KB |
Output is correct |
33 |
Correct |
1144 ms |
20184 KB |
Output is correct |
34 |
Correct |
435 ms |
20856 KB |
Output is correct |
35 |
Correct |
1171 ms |
18620 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4452 KB |
Output is correct |
39 |
Correct |
1115 ms |
38872 KB |
Output is correct |
40 |
Correct |
1102 ms |
38240 KB |
Output is correct |
41 |
Correct |
1139 ms |
38284 KB |
Output is correct |
42 |
Correct |
673 ms |
39892 KB |
Output is correct |
43 |
Correct |
526 ms |
40184 KB |
Output is correct |
44 |
Correct |
423 ms |
40108 KB |
Output is correct |
45 |
Correct |
1106 ms |
38996 KB |
Output is correct |
46 |
Correct |
519 ms |
40244 KB |
Output is correct |
47 |
Correct |
1 ms |
4572 KB |
Output is correct |
48 |
Correct |
155 ms |
32776 KB |
Output is correct |
49 |
Correct |
149 ms |
32956 KB |
Output is correct |
50 |
Correct |
152 ms |
32852 KB |
Output is correct |
51 |
Correct |
156 ms |
32932 KB |
Output is correct |
52 |
Correct |
167 ms |
32868 KB |
Output is correct |
53 |
Correct |
144 ms |
32888 KB |
Output is correct |
54 |
Correct |
158 ms |
32616 KB |
Output is correct |
55 |
Correct |
192 ms |
32884 KB |
Output is correct |
56 |
Correct |
142 ms |
32896 KB |
Output is correct |
57 |
Correct |
143 ms |
32776 KB |
Output is correct |
58 |
Correct |
152 ms |
32904 KB |
Output is correct |
59 |
Correct |
152 ms |
32664 KB |
Output is correct |
60 |
Correct |
158 ms |
33580 KB |
Output is correct |
61 |
Correct |
161 ms |
32888 KB |
Output is correct |
62 |
Correct |
1179 ms |
20708 KB |
Output is correct |
63 |
Correct |
862 ms |
19084 KB |
Output is correct |
64 |
Correct |
910 ms |
20020 KB |
Output is correct |
65 |
Correct |
990 ms |
20284 KB |
Output is correct |
66 |
Correct |
1142 ms |
20288 KB |
Output is correct |
67 |
Correct |
1169 ms |
19988 KB |
Output is correct |
68 |
Correct |
1139 ms |
19888 KB |
Output is correct |
69 |
Correct |
1132 ms |
20036 KB |
Output is correct |
70 |
Correct |
1153 ms |
20516 KB |
Output is correct |
71 |
Correct |
634 ms |
20488 KB |
Output is correct |
72 |
Correct |
1198 ms |
20560 KB |
Output is correct |
73 |
Correct |
1182 ms |
19148 KB |
Output is correct |
74 |
Correct |
1124 ms |
20284 KB |
Output is correct |
75 |
Correct |
1171 ms |
20712 KB |
Output is correct |
76 |
Correct |
464 ms |
19112 KB |
Output is correct |
77 |
Correct |
1200 ms |
20656 KB |
Output is correct |
78 |
Correct |
1295 ms |
39396 KB |
Output is correct |
79 |
Correct |
1012 ms |
43232 KB |
Output is correct |
80 |
Correct |
1050 ms |
41468 KB |
Output is correct |
81 |
Correct |
1133 ms |
40528 KB |
Output is correct |
82 |
Correct |
1302 ms |
40636 KB |
Output is correct |
83 |
Correct |
1281 ms |
40728 KB |
Output is correct |
84 |
Correct |
1316 ms |
39360 KB |
Output is correct |
85 |
Correct |
1267 ms |
38064 KB |
Output is correct |
86 |
Correct |
1248 ms |
39488 KB |
Output is correct |
87 |
Correct |
775 ms |
38976 KB |
Output is correct |
88 |
Correct |
1266 ms |
39508 KB |
Output is correct |
89 |
Correct |
1293 ms |
39472 KB |
Output is correct |
90 |
Correct |
1293 ms |
39532 KB |
Output is correct |
91 |
Correct |
1293 ms |
39116 KB |
Output is correct |
92 |
Correct |
568 ms |
42072 KB |
Output is correct |
93 |
Correct |
1292 ms |
40332 KB |
Output is correct |