Submission #748268

#TimeUsernameProblemLanguageResultExecution timeMemory
748268nguyentunglamReconstruction Project (JOI22_reconstruction)C++17
7 / 100
5065 ms6260 KiB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 01;
int a[N], b[N], w[N], lab[N];
int n, m;

int root(int v) {
    return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}

bool join(int u, int v) {
    u = root(u); v = root(v);
    if (u == v) return 0;
    if (lab[u] > lab[v]) swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
    return 1;
}

long long calc(int x) {
    for(int i = 1; i <= n; i++) lab[i] = -1;
    vector<tuple<int, int, int> > lst;
    for(int i = 1; i <= m; i++) lst.emplace_back(abs(x - w[i]), a[i], b[i]);
    sort(lst.begin(), lst.end());
    long long ret = 0;
    for(auto &[cost, x, y] : lst) {
        if (join(x, y)) ret += cost;
    }
    return ret;
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n >> m;
    for(int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> w[i];
    int q; cin >> q;
    while (q--) {
        int x; cin >> x;
        cout << calc(x) << endl;
    }
}

Compilation message (stderr)

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:39:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:40:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:43:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:44:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...