結果

問題 No.2949 Product on Tree
ユーザー umimelumimel
提出日時 2024-10-25 22:41:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 307 ms / 2,000 ms
コード長 5,324 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 179,316 KB
実行使用メモリ 40,984 KB
最終ジャッジ日時 2024-10-25 22:41:56
合計ジャッジ時間 17,301 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,824 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 261 ms
23,636 KB
testcase_04 AC 255 ms
23,152 KB
testcase_05 AC 265 ms
23,672 KB
testcase_06 AC 294 ms
23,836 KB
testcase_07 AC 255 ms
23,256 KB
testcase_08 AC 280 ms
24,216 KB
testcase_09 AC 265 ms
24,416 KB
testcase_10 AC 271 ms
24,772 KB
testcase_11 AC 280 ms
25,600 KB
testcase_12 AC 273 ms
28,324 KB
testcase_13 AC 269 ms
28,976 KB
testcase_14 AC 272 ms
30,744 KB
testcase_15 AC 288 ms
33,384 KB
testcase_16 AC 271 ms
31,372 KB
testcase_17 AC 279 ms
32,376 KB
testcase_18 AC 279 ms
32,236 KB
testcase_19 AC 283 ms
34,580 KB
testcase_20 AC 284 ms
33,936 KB
testcase_21 AC 290 ms
35,128 KB
testcase_22 AC 280 ms
36,268 KB
testcase_23 AC 273 ms
23,824 KB
testcase_24 AC 285 ms
24,020 KB
testcase_25 AC 270 ms
24,160 KB
testcase_26 AC 271 ms
24,052 KB
testcase_27 AC 273 ms
24,452 KB
testcase_28 AC 283 ms
24,548 KB
testcase_29 AC 272 ms
24,636 KB
testcase_30 AC 272 ms
25,292 KB
testcase_31 AC 281 ms
26,560 KB
testcase_32 AC 287 ms
27,124 KB
testcase_33 AC 285 ms
31,024 KB
testcase_34 AC 291 ms
36,076 KB
testcase_35 AC 286 ms
32,904 KB
testcase_36 AC 307 ms
38,204 KB
testcase_37 AC 295 ms
38,928 KB
testcase_38 AC 296 ms
35,120 KB
testcase_39 AC 288 ms
35,692 KB
testcase_40 AC 293 ms
40,264 KB
testcase_41 AC 306 ms
37,312 KB
testcase_42 AC 303 ms
40,984 KB
testcase_43 AC 108 ms
19,740 KB
testcase_44 AC 109 ms
18,684 KB
testcase_45 AC 138 ms
22,900 KB
testcase_46 AC 123 ms
20,852 KB
testcase_47 AC 90 ms
15,760 KB
testcase_48 AC 126 ms
21,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 2;


template<typename T> 
struct edge{
    int from;
    int to;
    T cost;
    int id;

    edge(){}
    edge(int to, T cost=1) : from(-1), to(to), cost(cost){}
    edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){}

    void reverse(){swap(from, to);}
};

template<typename T>
struct edges : std::vector<edge<T>>{
    void sort(){
        std::sort(
            (*this).begin(),
            (*this).end(), 
            [](const edge<T>& a, const edge<T>& b){
                return a.cost < b.cost;
            }
        );
    }
};

template<typename T = bool>
struct graph : std::vector<edges<T>>{
private:
    int n = 0;
    int m = 0;
    edges<T> es;
    bool dir;

public:
    graph(int n, bool dir) : n(n), dir(dir){
        (*this).resize(n);
    }

    void add_edge(int from, int to, T cost=1){
        if(dir){
            es.push_back(edge<T>(from, to, cost, m));
            (*this)[from].push_back(edge<T>(from, to, cost, m++));
        }else{
            if(from > to) swap(from, to);
            es.push_back(edge<T>(from, to, cost, m));
            (*this)[from].push_back(edge<T>(from, to, cost, m));
            (*this)[to].push_back(edge<T>(to, from, cost, m++));
        }
    }

    int get_vnum(){
        return n;
    }

    int get_enum(){
        return m;
    }

    bool get_dir(){
        return dir;
    }

    edge<T> get_edge(int i){
        return es[i];
    }

    edges<T> get_edge_set(){
        return es;
    }
};

template<typename T>
struct redge{
    int from, to;
    T cap, cost;
    int rev;
    
    redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){}
    redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){}
};

template<typename T> using Edges = vector<edge<T>>;
template<typename T> using weighted_graph = vector<Edges<T>>;
template<typename T> using tree = vector<Edges<T>>;
using unweighted_graph = vector<vector<int>>;
template<typename T> using residual_graph = vector<vector<redge<T>>>;


template<long long mod>
class modint{
    long long x;
public:
    modint(long long x=0) : x((x%mod+mod)%mod) {}
    modint operator-() const { 
      return modint(-x);
    }
    bool operator==(const modint& a){
        if(x == a) return true;
        else return false;
    }
    bool operator==(long long a){
        if(x == a) return true;
        else return false;
    }
    bool operator!=(const modint& a){
        if(x != a) return true;
        else return false;
    }
    bool operator!=(long long a){
        if(x != a) return true;
        else return false;
    }
    modint& operator+=(const modint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator-=(const modint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator*=(const  modint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    modint operator+(const modint& a) const {
        modint res(*this);
        return res+=a;
    }
    modint operator-(const modint& a) const {
        modint res(*this);
        return res-=a;
    }
    modint operator*(const modint& a) const {
        modint res(*this);
        return res*=a;
    }
    modint pow(long long t) const {
        if (!t) return 1;
        modint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    modint inv() const {
        return pow(mod-2);
    }
    modint& operator/=(const modint& a) {
        return (*this) *= a.inv();
    }
    modint operator/(const modint& a) const {
        modint res(*this);
        return res/=a;
    }

    friend std::istream& operator>>(std::istream& is, modint& m) noexcept {
        is >> m.x;
        m.x %= mod;
        if (m.x < 0) m.x += mod;
        return is;
    }

    friend ostream& operator<<(ostream& os, const modint& m){
        os << m.x;
        return os;
    }
};

using mint = modint<MOD998244353>;

void solve(){
    int n; cin >> n;
    vector<int> a(n);
    for(int i=0; i<n; i++) cin >> a[i];

    graph<int> T(n, false);
    for(int i=0; i<n-1; i++){
        int u, v; cin >> u >> v;
        T.add_edge(u-1, v-1);
    }

    vector<mint> dp(n, 1);
    mint ans = 0;
    function<void(int, int)> dfs = [&](int v, int p){
        mint sum = 0, sum2 = 0;
        for(auto e : T[v]) if(e.to != p){
            dfs(e.to, v);
            sum += dp[e.to];
            sum2 += dp[e.to]*dp[e.to];
            ans += mint(a[v])*dp[e.to];
        }

        dp[v] = (sum + mint(1))*mint(a[v]);

        sum = sum*sum - sum2;
        ans += sum*mint(a[v])/mint(2);
    }; dfs(0, -1);

    cout << ans << '\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0