結果

問題 No.1333 Squared Sum
ユーザー fumofumofunifumofumofuni
提出日時 2021-01-09 14:22:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 702 ms / 2,000 ms
コード長 4,188 bytes
コンパイル時間 2,825 ms
コンパイル使用メモリ 215,284 KB
実行使用メモリ 193,280 KB
最終ジャッジ日時 2024-04-28 21:41:34
合計ジャッジ時間 20,128 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 541 ms
39,296 KB
testcase_04 AC 495 ms
39,296 KB
testcase_05 AC 495 ms
39,424 KB
testcase_06 AC 491 ms
39,296 KB
testcase_07 AC 497 ms
39,224 KB
testcase_08 AC 499 ms
39,296 KB
testcase_09 AC 489 ms
39,424 KB
testcase_10 AC 485 ms
39,296 KB
testcase_11 AC 490 ms
39,332 KB
testcase_12 AC 483 ms
39,424 KB
testcase_13 AC 569 ms
193,224 KB
testcase_14 AC 640 ms
117,632 KB
testcase_15 AC 692 ms
174,848 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 702 ms
189,312 KB
testcase_27 AC 645 ms
121,728 KB
testcase_28 AC 691 ms
187,892 KB
testcase_29 AC 559 ms
193,280 KB
testcase_30 AC 180 ms
18,560 KB
testcase_31 AC 93 ms
11,904 KB
testcase_32 AC 286 ms
25,204 KB
testcase_33 AC 230 ms
20,608 KB
testcase_34 AC 398 ms
32,256 KB
testcase_35 AC 286 ms
25,316 KB
testcase_36 AC 148 ms
16,384 KB
testcase_37 AC 165 ms
16,896 KB
testcase_38 AC 182 ms
19,200 KB
testcase_39 AC 308 ms
28,160 KB
testcase_40 AC 405 ms
46,916 KB
testcase_41 AC 407 ms
47,040 KB
testcase_42 AC 411 ms
46,912 KB
testcase_43 AC 386 ms
46,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={-1,0,1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}



const int mod = 1000000007;
const int max_n = 200005;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
  bool operator==(const mint &p) const { return x == p.x; }
  bool operator!=(const mint &p) const { return x != p.x; }
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}


struct Edge{
    ll to,weight;
};
template<class T>
class Rerooting{
    public:
    int n;
    T def;
    function<T(T,T)> merge;
    function<T(T,ll)> addroot;
    vector<vector<Edge>> g;
    vector<vector<T>> dp;
    public:
    vector<T> ans;
    Rerooting(int _n,T _def,function<T(T,T)> _merge,function<T(T,ll)> _addroot)
    :n(_n),def(_def),merge(_merge),addroot(_addroot)
    {
        g=vector<vector<Edge>>(n);
        dp=vector<vector<T>>(n);
        ans=vector<T>(n,def);
    }
    void add_edge(int a,Edge e){
        g[a].pb(e);
    }

    T dfs(int v=0,int par=-1){
        T ret=def;
        dp[v].resize(g[v].size());
        rep(i,g[v].size()){
            if(g[v][i].to==par){
              continue;
            }
            dp[v][i]=addroot(dfs(g[v][i].to,v),g[v][i].weight);
            ret=merge(ret,dp[v][i]);
        }
        return ret;
    }

    void bfs(int v,T val,int par=-1){
        int deg=g[v].size();
        rep(i,deg){
            if(g[v][i].to==par)dp[v][i]=val;
        }
        vector<T> dpl(deg+1,def),dpr(deg+1,def);
        rep(i,deg)dpl[i+1]=merge(dpl[i],dp[v][i]);
        per(i,deg)dpr[i]=merge(dpr[i+1],dp[v][i]);
        ans[v]=dpl[deg];
        rep(i,deg){
            int nv=g[v][i].to;
            if(nv==par)continue;
            bfs(nv,addroot(merge(dpl[i],dpr[i+1]),g[v][i].weight),v);
        }
    }
};

struct val{
    mint sq,sum,len;
};
int main(){
    ll n;cin >> n;
    auto mg=[](val a,val b){
        a.sq+=b.sq;a.sum+=b.sum;a.len+=b.len;
        return a;
    };
    auto addroot=[](val a,ll b){
        a.sq+=a.sum*2*b+(a.len+1)*b*b;
        a.sum+=(a.len+1)*b;
        a.len+=1;
        return a;
    };
    val def={0,0,0};
    Rerooting<val> g(n,def,mg,addroot);
    rep(i,n-1){
        ll a,b,w;cin >> a >> b >> w;a--;b--;
        g.add_edge(a,{b,w});
        g.add_edge(b,{a,w});
    }
    g.dfs(0);
    g.bfs(0,def);
    mint ans=0;
    rep(i,n)ans+=g.ans[i].sq;
    cout << ans/2 <<endl;
}  
0