This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
int 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 (stderr)
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))
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |