結果

問題 No.1333 Squared Sum
ユーザー 👑 NachiaNachia
提出日時 2021-01-09 12:32:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 208,540 KB
実行使用メモリ 27,492 KB
最終ジャッジ日時 2024-04-28 19:56:15
合計ジャッジ時間 10,541 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
16,148 KB
testcase_01 AC 6 ms
16,148 KB
testcase_02 AC 7 ms
16,152 KB
testcase_03 AC 214 ms
27,216 KB
testcase_04 AC 203 ms
27,220 KB
testcase_05 AC 208 ms
27,088 KB
testcase_06 AC 207 ms
27,092 KB
testcase_07 AC 209 ms
27,216 KB
testcase_08 AC 213 ms
27,216 KB
testcase_09 AC 209 ms
27,088 KB
testcase_10 AC 216 ms
27,212 KB
testcase_11 AC 211 ms
27,092 KB
testcase_12 AC 210 ms
27,092 KB
testcase_13 AC 174 ms
27,480 KB
testcase_14 AC 226 ms
27,360 KB
testcase_15 AC 226 ms
27,356 KB
testcase_16 AC 7 ms
16,152 KB
testcase_17 AC 6 ms
16,152 KB
testcase_18 AC 6 ms
16,156 KB
testcase_19 AC 6 ms
16,152 KB
testcase_20 AC 6 ms
16,284 KB
testcase_21 AC 6 ms
16,148 KB
testcase_22 AC 6 ms
16,148 KB
testcase_23 AC 7 ms
16,152 KB
testcase_24 AC 6 ms
16,280 KB
testcase_25 AC 6 ms
16,276 KB
testcase_26 AC 235 ms
27,492 KB
testcase_27 AC 248 ms
27,484 KB
testcase_28 AC 235 ms
27,376 KB
testcase_29 AC 168 ms
27,356 KB
testcase_30 AC 72 ms
20,636 KB
testcase_31 AC 37 ms
18,696 KB
testcase_32 AC 103 ms
22,680 KB
testcase_33 AC 79 ms
21,400 KB
testcase_34 AC 153 ms
25,116 KB
testcase_35 AC 110 ms
22,696 KB
testcase_36 AC 60 ms
20,096 KB
testcase_37 AC 58 ms
20,244 KB
testcase_38 AC 71 ms
20,868 KB
testcase_39 AC 137 ms
24,064 KB
testcase_40 AC 151 ms
26,960 KB
testcase_41 AC 149 ms
27,192 KB
testcase_42 AC 149 ms
27,080 KB
testcase_43 AC 145 ms
27,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const ULL M=1000000007;

struct MLL{
	ULL v;
	MLL(ULL a=0){ v=a; }
	void operator+=(MLL r){ v+=r.v; if(v>=M) v-=M; }
	MLL operator+(MLL r) const { r+=*this; return r; }
	void operator*=(MLL r){ v=v*r.v%M; }
	MLL operator*(MLL r) const { r*=*this; return r; }
};

struct Mat{
  MLL S=0,L=0,C=0;
  Mat add_D0() const { return Mat{S,L,C+1}; }
  Mat extend(MLL d) const { return Mat{S+L*2*d+d*d*C,L+C*d,C}; }
};

MLL getSqSum(Mat l,Mat r){ return l.S*r.C + l.L*2*r.L + r.S*l.C; }
Mat operator+(Mat l,Mat r){ return { l.S+r.S, l.L+r.L, l.C+r.C }; }

struct Edge{ int to; MLL d; };

int N;
vector<Edge> E[200000];
Edge P[200000]={};
vector<int> I;
Mat V[200000];
MLL ans=0;

int main(){
  scanf("%d",&N);
  rep(i,N-1){
    int u,v,d; scanf("%d%d%d",&u,&v,&d); u--; v--;
    E[u].push_back({v,ULL(d)});
    E[v].push_back({u,ULL(d)});
  }
  int pI=0;
  P[1]={-1,0}; I.push_back(1);
  while(I.size()>pI){
    int p=I[pI++];
    for(Edge e:E[p]) if(P[p].to!=e.to){ P[e.to]={p,e.d}; I.push_back(e.to); }
  }
  reverse(I.begin(),I.end());
  I.pop_back();

  for(int p:I){
    V[p] = V[p].add_D0();
    V[p] = V[p].extend(P[p].d);
    ans += getSqSum(V[P[p].to],V[p]);
    ans += V[p].S;
    V[P[p].to] = V[P[p].to] + V[p];
  }

  printf("%llu\n",ans.v);

  return 0;
}
0