이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 1e18 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, a[maxn];
vector <ll> g[maxn];
ll ord[maxn];
ll q[maxn];
bool cmp (ll x, ll y)
{
return (a[x] < a[y]);
}
struct DSU
{
ll par[maxn];
DSU ()
{
iota(par, par + maxn, 0);
}
ll find_set (ll v)
{
if (v == par[v]) return v;
return v = find_set(par[v]);
}
void unite (ll u, ll v)
{
u = find_set(u);
v = find_set(v);
if (u != v)
{
par[v] = u;
a[u] = max(a[u], a[v]);
}
}
} dsu;
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1; i <= n; ++i)
{
cin >> a[i];
q[i] = a[i];
ord[i] = i;
}
for (ll i = 1; i < n; ++i)
{
ll u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
sort(ord + 1, ord + 1 + n, cmp);
ll ans = 0;
for (ll i = 1; i <= n; ++i)
{
ll v = ord[i];
for (ll to : g[v])
{
if (a[v] > a[to])
{
v = dsu.find_set(v);
to = dsu.find_set(to);
if (v != to)
{
ans += a[v] + a[to];
dsu.unite(v, to);
}
}
}
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("angry.in", "r", stdin);
// freopen("angry.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# | 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... |