#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int mmax = 1e5 + 5;
vector<tii> scand[mmax];
struct DSU {
vector<int> dsu;
ll cost;
void init(int n) {
dsu.assign(n, 0);
cost = 0;
iota(all(dsu), 0);
}
int f(int x) { return dsu[x] == x? x : dsu[x] = f(dsu[x]); }
bool unite(int w, int x, int y) {
x = f(x);
y = f(y);
if(x == y) return 0;
dsu[x] = y;
cost += w;
return 1;
}
bool unite(tii a) {
return unite(get<0>(a), get<1>(a), get<2>(a));
}
};
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, m;
cin >> n >> m;
map<int, int> fpozc;
vector<tii> edges(m);
for(auto &[w, x, y] : edges) cin >> x >> y >> w, --x, --y;
sort(all(edges));
auto addTo = [&](tii a, vector<tii>& nv, vector<tii> R) {
R.insert(R.begin(), a);
DSU d;
d.init(n);
nv.clear();
for(auto t : R)
if(d.unite(t)) nv.emplace_back(t);
};
for(int i = m - 1; i >= 0; i--) {
addTo(edges[i], scand[i], scand[i + 1]);
}
vector<tii> pref;
int q, p = 0;
cin >> q;
for(int i = 0; i < q; i++) {
int X;
cin >> X;
while(p < m && get<0>(edges[p]) <= X)
addTo(edges[p], pref, pref),
p++;
vector<tii> L, R, mine;
for(auto [w, x, y] : pref) L.emplace_back(X - w, x, y);
for(auto [w, x, y] : scand[p]) R.emplace_back(w - X, x, y);
merge(all(L), all(R), back_inserter(mine));
DSU d;
d.init(n);
for(auto x : mine)
d.unite(x);
cout << d.cost << '\n';
}
}
/**
Istenem! Nu poate fi real.
-- Surse verificate
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2700 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2700 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1664 ms |
608060 KB |
Output is correct |
21 |
Correct |
1029 ms |
607836 KB |
Output is correct |
22 |
Correct |
1103 ms |
607828 KB |
Output is correct |
23 |
Correct |
1220 ms |
607804 KB |
Output is correct |
24 |
Correct |
1299 ms |
607460 KB |
Output is correct |
25 |
Correct |
1775 ms |
608016 KB |
Output is correct |
26 |
Correct |
1613 ms |
607904 KB |
Output is correct |
27 |
Correct |
1590 ms |
607828 KB |
Output is correct |
28 |
Correct |
1604 ms |
607812 KB |
Output is correct |
29 |
Correct |
1588 ms |
599324 KB |
Output is correct |
30 |
Correct |
1473 ms |
607880 KB |
Output is correct |
31 |
Correct |
1527 ms |
607828 KB |
Output is correct |
32 |
Correct |
1602 ms |
608084 KB |
Output is correct |
33 |
Correct |
1255 ms |
608084 KB |
Output is correct |
34 |
Correct |
757 ms |
518664 KB |
Output is correct |
35 |
Correct |
1298 ms |
607824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Execution timed out |
5116 ms |
614260 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2700 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Execution timed out |
5089 ms |
14688 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2700 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1664 ms |
608060 KB |
Output is correct |
21 |
Correct |
1029 ms |
607836 KB |
Output is correct |
22 |
Correct |
1103 ms |
607828 KB |
Output is correct |
23 |
Correct |
1220 ms |
607804 KB |
Output is correct |
24 |
Correct |
1299 ms |
607460 KB |
Output is correct |
25 |
Correct |
1775 ms |
608016 KB |
Output is correct |
26 |
Correct |
1613 ms |
607904 KB |
Output is correct |
27 |
Correct |
1590 ms |
607828 KB |
Output is correct |
28 |
Correct |
1604 ms |
607812 KB |
Output is correct |
29 |
Correct |
1588 ms |
599324 KB |
Output is correct |
30 |
Correct |
1473 ms |
607880 KB |
Output is correct |
31 |
Correct |
1527 ms |
607828 KB |
Output is correct |
32 |
Correct |
1602 ms |
608084 KB |
Output is correct |
33 |
Correct |
1255 ms |
608084 KB |
Output is correct |
34 |
Correct |
757 ms |
518664 KB |
Output is correct |
35 |
Correct |
1298 ms |
607824 KB |
Output is correct |
36 |
Correct |
2212 ms |
608260 KB |
Output is correct |
37 |
Correct |
1518 ms |
608336 KB |
Output is correct |
38 |
Correct |
1571 ms |
608144 KB |
Output is correct |
39 |
Correct |
1736 ms |
607824 KB |
Output is correct |
40 |
Correct |
1932 ms |
608240 KB |
Output is correct |
41 |
Correct |
2368 ms |
608616 KB |
Output is correct |
42 |
Correct |
2312 ms |
608080 KB |
Output is correct |
43 |
Correct |
2230 ms |
608372 KB |
Output is correct |
44 |
Correct |
1916 ms |
608340 KB |
Output is correct |
45 |
Correct |
1809 ms |
599788 KB |
Output is correct |
46 |
Correct |
2290 ms |
608348 KB |
Output is correct |
47 |
Correct |
2240 ms |
608336 KB |
Output is correct |
48 |
Correct |
2274 ms |
608340 KB |
Output is correct |
49 |
Correct |
1915 ms |
608460 KB |
Output is correct |
50 |
Correct |
914 ms |
518960 KB |
Output is correct |
51 |
Correct |
1532 ms |
608260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2700 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1664 ms |
608060 KB |
Output is correct |
21 |
Correct |
1029 ms |
607836 KB |
Output is correct |
22 |
Correct |
1103 ms |
607828 KB |
Output is correct |
23 |
Correct |
1220 ms |
607804 KB |
Output is correct |
24 |
Correct |
1299 ms |
607460 KB |
Output is correct |
25 |
Correct |
1775 ms |
608016 KB |
Output is correct |
26 |
Correct |
1613 ms |
607904 KB |
Output is correct |
27 |
Correct |
1590 ms |
607828 KB |
Output is correct |
28 |
Correct |
1604 ms |
607812 KB |
Output is correct |
29 |
Correct |
1588 ms |
599324 KB |
Output is correct |
30 |
Correct |
1473 ms |
607880 KB |
Output is correct |
31 |
Correct |
1527 ms |
607828 KB |
Output is correct |
32 |
Correct |
1602 ms |
608084 KB |
Output is correct |
33 |
Correct |
1255 ms |
608084 KB |
Output is correct |
34 |
Correct |
757 ms |
518664 KB |
Output is correct |
35 |
Correct |
1298 ms |
607824 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Execution timed out |
5116 ms |
614260 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |