제출 #899845

#제출 시각아이디문제언어결과실행 시간메모리
899845TAhmed33Reconstruction Project (JOI22_reconstruction)C++98
3 / 100
5039 ms22084 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("Ofast") using namespace std; typedef long long ll; const int MAXN = 1e3 + 1; array <int, 3> edges[MAXN]; int sze[MAXN], par[MAXN]; inline void init (int n) { for (int i = 1; i <= 500; i++) { sze[i] = 1; par[i] = i; } } int find (int x) { return par[x] == x ? x : par[x] = find(par[x]); } ll sum; int x; inline void merge (int a, int b, int c) { a = find(a); b = find(b); if (a == b) return; sum += abs(c - x); if (sze[a] > sze[b]) swap(a, b); sze[b] += sze[a]; par[a] = b; } int n, m; int main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; edges[i] = {c, a, b}; } sort(edges + 1, edges + m + 1); int ptr = 1; int q; cin >> q; while (q--) { cin >> x; if (m == 1) { cout << abs(x - edges[1][0]) << '\n'; continue; } init(n); sum = 0; while (ptr + 1 <= m && abs(edges[ptr + 1][0] - x) <= abs(edges[ptr][0] - x)) ptr++; int l = ptr, r = ptr; while (l >= 1 || r <= m) { if (l >= 1 && (r > m || abs(x - edges[l][0]) < abs(x - edges[r][0]))) { merge(edges[l][1], edges[l][2], edges[l][0]); l--; } else { merge(edges[r][1], edges[r][2], edges[r][0]); r++; } } cout << sum << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...