結果

問題 No.1333 Squared Sum
ユーザー 👑 NachiaNachia
提出日時 2021-01-08 22:14:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 203,640 KB
実行使用メモリ 27,184 KB
最終ジャッジ日時 2023-08-10 10:03:22
合計ジャッジ時間 10,411 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,928 KB
testcase_01 AC 4 ms
11,916 KB
testcase_02 AC 4 ms
12,008 KB
testcase_03 AC 207 ms
26,804 KB
testcase_04 AC 211 ms
26,628 KB
testcase_05 AC 197 ms
26,640 KB
testcase_06 AC 193 ms
26,792 KB
testcase_07 AC 204 ms
26,660 KB
testcase_08 AC 186 ms
26,656 KB
testcase_09 AC 213 ms
26,760 KB
testcase_10 AC 214 ms
26,636 KB
testcase_11 AC 229 ms
26,912 KB
testcase_12 AC 219 ms
26,572 KB
testcase_13 AC 177 ms
26,920 KB
testcase_14 AC 204 ms
26,944 KB
testcase_15 AC 216 ms
26,892 KB
testcase_16 AC 4 ms
11,916 KB
testcase_17 AC 4 ms
11,796 KB
testcase_18 AC 4 ms
12,084 KB
testcase_19 AC 4 ms
11,988 KB
testcase_20 AC 4 ms
11,780 KB
testcase_21 AC 4 ms
11,876 KB
testcase_22 AC 3 ms
11,924 KB
testcase_23 AC 3 ms
11,864 KB
testcase_24 AC 4 ms
11,848 KB
testcase_25 AC 4 ms
11,856 KB
testcase_26 AC 204 ms
27,032 KB
testcase_27 AC 238 ms
26,996 KB
testcase_28 AC 224 ms
27,056 KB
testcase_29 AC 180 ms
27,184 KB
testcase_30 AC 69 ms
18,272 KB
testcase_31 AC 37 ms
15,428 KB
testcase_32 AC 98 ms
21,136 KB
testcase_33 AC 72 ms
19,164 KB
testcase_34 AC 147 ms
24,216 KB
testcase_35 AC 97 ms
21,120 KB
testcase_36 AC 55 ms
17,436 KB
testcase_37 AC 58 ms
17,572 KB
testcase_38 AC 69 ms
18,532 KB
testcase_39 AC 120 ms
22,352 KB
testcase_40 AC 150 ms
26,848 KB
testcase_41 AC 150 ms
26,872 KB
testcase_42 AC 145 ms
27,068 KB
testcase_43 AC 135 ms
27,080 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:38:23: 警告: 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: 警告: 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