結果

問題 No.1333 Squared Sum
ユーザー 👑 NachiaNachia
提出日時 2021-01-09 12:37:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 1,053 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 206,352 KB
実行使用メモリ 27,176 KB
最終ジャッジ日時 2024-04-28 20:00:39
合計ジャッジ時間 13,589 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,192 KB
testcase_01 AC 5 ms
8,192 KB
testcase_02 AC 5 ms
8,192 KB
testcase_03 AC 381 ms
26,756 KB
testcase_04 AC 388 ms
26,728 KB
testcase_05 AC 387 ms
26,756 KB
testcase_06 AC 372 ms
26,884 KB
testcase_07 AC 372 ms
26,740 KB
testcase_08 AC 372 ms
26,820 KB
testcase_09 AC 377 ms
26,864 KB
testcase_10 AC 370 ms
26,728 KB
testcase_11 AC 369 ms
26,796 KB
testcase_12 AC 376 ms
26,780 KB
testcase_13 AC 340 ms
27,100 KB
testcase_14 AC 394 ms
27,028 KB
testcase_15 AC 392 ms
27,028 KB
testcase_16 AC 4 ms
8,192 KB
testcase_17 AC 4 ms
8,064 KB
testcase_18 AC 4 ms
8,192 KB
testcase_19 AC 5 ms
8,320 KB
testcase_20 AC 4 ms
8,192 KB
testcase_21 AC 4 ms
8,192 KB
testcase_22 AC 5 ms
8,192 KB
testcase_23 AC 5 ms
8,320 KB
testcase_24 AC 5 ms
8,192 KB
testcase_25 AC 5 ms
8,320 KB
testcase_26 AC 383 ms
27,032 KB
testcase_27 AC 387 ms
27,048 KB
testcase_28 AC 388 ms
27,172 KB
testcase_29 AC 327 ms
27,076 KB
testcase_30 AC 132 ms
15,868 KB
testcase_31 AC 73 ms
12,544 KB
testcase_32 AC 211 ms
19,456 KB
testcase_33 AC 155 ms
17,024 KB
testcase_34 AC 280 ms
23,412 KB
testcase_35 AC 196 ms
19,476 KB
testcase_36 AC 116 ms
14,984 KB
testcase_37 AC 113 ms
15,280 KB
testcase_38 AC 133 ms
16,472 KB
testcase_39 AC 228 ms
21,104 KB
testcase_40 AC 304 ms
27,176 KB
testcase_41 AC 293 ms
26,872 KB
testcase_42 AC 297 ms
26,864 KB
testcase_43 AC 260 ms
27,048 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:31:35: warning: narrowing conversion of 'd' from 'int' to 'ULL' {aka 'long long unsigned int'} [-Wnarrowing]
   31 |                 E[u].push_back({v,d});
      |                                   ^
main.cpp:32:35: warning: narrowing conversion of 'd' from 'int' to 'ULL' {aka 'long long unsigned int'} [-Wnarrowing]
   32 |                 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;
    void ad(){ *this={S,L,(C+1)%M}; }
    void ex(ULL d){ *this={(S+L*2*d%M+d*d%M*C)%M,(L+C*d)%M,C}; }
};

ULL sqsum(Mat l,Mat r){ return (l.S*r.C+l.L*2*r.L+r.S*l.C)%M; }
Mat mg(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(){
	cin>>N;
	rep(i,N-1){
		int u,v,d; cin>>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].ad();
		V[p].ex(P[p].d);
		ans += sqsum(V[P[p].to],V[p]);
		ans += V[p].S;
		V[P[p].to] = mg(V[P[p].to],V[p]);
	}
	cout<<ans%M<<endl;
	return 0;
}
0