답안 #591193

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
591193 2022-07-07T02:41:23 Z nguyen31hoang08minh2003 Sjekira (COCI20_sjekira) C++14
0 / 110
48 ms 8536 KB
/*
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|\/ /|\ \/|
| \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ | \/ |
|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |  \/|\/  |
|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |   \|/   |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |   /|\   |
|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |  /\|/\  |
| /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ | /\ |
|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|/\ \|/ /\|
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
*/
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 8536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2676 KB Output isn't correct
2 Halted 0 ms 0 KB -