Submission #1249235

#TimeUsernameProblemLanguageResultExecution timeMemory
124923529ChuManhTichSjekira (COCI20_sjekira)C++20
110 / 110
27 ms2376 KiB
#include<bits/stdc++.h>
using namespace std;

#define NAME "FOX"
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int, int>
#define fi first
#define se second
#define fastIO ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                                \
    cout.tie(NULL);
#define BIT(x, i) ((x >> i) & 1)
#define ALL(x) x.begin(), x.end()

const int maxn = 2e5 + 11;
const int LOGN = 20;
const int MOD = 1e9 + 7;


ll res = 0;
ii edge[maxn];
int n, a[maxn];

struct DSU {
    int p[maxn], sz[maxn];
    void make_set(int i) {
        p[i] = i;
        sz[i] = a[i];
    }

    int find_set(int u) {
        return ((u == p[u]) ? u : p[u] = find_set(p[u]));
    }

    void united(int u, int v) {
        u = find_set(u);
        v = find_set(v);
        if(u == v) return;
        res += sz[u] + sz[v];
        sz[u] = max(sz[u], sz[v]);
        p[v] = u;
    }
} dsu;

bool cmp(ii x, ii y) {
    int valx = max(a[x.fi], a[x.se]);
    int valy = max(a[y.fi], a[y.se]);

    return valx < valy;
}

signed main() {
    if(fopen(NAME".INP", "r")) {
        freopen(NAME".INP", "r", stdin);
        freopen(NAME".OUT", "w", stdout);
    }
    fastIO;

    cin >> n;
    FOR(i, 1, n) {
        cin >> a[i];
        dsu.make_set(i);
    }

    FOR(i, 1, n - 1) {
        int u, v; cin >> u >> v;
        edge[i] = {u, v};
    }

    sort(edge + 1, edge + n, cmp);

    FOR(i, 1, n - 1) {
        int u = edge[i].fi, v = edge[i].se;
        dsu.united(u, v);
    }

    cout << res;
    return 0;
}


Compilation message (stderr)

sjekira.cpp: In function 'int main()':
sjekira.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen(NAME".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         freopen(NAME".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...