結果

問題 No.2115 Making Forest Easy
ユーザー packer_jppacker_jp
提出日時 2022-10-28 23:49:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 12,115 bytes
コンパイル時間 2,387 ms
コンパイル使用メモリ 215,440 KB
実行使用メモリ 13,880 KB
最終ジャッジ日時 2024-07-06 03:09:14
合計ジャッジ時間 5,689 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,880 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define t0 first
#define t1 second
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
constexpr double pi = 3.14159265358979323846;
constexpr ll dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr ll sign(ll a) { return (a > 0) - (a < 0); }
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
constexpr ll cdiv(ll a, ll b) { return -fdiv(-a, b); }
constexpr ll pw(ll n) { return 1ll << n; }
constexpr ll flg(ll n) { return 63 - __builtin_clzll(n); }
constexpr ll clg(ll n) { return flg(n - 1) + 1; }
constexpr ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T> constexpr T sq(const T &a) { return a * a; }
template <typename T, typename U> constexpr bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <typename T, typename U> constexpr bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
    os << "(" << a.first << ", " << a.second << ")";
    return os;
}
template <typename T, typename U, typename V> ostream &operator<<(ostream &os, const tuple<T, U, V> &a) {
    os << "(" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
    os << "(";
    for (auto itr = a.begin(); itr != a.end(); ++itr) os << *itr << (next(itr) != a.end() ? ", " : "");
    os << ")";
    return os;
}
#ifdef ONLINE_JUDGE
#define dump(...) (void(0))
#else
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head &&head, Tail &&... tail) {
    cerr << head;
    if (sizeof...(Tail)) cerr << ", ";
    debug(tail...);
}
#define dump(...) cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ", debug(__VA_ARGS__)
#endif
struct rep {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { ++v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v < *i; }
    };
    ll l, r;
    rep(ll l, ll r) : l(l), r(r) {}
    rep(ll r) : rep(0, r) {}
    itr begin() const { return l; };
    itr end() const { return r; };
};
struct per {
    struct itr {
        ll v;
        itr(ll v) : v(v) {}
        void operator++() { --v; }
        ll operator*() const { return v; }
        bool operator!=(itr i) const { return v > *i; }
    };
    ll l, r;
    per(ll l, ll r) : l(l), r(r) {}
    per(ll r) : per(0, r) {}
    itr begin() const { return r - 1; };
    itr end() const { return l - 1; };
};
struct io_setup {
    static constexpr int PREC = 20;
    io_setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} iOS;

template <typename M> struct modint {
    ll val;
    modint(ll val = 0) : val(val >= 0 ? val % M::mod : (M::mod - (-val) % M::mod) % M::mod) {}
    static ll mod() { return M::mod; }
    modint inv() const {
        ll a = val, b = M::mod, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return u;
    }
    modint pow(ll k) const {
        modint ret = 1, mul = val;
        while (k) {
            if (k & 1) ret *= mul;
            mul *= mul;
            k >>= 1;
        }
        return ret;
    }
    modint &operator+=(const modint &a) {
        if ((val += a.val) >= M::mod) val -= M::mod;
        return *this;
    }
    modint &operator-=(const modint &a) {
        if ((val += M::mod - a.val) >= M::mod) val -= M::mod;
        return *this;
    }
    modint &operator*=(const modint &a) {
        (val *= a.val) %= M::mod;
        return *this;
    }
    modint &operator/=(const modint &a) { return *this *= a.inv(); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint(-val); }
    friend bool operator==(const modint &a, const modint &b) { return a.val == b.val; }
    friend bool operator!=(const modint &a, const modint &b) { return rel_ops::operator!=(a, b); }
    friend modint operator+(const modint &a, const modint &b) { return modint(a) += b; }
    friend modint operator-(const modint &a, const modint &b) { return modint(a) -= b; }
    friend modint operator*(const modint &a, const modint &b) { return modint(a) *= b; }
    friend modint operator/(const modint &a, const modint &b) { return modint(a) /= b; }
    friend istream &operator>>(istream &is, modint &a) {
        ll val;
        is >> val;
        a = modint(val);
        return is;
    }
    friend ostream &operator<<(ostream &os, const modint &a) { return os << a.val; }
};

struct _998244353 {
    constexpr static ll mod = 998244353;
};
struct _1000000007 {
    constexpr static ll mod = 1000000007;
};
using modint998244353 = modint<_998244353>;
using modint1000000007 = modint<_1000000007>;

struct arbitrary {
    static ll mod;
};
ll arbitrary::mod;

