Submission #1244827

#TimeUsernameProblemLanguageResultExecution timeMemory
1244827CrabCNHSjekira (COCI20_sjekira)C++20
110 / 110
44 ms11076 KiB
#include <bits/stdc++.h> #define int long long #define pii pair<int, int> #define fi first #define se second using namespace std; const int N = 1e5 + 5; int a[N]; void read() { #define task "KB" if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } } struct edge { int u, v, c; }; struct node { int id, val; }; bool cmpnode (node A, node B) { return A.val < B.val; } bool cmpedge(edge a, edge b) { return a.c > b.c; } int n; int sz[N + 5], par[N + 5]; int tt[N]; vector<edge> edges; vector <int> adj[N]; int mx[N + 5]; node t[N]; void init(int n) { for (int i = 1; i <= n; i++) { par[i] = -1; sz[i] = 1; mx[i] = t[i].val; } } int getroot(int u) { if (par[u] < 0) { return u; } return par[u] = getroot(par[u]); } bool check(int u, int v) { return getroot(u) == getroot(v); } bool join(int u, int v) { u = getroot(u); v = getroot(v); if (u == v) { return 0; } if (sz[u] < sz[v]) { swap(u, v); } sz[u] += sz[v]; par[v] = u; mx[u] = max (mx[v], mx[u]); mx[v] = max (mx[v], mx[u]); return 1; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i ++) { cin >> t[i].val;; t[i].id = i; tt[i] = t[i].val; } init(n); for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back (v); adj[v].push_back (u); } int ans = 0; sort (t + 1, t + n + 1, cmpnode); for (int i = 1; i <= n; i ++) { int cur_node = t[i].id; int cur_val = t[i].val; for (auto v : adj[cur_node]) { if (check (cur_node, v) == 1 || mx[getroot (v)] > mx[getroot (cur_node)]) { continue; } else { ans += mx[getroot (cur_node)] + mx[getroot (v)]; join (cur_node, v); } } } cout << ans; return 0; }

Compilation message (stderr)

sjekira.cpp: In function 'void read()':
sjekira.cpp:17:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         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...