結果

問題 No.1333 Squared Sum
ユーザー penguinmanpenguinman
提出日時 2020-10-30 04:26:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 549 ms / 2,000 ms
コード長 2,046 bytes
コンパイル時間 3,741 ms
コンパイル使用メモリ 179,224 KB
実行使用メモリ 54,544 KB
最終ジャッジ日時 2023-09-29 04:15:50
合計ジャッジ時間 18,964 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 511 ms
29,768 KB
testcase_04 AC 509 ms
29,908 KB
testcase_05 AC 501 ms
29,696 KB
testcase_06 AC 506 ms
29,840 KB
testcase_07 AC 502 ms
29,840 KB
testcase_08 AC 498 ms
29,700 KB
testcase_09 AC 497 ms
29,904 KB
testcase_10 AC 500 ms
29,844 KB
testcase_11 AC 504 ms
29,796 KB
testcase_12 AC 519 ms
29,740 KB
testcase_13 AC 438 ms
54,524 KB
testcase_14 AC 546 ms
42,192 KB
testcase_15 AC 549 ms
51,748 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 536 ms
53,932 KB
testcase_27 AC 534 ms
42,996 KB
testcase_28 AC 541 ms
53,552 KB
testcase_29 AC 440 ms
54,544 KB
testcase_30 AC 183 ms
14,316 KB
testcase_31 AC 96 ms
9,448 KB
testcase_32 AC 285 ms
19,124 KB
testcase_33 AC 214 ms
15,956 KB
testcase_34 AC 395 ms
24,624 KB
testcase_35 AC 288 ms
19,600 KB
testcase_36 AC 154 ms
12,744 KB
testcase_37 AC 164 ms
12,976 KB
testcase_38 AC 193 ms
15,140 KB
testcase_39 AC 330 ms
21,408 KB
testcase_40 AC 390 ms
31,244 KB
testcase_41 AC 389 ms
31,300 KB
testcase_42 AC 392 ms
31,252 KB
testcase_43 AC 362 ms
31,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using ll=long long;
//宣言、modpow
const int mod=1e9+7;
vector<vector<int>> edge,weight;
vector<ll> dp,subtree,sum;
vector<bool> flag;
ll ans=0;
ll modpow(ll x,ll y){
    ll ret=1;
    while(y){
        if(y&1){
            ret*=x;
            ret%=mod;
        }
        x*=x;
        x%=mod;
        y/=2;
    }
    return ret;
}
//dfs
void dfs(int now){
    flag[now]=1;
    for(int i=0;i<edge[now].size();i++){
        int next=edge[now][i];
        ll w=weight[now][i];
        if(flag[next]) continue;

        dfs(next);
        subtree[now]+=subtree[next];
        dp[now]+=dp[next]+subtree[next]*w%mod*w%mod+sum[next]*w%mod*2%mod;
        dp[now]%=mod;
        sum[now]+=sum[next]+w*subtree[next]%mod;
        sum[now]%=mod;
    }
}
//rerooting
void reroot(int now){
    flag[now]=0;
    ans+=dp[now];
    ans%=mod;
    for(int i=0;i<edge[now].size();i++){
        int next=edge[now][i];
        ll w=weight[now][i];
        if(!flag[next]) continue;

        ll dp2=dp[now]-dp[next]-subtree[next]*w%mod*w%mod-sum[next]*w%mod*2%mod;
        dp2%=mod;
        if(dp2<0) dp2+=mod;

        ll sum2=sum[now]-sum[next]-w*subtree[next]%mod;
        sum2%=mod;
        if(sum2<0) sum2+=mod;

        ll subtree2=subtree[now]-subtree[next];

        dp[next]+=dp2+subtree2*w%mod*w%mod+sum2*w%mod*2%mod;
        dp[next]%=mod;
        sum[next]+=sum2+w*subtree2%mod;
        sum[next]%=mod;
        subtree[next]+=subtree2;
        reroot(next);
    }
}
//main
int main(){
    //resize、input
    int N; cin>>N;
    edge.resize(N);
    weight.resize(N);
    dp.resize(N);
    subtree.resize(N,1);
    sum.resize(N);
    flag.resize(N);
    for(int i=1;i<N;i++){
        int x,y,z; cin>>x>>y>>z;
        edge[x-1].push_back(y-1);
        edge[y-1].push_back(x-1);
        weight[x-1].push_back(z);
        weight[y-1].push_back(z);
    }
    //求値、output
    dfs(0);
    reroot(0);
    ans*=modpow(2,mod-2);
    ans%=mod;
    cout<<ans<<endl;
}
0