結果

問題 No.1333 Squared Sum
ユーザー 👑 NachiaNachia
提出日時 2021-01-08 22:14:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 207,812 KB
実行使用メモリ 27,320 KB
最終ジャッジ日時 2024-04-28 03:28:34
合計ジャッジ時間 7,966 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,780 KB
testcase_01 AC 3 ms
11,076 KB
testcase_02 AC 4 ms
11,840 KB
testcase_03 AC 149 ms
26,864 KB
testcase_04 AC 149 ms
26,856 KB
testcase_05 AC 157 ms
26,876 KB
testcase_06 AC 158 ms
26,920 KB
testcase_07 AC 156 ms
26,896 KB
testcase_08 AC 153 ms
26,896 KB
testcase_09 AC 152 ms
26,980 KB
testcase_10 AC 153 ms
26,900 KB
testcase_11 AC 151 ms
26,884 KB
testcase_12 AC 151 ms
26,924 KB
testcase_13 AC 146 ms
27,136 KB
testcase_14 AC 163 ms
27,120 KB
testcase_15 AC 154 ms
27,120 KB
testcase_16 AC 5 ms
11,184 KB
testcase_17 AC 3 ms
10,952 KB
testcase_18 AC 4 ms
12,292 KB
testcase_19 AC 3 ms
11,588 KB
testcase_20 AC 4 ms
10,532 KB
testcase_21 AC 4 ms
11,880 KB
testcase_22 AC 3 ms
10,996 KB
testcase_23 AC 4 ms
11,732 KB
testcase_24 AC 4 ms
10,728 KB
testcase_25 AC 3 ms
10,756 KB
testcase_26 AC 158 ms
27,144 KB
testcase_27 AC 162 ms
27,120 KB
testcase_28 AC 159 ms
27,252 KB
testcase_29 AC 142 ms
27,140 KB
testcase_30 AC 59 ms
17,972 KB
testcase_31 AC 31 ms
14,356 KB
testcase_32 AC 78 ms
21,728 KB
testcase_33 AC 64 ms
20,396 KB
testcase_34 AC 115 ms
24,124 KB
testcase_35 AC 80 ms
21,184 KB
testcase_36 AC 47 ms
17,580 KB
testcase_37 AC 49 ms
18,188 KB
testcase_38 AC 55 ms
18,516 KB
testcase_39 AC 96 ms
22,096 KB
testcase_40 AC 128 ms
27,320 KB
testcase_41 AC 126 ms
27,044 KB
testcase_42 AC 132 ms
27,080 KB
testcase_43 AC 122 ms
27,064 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:38:23: warning: narrowing conversion of 'd' from 'int' to 'ULL' {aka 'long long unsigned int'} [-Wnarrowing]
   38 |     E[u].push_back({v,d});
      |                       ^
main.cpp:39:23: warning: narrowing conversion of 'd' from 'int' to 'ULL' {aka 'long long unsigned int'} [-Wnarrowing]
   39 |     E[v].push_back({u,d});
      |                       ^

ソースコード

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++)

ULL M = 1000000007;

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

ULL getSqSum(Mat l,Mat r){
  ULL res=0;
  res+= (l.S*r.C%M + l.L*2*r.L%M + r.S*l.C%M)%M;
  return res%M;
}

Mat operator+(Mat l,Mat r){
  return { (l.S+r.S)%M, (l.L+r.L)%M, (l.C+r.C)%M };
}

struct Edge{ int to; ULL d; };

int N;
vector<Edge> E[200000];
Edge P[200000]={};
vector<int> I;
Mat V[200000];
ULL 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,d});
    E[v].push_back({u,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%M);

  return 0;
}
0