結果
問題 | No.1333 Squared Sum |
ユーザー | 👑 Nachia |
提出日時 | 2021-01-08 22:14:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 198 ms / 2,000 ms |
コード長 | 1,215 bytes |
コンパイル時間 | 2,039 ms |
コンパイル使用メモリ | 198,904 KB |
最終ジャッジ日時 | 2025-01-17 12:26:10 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 44 |
コンパイルメッセージ
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}); | ^ main.cpp:35:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 35 | scanf("%d",&N); | ~~~~~^~~~~~~~~ main.cpp:37:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 37 | int u,v,d; scanf("%d%d%d",&u,&v,&d); u--; v--; | ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#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; }