template <typename V> struct fenwick_tree {
    vector<V> data;
    fenwick_tree(ll n) : data(n + 1, V()) {}
    void add(ll i, const V &x) {
        for (++i; i < (ll)data.size(); i += i & -i) data[i] += x;
    }
    V sum(ll i) const {
        V ret = V();
        for (; i > 0; i -= i & -i) ret += data[i];
        return ret;
    }
    V sum(ll l, ll r) const { return sum(r) - sum(l); }
};
template <typename P> struct unionfind {
    using V = typename P::V;
    ll n;
    vector<ll> ps;
    vector<V> val;
    unionfind(const vector<V> &val) : n(val.size()), ps(n, -1), val(val) {}
    unionfind(ll n, const V &a = {}) : unionfind(vector<V>(n, a)) {}
    ll find(ll i) {
        if (ps[i] < 0) return i;
        return ps[i] = find(ps[i]);
    }
    ll size(ll i) { return -ps[find(i)]; }
    void unite(ll i, ll j) {
        if ((i = find(i)) == (j = find(j))) return;
        if (-ps[i] < -ps[j]) swap(i, j);
        ps[i] += ps[j];
        P::merge(val[i], val[j]);
        ps[j] = i;
    }
    bool same(ll i, ll j) { return find(i) == find(j); }
    V &operator[](ll i) { return val[find(i)]; }
    vector<vector<ll>> groups() {
        vector<vector<ll>> ret(n);
        for (ll i : rep(n)) ret[find(i)].push_back(i);
        ret.erase(remove_if(all(ret), [](const vector<ll> &v) { return v.empty(); }), ret.end());
        return ret;
    }
};
struct normal_uf {
    using V = struct {};
    static void merge(V &a, const V &b) {}
};
template <typename V> V xor64(V lb, V ub) {
    static ull x = 88172645463325252ull;
    x ^= x << 7;
    return lb + (x ^= x >> 9) % (ub - lb);
}
template <typename V> vector<V> prime_factorize(V n) {
    vector<V> ret;
    for (V i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            ret.push_back(i);
            n /= i;
        }
    }
    if (n != 1) ret.push_back(n);
    return ret;
}
template <typename F> ll bisect(ll ok, ll ng, F f) {
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
vector<bool> prime_table(ll n) {
    vector<bool> ret(n + 1, true);
    if (n >= 0) ret[0] = false;
    if (n >= 1) ret[1] = false;
    for (ll i = 2; i * i <= n; ++i) {
        if (!ret[i]) continue;
        for (ll j = i << 1; j <= n; j += i) ret[j] = false;
    }
    return ret;
}

struct rational {
    long long num, den;
    static long long gcd(long long a, long long b) {
        if (a < 0) { a = -a; }
        if (b < 0) { b = -b; }
        while (b) { std::swap(a %= b, b); }
        return a;
    }
    void reduce() {
        long long g = gcd(num, den);
        num /= g, den /= g;
    }
    rational(long long num_ = 0, long long den_ = 1, bool reduction = true) : num(num_), den(den_) {
        if (reduction) { reduce(); }
        if (den < 0) { num = -num, den = -den; }
    }
    rational &operator+=(const rational &a) {
        num = num * a.den + a.num * den, den *= a.den;
        reduce();
        return *this;
    }
    rational &operator-=(const rational &a) { return *this += -a; }
    rational &operator*=(const rational &a) {
        num *= a.num, den *= a.den;
        reduce();
        return *this;
    }
    rational &operator/=(const rational &a) { return *this *= a.inv(); }
    rational operator+() const { return *this; }
    rational operator-() const { return rational(-num, den, false); }
    friend bool operator==(const rational &a, const rational &b) { return a.num == b.num && a.den == b.den; }
    friend bool operator!=(const rational &a, const rational &b) { return std::rel_ops::operator!=(a, b); }
    friend bool operator<(const rational &a, const rational &b) { return a.num * b.den < b.num * a.den; }
    friend bool operator>(const rational &a, const rational &b) { return std::rel_ops::operator>(a, b); }
    friend bool operator<=(const rational &a, const rational &b) { return std::rel_ops::operator<=(a, b); }
    friend bool operator>=(const rational &a, const rational &b) { return std::rel_ops::operator>=(a, b); }
    friend rational operator+(rational a, const rational &b) { return a += b; }
    friend rational operator-(rational a, const rational &b) { return a -= b; }
    friend rational operator*(rational a, const rational &b) { return a *= b; }
    friend rational operator/(rational a, const rational &b) { return a /= b; }
    rational inv() const { return rational(den, num, false); }
    rational pow(long long n) const {
        if (n < 0) { return pow(-n).inv(); }
        rational ret(1, 1, false), mul = *this;
        while (n > 0) {
            if (n & 1) { ret.num *= mul.num, ret.den *= mul.den; }
            mul.num *= mul.num, mul.den *= mul.den;
            n >>= 1;
        }
        return ret;
    }
    double to_double() const { return (double)num / den; }
    friend std::istream &operator>>(std::istream &is, rational &rhs) {
        long long n, d;
        char s;
        is >> n >> s >> d;
        rhs = rational(n, d);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const rational &rhs) { return os << rhs.num << " / " << rhs.den; }
};
int main() {
    ll n;
    cin >> n;
    vll a(n);
    for (ll i : rep(n)) cin >> a[i];
    vector g(n, vll());
    for (ll i : rep(n - 1)) {
        ll u, v;
        cin >> u >> v, --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    using mint = modint998244353;
    auto dp = [&](auto dp, ll cur, ll par, ll root) -> tuple<mint, mint, mint> {
        mint ok = 1, ng = 1, cnt = 0;
        for (ll to : g[cur]) {
            if (to == par) continue;
            auto [nok, nng, ncnt] = dp(dp, to, cur, root);
            if (root == 2) { dump(to, nok, nng, ncnt); }
            ok *= nok;
            ng *= nng;
            cnt += ncnt / nng;
        }
        if (make_pair(a[cur], cur) > make_pair(a[root], root)) {
            return {ok * 2, ok, 0};
        } else {
            return {ok * 2, ok + ng, cnt * ng + ng};
        }
    };
    mint ans = 0;
    for (ll i : rep(n)) {
        auto [ok, ng, cnt] = dp(dp, i, -1, i);
        dump(i, ok, ng, cnt);
        ans += a[i] * cnt;
    }
    cout << ans << endl;
}
0