結果

問題 No.1333 Squared Sum
ユーザー leaf_1415leaf_1415
提出日時 2021-01-08 22:39:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,450 bytes
コンパイル時間 1,298 ms
コンパイル使用メモリ 85,672 KB
実行使用メモリ 35,012 KB
最終ジャッジ日時 2024-04-28 04:07:51
合計ジャッジ時間 18,615 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,008 KB
testcase_01 AC 4 ms
8,004 KB
testcase_02 AC 4 ms
8,132 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 3 ms
7,984 KB
testcase_17 AC 5 ms
8,136 KB
testcase_18 AC 3 ms
8,016 KB
testcase_19 AC 3 ms
8,148 KB
testcase_20 AC 4 ms
8,260 KB
testcase_21 AC 4 ms
8,516 KB
testcase_22 AC 3 ms
8,132 KB
testcase_23 AC 3 ms
8,128 KB
testcase_24 AC 4 ms
8,520 KB
testcase_25 AC 4 ms
8,164 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 287 ms
18,224 KB
testcase_41 AC 279 ms
18,156 KB
testcase_42 AC 286 ms
18,524 KB
testcase_43 AC 264 ms
19,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18
#define mod 1000000007

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){
		to = a, cost = b;
	}
};

int n;
vector<edge> G[200005];
int size[200005];
bool used[200005];

int sizedfs(int v, int pre)
{
	int ret = 1;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].to == pre) continue;
		if(used[G[v][i].to]) continue;
		ret += sizedfs(G[v][i].to, v);
	}
	return size[v] = ret;
}

int centdfs(int v, int pre, int sz)
{
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i].to == pre) continue;
		if(used[G[v][i].to]) continue;
		if(size[G[v][i].to] > sz/2) return centdfs(G[v][i].to, v, sz);
	}
	return v;
}

ll ans = 0;
ll num, sum, sum2;
ll gnum, gsum, gsum2;

void dfs(int v, int p, ll d)
{
	num++, sum += d, sum2 += d*d%mod;
	for(auto e : G[v]){
		ll u = e.to;
		if(u == p || used[u]) continue;
		dfs(u, v, d+e.cost);
	}
}

void solve(int v)
{
	sizedfs(v, -1);
	v = centdfs(v, -1, size[v]);
	
	gnum = gsum = gsum2 = 0;
	for(auto e : G[v]){
		ll u = e.to;
		if(used[u]) continue;
		
		num = sum = sum2 = 0;
		dfs(u, v, e.cost);
		
		sum %= mod, sum2 %= mod;
		gnum += num, (gsum += sum) %= mod, (gsum2 += sum2) %= mod;
		
		ans += mod - num * sum2 * 2 % mod, ans %= mod;
		ans += mod - sum * sum * 2 % mod, ans %= mod;
	}
	//cout << ans << endl;
	//cout << gnum << " " << gsum << " " << gsum2 << endl;
	
	ans += gnum * gsum2 * 2 % mod, ans %= mod;
	ans += gsum * gsum * 2 % mod, ans %= mod;
	ans += 2 * gsum2 % mod, ans %= mod;
	
	//cout << v << " " << ans << endl;
	
	used[v] = true;
	for(int i = 0; i < G[v].size(); i++){
		if(used[G[v][i].to]) continue;
		solve(G[v][i].to);
	}
}

int main(void)
{
	cin >> n;
	llint u, v, w;
	for(int i = 0; i < n-1; i++){
		cin >> u >> v >> w;
		G[u].push_back(edge(v, w));
		G[v].push_back(edge(u, w));
	}
	
	solve(1);
	
	ans *= (mod+1)/2, ans %= mod;
	cout << ans << endl;
	
	return 0;
}
0