이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n, m, q;
const int maxn = 1e5 + 7;
const int maxm = 500 + 7;
const ll oo = 1e18 + 3;
int p[maxn], sz[maxn];
int E1[maxn][maxm], s1[maxn];
int E2[maxn][maxm], s2[maxn];
vector<int> A[maxn]; vector<ll> arr;
vector<pair<pii, int>> E;
int GI(ll x) {
return lower_bound(all(arr), x) - arr.begin();
}
int get(int a) {
return (p[a] == a) ? a : p[a] = get(p[a]);
}
bool merge(int a, int b) {
a = get(a); b = get(b);
if (a == b) return 0;
if (sz[a] > sz[b]) swap(a, b);
p[a] = b; sz[b] += sz[a]; sz[a] = 0;
return 1;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w; u--; v--;
E.pb(Mp(Mp(u, v), w)); arr.pb(w);
}
arr.pb(-oo); arr.pb(oo);
sort(all(arr)); arr.resize(unique(all(arr)) - arr.begin());
for (int i = 0; i < m; i++) {
E[i].S = GI(E[i].S);
A[E[i].S].pb(i);
}
for (int i = 0; i < len(arr); i++) {
iota(p, p + n, 0); fill(sz, sz + n, 1);
for (int j : A[i]) {
int u = E[j].F.F, v = E[j].F.S;
if (merge(u, v)) E1[i][s1[i]++] = j;
}
if (i - 1 >= 0) {
for (int r = 0; r < s1[i - 1]; r++) {
int j = E1[i - 1][r];
int u = E[j].F.F, v = E[j].F.S;
if (merge(u, v)) E1[i][s1[i]++] = j;
}
}
}
for (int i = len(arr) - 1; i >= 0; i--) {
iota(p, p + n, 0); fill(sz, sz + n, 1);
for (int j : A[i]) {
int u = E[j].F.F, v = E[j].F.S;
if (merge(u, v)) E2[i][s2[i]++] = j;
}
if (i + 1 < len(arr)) {
for (int r = 0; r < s2[i + 1]; r++) {
int j = E2[i + 1][r];
int u = E[j].F.F, v = E[j].F.S;
if (merge(u, v)) E2[i][s2[i]++] = j;
}
}
}
cin >> q;
q = min(q, 100000);
while (q--) {
ll x;
cin >> x;
int j = GI(x);
ll ans = 0;
int p1 = 0, p2 = 0;
iota(p, p + n, 0); fill(sz, sz + n, 1);
while (p1 < s1[j - 1] && p2 < s2[j]) {
int r1 = E1[j - 1][p1], r2 = E2[j][p2];
ll w1 = x - arr[E[r1].S], w2 = arr[E[r2].S] - x;
if (w1 < w2) {
ans += merge(E[r1].F.F, E[r1].F.S) * w1; p1++;
}
else {
ans += merge(E[r2].F.F, E[r2].F.S) * w2; p2++;
}
}
while (p1 < s1[j - 1]) {
int r1 = E1[j - 1][p1]; ll w1 = x - arr[E[r1].S];
ans += merge(E[r1].F.F, E[r1].F.S) * w1; p1++;
}
while (p2 < s2[j]) {
int r2 = E2[j][p2]; ll w2 = arr[E[r2].S] - x;
ans += merge(E[r2].F.F, E[r2].F.S) * w2; p2++;
}
cout << ans << endl;
}
return 0;
}
# | 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... |