Submission #568723

#TimeUsernameProblemLanguageResultExecution timeMemory
568723joelauReconstruction Project (JOI22_reconstruction)C++14
7 / 100
5040 ms6904 KiB
#include <bits/stdc++.h> using namespace std; long long N,M,Q, ufds[505], rnk[505]; tuple<long long,long long,long long> A[100005], B[100005]; long long findSet(long long i) { return ufds[i] == i ? i : ufds[i] = findSet(ufds[i]); } void unionSet(long long i, long long j) { long long a = findSet(i), b = findSet(j); if (a == b) return; if (rnk[a] < rnk[b]) ufds[a] = b; if (rnk[a] > rnk[b]) ufds[b] = a; if (rnk[a] == rnk[b]) ufds[a] = b, rnk[b]++; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> M; for (long long i = 0; i < M; ++i) { long long u,v,w; cin >> u >> v >> w; u--, v--; A[i] = make_tuple(u,v,w); } cin >> Q; while (Q--) { long long x; cin >> x; for (long long i = 0; i < M; ++i) B[i] = make_tuple(abs(x-get<2>(A[i])),get<0>(A[i]),get<1>(A[i])); sort(B,B+M); for (long long i = 0; i < N; ++i) ufds[i] = i, rnk[i] = 1; long long ans = 0; for (long long i = 0; i < M; ++i) { long long u,v,w; tie(w,u,v) = B[i]; if (findSet(u) == findSet(v)) continue; ans += w; unionSet(u,v); } cout << ans << '\n'; } return 0; }
#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...