#include <bits/stdc++.h>
// #define int long long
using ll = long long;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
vector<int> dsu, sz;
int f(int x) { return x == dsu[x] ? x : dsu[x] = f(dsu[x]); }
void un(int a, int b)
{
a = f(a), b = f(b);
if (a == b)
return;
if (sz[a] < sz[b])
swap(a, b);
dsu[b] = a, sz[a] += sz[b];
}
void solve()
{
int n, m;
cin >> n >> m;
vector<array<int, 3>> e(m);
int flag = 1;
for (auto &i : e)
{
cin >> i[1] >> i[2] >> i[0], --i[1], --i[2];
flag &= i[1] + 1 == i[2];
}
sort(all(e));
/*
if (flag)
{
vector<vector<int>> v(n);
for (auto [w, a, b] : e0)
v[a].emplace_back(w);
for (auto &i : v)
sort(i.begin(), i.end());
vector<array<int, 3>> op;
for (auto i : v)
int q;
cin >> q;
vector<int> queries(q);
for (int &i : queries)
cin >> i;
vector<int> ans(q + 1, 0), tmp(q + 1, 0);
for (auto i : v)
{
int p = 0;
for (int j : i)
{
}
}
}
*/
int q, pt = 0;
cin >> q;
while (q--)
{
int x;
cin >> x;
sz.assign(n, 1), dsu = sz;
for (int i = 0; i < n; ++i)
dsu[i] = i;
while (pt < m && e[pt][0] <= x)
++pt;
int l = pt - 1, r = pt;
ll ans = 0;
while (sz[f(0)] < n)
{
int nxt = -1, flag = 0;
if (0 <= l)
nxt = l, flag = 0;
if (nxt == -1 || r < m && abs(e[nxt][0] - x) > abs(e[r][0] - x))
nxt = r, flag = 1;
if (flag)
++r;
else
--l;
// cout << nxt << ' ';
if (f(e[nxt][2]) != f(e[nxt][1]))
{
un(e[nxt][2], e[nxt][1]);
ans += abs(x - e[nxt][0]);
}
}
cout << ans << '\n';
}
}
/*
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
6
3
6
8
10
13
17
*/
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
Compilation message
reconstruction.cpp: In function 'void solve()':
reconstruction.cpp:83:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
83 | if (nxt == -1 || r < m && abs(e[nxt][0] - x) > abs(e[r][0] - x))
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
26 ms |
1624 KB |
Output is correct |
21 |
Correct |
33 ms |
1624 KB |
Output is correct |
22 |
Correct |
32 ms |
1884 KB |
Output is correct |
23 |
Correct |
26 ms |
1628 KB |
Output is correct |
24 |
Correct |
26 ms |
1628 KB |
Output is correct |
25 |
Correct |
26 ms |
1624 KB |
Output is correct |
26 |
Correct |
27 ms |
1628 KB |
Output is correct |
27 |
Correct |
26 ms |
1628 KB |
Output is correct |
28 |
Correct |
28 ms |
1628 KB |
Output is correct |
29 |
Correct |
31 ms |
1628 KB |
Output is correct |
30 |
Correct |
25 ms |
1628 KB |
Output is correct |
31 |
Correct |
26 ms |
1644 KB |
Output is correct |
32 |
Correct |
26 ms |
1644 KB |
Output is correct |
33 |
Correct |
26 ms |
1824 KB |
Output is correct |
34 |
Correct |
30 ms |
1624 KB |
Output is correct |
35 |
Correct |
26 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5075 ms |
2996 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Execution timed out |
5048 ms |
8100 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
26 ms |
1624 KB |
Output is correct |
21 |
Correct |
33 ms |
1624 KB |
Output is correct |
22 |
Correct |
32 ms |
1884 KB |
Output is correct |
23 |
Correct |
26 ms |
1628 KB |
Output is correct |
24 |
Correct |
26 ms |
1628 KB |
Output is correct |
25 |
Correct |
26 ms |
1624 KB |
Output is correct |
26 |
Correct |
27 ms |
1628 KB |
Output is correct |
27 |
Correct |
26 ms |
1628 KB |
Output is correct |
28 |
Correct |
28 ms |
1628 KB |
Output is correct |
29 |
Correct |
31 ms |
1628 KB |
Output is correct |
30 |
Correct |
25 ms |
1628 KB |
Output is correct |
31 |
Correct |
26 ms |
1644 KB |
Output is correct |
32 |
Correct |
26 ms |
1644 KB |
Output is correct |
33 |
Correct |
26 ms |
1824 KB |
Output is correct |
34 |
Correct |
30 ms |
1624 KB |
Output is correct |
35 |
Correct |
26 ms |
1624 KB |
Output is correct |
36 |
Correct |
596 ms |
1972 KB |
Output is correct |
37 |
Execution timed out |
5046 ms |
1692 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
26 ms |
1624 KB |
Output is correct |
21 |
Correct |
33 ms |
1624 KB |
Output is correct |
22 |
Correct |
32 ms |
1884 KB |
Output is correct |
23 |
Correct |
26 ms |
1628 KB |
Output is correct |
24 |
Correct |
26 ms |
1628 KB |
Output is correct |
25 |
Correct |
26 ms |
1624 KB |
Output is correct |
26 |
Correct |
27 ms |
1628 KB |
Output is correct |
27 |
Correct |
26 ms |
1628 KB |
Output is correct |
28 |
Correct |
28 ms |
1628 KB |
Output is correct |
29 |
Correct |
31 ms |
1628 KB |
Output is correct |
30 |
Correct |
25 ms |
1628 KB |
Output is correct |
31 |
Correct |
26 ms |
1644 KB |
Output is correct |
32 |
Correct |
26 ms |
1644 KB |
Output is correct |
33 |
Correct |
26 ms |
1824 KB |
Output is correct |
34 |
Correct |
30 ms |
1624 KB |
Output is correct |
35 |
Correct |
26 ms |
1624 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5075 ms |
2996 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |