結果

問題 No.1333 Squared Sum
ユーザー penguinmanpenguinman
提出日時 2020-11-03 07:45:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 2,000 ms
コード長 1,898 bytes
コンパイル時間 2,196 ms
コンパイル使用メモリ 179,096 KB
実行使用メモリ 54,576 KB
最終ジャッジ日時 2023-09-29 14:24:52
合計ジャッジ時間 10,840 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 280 ms
29,712 KB
testcase_04 AC 283 ms
29,708 KB
testcase_05 AC 281 ms
29,736 KB
testcase_06 AC 282 ms
29,696 KB
testcase_07 AC 286 ms
29,668 KB
testcase_08 AC 283 ms
29,668 KB
testcase_09 AC 283 ms
29,844 KB
testcase_10 AC 290 ms
29,808 KB
testcase_11 AC 289 ms
29,732 KB
testcase_12 AC 280 ms
29,736 KB
testcase_13 AC 250 ms
54,576 KB
testcase_14 AC 323 ms
42,128 KB
testcase_15 AC 309 ms
51,552 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 306 ms
53,836 KB
testcase_27 AC 319 ms
42,936 KB
testcase_28 AC 303 ms
53,816 KB
testcase_29 AC 231 ms
54,572 KB
testcase_30 AC 78 ms
14,384 KB
testcase_31 AC 41 ms
9,280 KB
testcase_32 AC 134 ms
19,288 KB
testcase_33 AC 90 ms
15,916 KB
testcase_34 AC 173 ms
24,600 KB
testcase_35 AC 118 ms
19,292 KB
testcase_36 AC 65 ms
12,716 KB
testcase_37 AC 69 ms
12,980 KB
testcase_38 AC 81 ms
14,856 KB
testcase_39 AC 159 ms
21,420 KB
testcase_40 AC 208 ms
31,148 KB
testcase_41 AC 212 ms
31,148 KB
testcase_42 AC 215 ms
31,160 KB
testcase_43 AC 202 ms
31,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using std::cin;
using std::cout;
#define endl "\n"
using std::vector;
using ll=long long;
//宣言、modpow
const int mod=2e9+14;
vector<vector<int>> edge,weight;
vector<ll> dp,subtree,sum;
vector<bool> flag;
ll ans=0;
//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(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    //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/=2;
    cout<<ans<<endl;
}
0