이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifndef _LOCAL
//#pragma GCC optimize("O3,Ofast")
#else
#pragma GCC optimize("O0")
#endif
template<typename t> inline void umin(t &a, const t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, const t b) {a = max(a, b);}
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef int8_t byte;
ll time() {return chrono::system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
#define ft first
#define sd second
#define len(f) int((f).size())
#define bnd(f) (f).begin(), (f).end()
#define _ <<' '<<
const int inf = 1e9 + 5;
const ll inf64 = 4e18 + 5;
const int md = 998244353;
namespace MD {
void add(int &a, const int b) {if((a += b) >= md) a -= md;}
void sub(int &a, const int b) {if((a -= b) < 0) a += md;}
int prod(const int a, const int b) {return ll(a) * b % md;}
};
int n;
const int N = 1e5 + 5;
vector<int> g[N];
int t[N];
ll ans;
int p[N], f[N];
int get(int i) {return p[i] == i ? i : p[i] = get(p[i]);}
void unite(int i, int j) {p[i = get(i)] = j = get(j); umax(t[j], t[i]);}
void solve() {
cin >> n;
iota(p, p + n, 0);
for(int i = 0; i < n; ++i) g[i].clear();
iota(f, f + n, 0);
for(int i = 0; i < n; ++i) cin >> t[i];
for(int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
sort(f, f + n, [&] (const int &a, const int &b) {return t[a] < t[b];});
ans = 0;
for(int i = 0; i < n; ++i) {
int v = f[i];
for(int u : g[v]) {
if(t[u] > t[v]) continue;
if(get(u) != get(v)) {
ans += t[get(u)] + t[get(v)];
unite(u, v);
}
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef _LOCAL
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
#else
system("color a");
freopen("in.txt", "r", stdin);
int t; cin >> t;
while(t--)
#endif
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |