Submission #1019525

#TimeUsernameProblemLanguageResultExecution timeMemory
1019525ilovveyyouSjekira (COCI20_sjekira)C++14
110 / 110
48 ms25824 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define pll pair<ll, ll> #define pii pair<int, int> #define mp(x, y) make_pair(x, y) #define sz(v) ((int) (v).size()) #define all(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define BIT(x, y) (((x) >> (y)) & 1) #define clz __builtin_clzll #define ctz __builtin_ctzll #define popcount __builtin_popcount #define lg(x) (63 - clz(x)) template <class X, class Y> inline bool maximize(X &x, Y y) { if (x < y) return x = y, true; return false; } template <class X, class Y> inline bool minimize(X &x, Y y) { if (x > y) return x = y, true; return false; } template <class X> inline void compress(vector<X> &a) { sort(all(a)); a.resize(unique(all(a)) - a.begin()); } const ll oo = (ll) 1e17; const int inf = (int) 1e9, mod = (int) 1e9 + 7; const int mxn = (int) 1e5 + 5, mxm = (int) 11; int n; int t[mxn]; vector<int> node; ll ans; bool ok[mxn]; vector<int> g[mxn]; class Dsu { int n; vector<int> par, sz; vector<int> max_cc; // set<pair<int, int>> s; public: Dsu(int n) : n(n) { par.resize(n+1); sz.resize(n+1, 1); max_cc.resize(n+1); for (int i = 1; i <= n; ++i) { par[i] = i; max_cc[i] = t[i]; } } int find_set(int u) { return (u == par[u] ? u : par[u] = find_set(par[u])); } bool union_set(int u, int v) { u = find_set(u); v = find_set(v); if (u == v) return false; if (sz[u] < sz[v]) swap(u, v); par[v] = u; sz[u] += sz[v]; maximize(max_cc[u], max_cc[v]); return true; } int get_max(int u) { return max_cc[find_set(u)]; } }; ll dnc(int p, Dsu &dsu) { if (p == sz(node)) return 0; ll res = dnc(p+1, dsu); for (int v : g[node[p]]) if (ok[v]) { res += t[node[p]] + dsu.get_max(v); dsu.union_set(node[p], v); } ok[node[p]] = true; return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> t[i]; } for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } node.resize(n); iota(all(node), 1); sort(all(node), [&](int &u, int &v) { return t[u] > t[v]; }); Dsu dsu(n); cout << dnc(0, dsu); 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...