제출 #646930

#제출 시각아이디문제언어결과실행 시간메모리
646930BackNoobSjekira (COCI20_sjekira)C++14
110 / 110
59 ms14172 KiB
#pragma GCC optimize ("Ofast") #pragma GCC target ("avx2") #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define endl '\n' #define MASK(i) (1LL << (i)) #define ull unsigned long long #define ld long double #define pb push_back #define all(x) (x).begin() , (x).end() #define BIT(x , i) ((x >> (i)) & 1) #define TASK "task" #define sz(s) (int) (s).size() using namespace std; const int mxN = 3e5 + 227; const int inf = 1e9 + 277; const int mod = 1e9 + 7; const ll infll = 1e18 + 7; const int base = 307; const int LOG = 20; const int block_sz = 500; template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } struct DSU{ int n; vector<int> lab; vector<int> mx; DSU(){} DSU(int _n) { n = _n; lab.resize(n + 7, -1); mx.resize(n + 7); } int root(int u) {return lab[u] < 0 ? u : lab[u] = root(lab[u]);} bool union_root(int u, int v) { u = root(u); v = root(v); if(u == v) return false; if(lab[u] > lab[v]) swap(u, v); lab[u] += lab[v]; lab[v] = u; maximize(mx[u], mx[v]); return true; } } dsu; int n; bool used[mxN]; pair<int, int> ed[mxN]; vector<int> adj[mxN]; int idx[mxN]; int best[mxN]; bool cmp(int a, int b) { return dsu.mx[a] < dsu.mx[b]; } void solve() { cin >> n; dsu = DSU(n); for(int i = 1; i <= n; i++) cin >> dsu.mx[i]; for(int i = 1; i <= n; i++) idx[i] = i; sort(idx + 1, idx + 1 + n, cmp); for(int i = 1; i <= n; i++) best[idx[i]] = i; for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; if(best[u] < best[v]) swap(u, v); adj[u].pb(v); } ll res = 0; for(int i = 1; i <= n; i++) { int u = idx[i]; for(int v : adj[u]) { res += dsu.mx[dsu.root(u)]; res += dsu.mx[dsu.root(v)]; dsu.union_root(u, v); } } cout << res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen("task.inp", "r", stdin); //freopen("task.out", "w", stdout); int tc = 1; //cin >> tc; while(tc--) { solve(); } 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...