This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|
| \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ |
|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ | \/|\/ |
|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ | \|/ |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ | /|\ |
|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ | /\|/\ |
| /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ |
|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
*/
#include <bits/stdc++.h>
#define fore(i, a, b) for (signed i = (a), i##_last = (b); i < i##_last; ++i)
#define fort(i, a, b) for (signed i = (a), i##_last = (b); i <= i##_last; ++i)
#define ford(i, a, b) for (signed i = (a), i##_last = (b); i >= i##_last; --i)
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double;
template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;};
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
struct DisjointSet {
private:
std::vector<int> r, maximum;
public:
DisjointSet() {};
DisjointSet(const int n): r(n, -1) {};
void resize(const int n) {
r.resize(n, -1);
maximum.resize(n);
}
void reload() {
std::fill(r.begin(), r.end(), -1);
};
int getRoot(const int x) const {
return r[x] < 0 ? x : getRoot(r[x]);
}
int unite(int x, int y) {
x = getRoot(x);
y = getRoot(y);
if (x == y)
return 0;
const int res = maximum[x] + maximum[y];
if (r[x] > r[y])
std::swap(x, y);
maximum[x] = max(maximum[x], maximum[y]);
r[x] += r[y];
r[y] = x;
return res;
}
bool checkConnected(const int x, const int y) const {
return getRoot(x) == getRoot(y);
}
int getTreeSize(const int x) const {
return -r[getRoot(x)];
}
int& getMaximum(const int x) {
return maximum[getRoot(x)];
}
};
const int maxN = 100005;
int n, res, t[maxN];
vi p, adj[maxN];
DisjointSet dsu;
void input() {
int x, y;
cin >> n;
p.resize(n);
dsu.resize(n);
fore(i, 0, n) {
cin >> t[i];
p[i] = i;
dsu.getMaximum(i) = t[i];
}
fore(_, 1, n) {
cin >> x >> y;
--x;
--y;
adj[x].pb(y);
adj[y].pb(x);
}
}
void solve() {
sort(all(p), [&](const int &x, const int &y) -> bool {
return t[x] < t[y];
});
for (const int &u : p)
for (const int &v : adj[u]) {
if (t[v] > t[u])
continue;
res += dsu.unite(u, v);
}
cout << res << '\n';
}
int main() {
#ifdef LOCAL
freopen("input.INP", "r", stdin);
#endif // LOCAL
cin.tie(0) -> sync_with_stdio(0);
cout.tie(0);
input();
solve();
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... |