結果

問題 No.1333 Squared Sum
ユーザー kotatsugamekotatsugame
提出日時 2021-01-08 22:22:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 2,875 bytes
コンパイル時間 840 ms
コンパイル使用メモリ 75,188 KB
実行使用メモリ 40,736 KB
最終ジャッジ日時 2023-08-10 10:16:45
合計ジャッジ時間 11,523 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,444 KB
testcase_01 AC 4 ms
9,444 KB
testcase_02 AC 5 ms
9,440 KB
testcase_03 AC 324 ms
16,816 KB
testcase_04 AC 315 ms
16,860 KB
testcase_05 AC 320 ms
16,816 KB
testcase_06 AC 328 ms
16,804 KB
testcase_07 AC 325 ms
16,800 KB
testcase_08 AC 325 ms
17,084 KB
testcase_09 AC 319 ms
16,824 KB
testcase_10 AC 316 ms
16,760 KB
testcase_11 AC 315 ms
16,756 KB
testcase_12 AC 316 ms
16,864 KB
testcase_13 AC 322 ms
40,736 KB
testcase_14 AC 346 ms
28,400 KB
testcase_15 AC 354 ms
37,784 KB
testcase_16 AC 4 ms
9,444 KB
testcase_17 AC 5 ms
9,520 KB
testcase_18 AC 5 ms
9,620 KB
testcase_19 AC 5 ms
9,580 KB
testcase_20 AC 4 ms
9,536 KB
testcase_21 AC 5 ms
9,540 KB
testcase_22 AC 4 ms
9,440 KB
testcase_23 AC 4 ms
9,424 KB
testcase_24 AC 4 ms
9,436 KB
testcase_25 AC 4 ms
9,728 KB
testcase_26 AC 345 ms
40,076 KB
testcase_27 AC 338 ms
29,096 KB
testcase_28 AC 343 ms
39,876 KB
testcase_29 AC 311 ms
40,696 KB
testcase_30 AC 119 ms
12,676 KB
testcase_31 AC 65 ms
11,316 KB
testcase_32 AC 182 ms
13,896 KB
testcase_33 AC 138 ms
13,276 KB
testcase_34 AC 252 ms
15,384 KB
testcase_35 AC 183 ms
14,004 KB
testcase_36 AC 100 ms
12,184 KB
testcase_37 AC 106 ms
12,216 KB
testcase_38 AC 130 ms
12,724 KB
testcase_39 AC 213 ms
14,496 KB
testcase_40 AC 261 ms
20,032 KB
testcase_41 AC 259 ms
20,080 KB
testcase_42 AC 261 ms
19,884 KB
testcase_43 AC 232 ms
20,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:31:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<vector>
using namespace std;
#line 3 "/home/kotatsugame/library/math/modint.cpp"
#include<utility>
template<int m>
struct modint{
	unsigned int x;
	constexpr modint()noexcept:x(){}
	template<typename T>
	constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){}
	constexpr unsigned int val()const noexcept{return x;}
	constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;}
	constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;}
	constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;}
	constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;}
	constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;}
	constexpr modint&operator-=(const modint&a)noexcept{if(x<a.x)x+=m;x-=a.x;return*this;}
	constexpr modint&operator*=(const modint&a)noexcept{x=(unsigned long long)x*a.x%m;return*this;}
	constexpr modint&operator/=(const modint&a)noexcept{return*this*=a.inv();}
	constexpr modint operator+()const noexcept{return*this;}
	constexpr modint operator-()const noexcept{return modint()-*this;}
	constexpr modint pow(long long n)const noexcept
	{
		if(n<0)return pow(-n).inv();
		modint x=*this,r=1;
		for(;n;x*=x,n>>=1)if(n&1)r*=x;
		return r;
	}
	constexpr modint inv()const noexcept
	{
		int s=x,t=m,x=1,u=0;
		while(t)
		{
			int k=s/t;
			s-=k*t;
			swap(s,t);
			x-=k*u;
			swap(x,u);
		}
		return modint(x);
	}
	friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;}
	friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;}
	friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;}
	friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;}
	friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;}
	friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;}
	friend ostream&operator<<(ostream&os,const modint&a){return os<<a.x;}
	friend istream&operator>>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;}
};
#line 5 "a.cpp"
using mint=modint<(int)1e9+7>;
int N;
vector<pair<int,mint> >G[2<<17];
mint ans=0;
pair<mint,mint>dfs(int u,int p,mint d)
{
	vector<pair<mint,mint> >A;
	mint ch=1,ds=0;
	for(pair<int,mint>q:G[u])if(q.first!=p)
	{
		pair<mint,mint>r=dfs(q.first,u,d+q.second);
		A.push_back(r);
		ch+=r.first;
		ds+=r.second;
	}
	ans+=d*d*(N-2);
	for(pair<mint,mint>q:A)
	{
		ans+=2*(ch-q.first)*q.first*d*d;
		ans-=4*(ch-q.first)*d*q.second;
	}
	ans+=2*(ch-1)*d*d;
	ans-=4*(ch-1)*d*d;
	ds+=d;
	return make_pair(ch,ds);
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int u,v;mint w;cin>>u>>v>>w;
		u--,v--;
		G[u].push_back(make_pair(v,w));
		G[v].push_back(make_pair(u,w));
	}
	mint ad=dfs(0,0,mint(0)).second;
	ans+=ad*ad;
	cout<<ans<<endl;
}
0