結果

問題 No.1333 Squared Sum
ユーザー りあんりあん
提出日時 2021-01-08 23:23:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,850 bytes
コンパイル時間 2,237 ms
コンパイル使用メモリ 206,720 KB
実行使用メモリ 50,176 KB
最終ジャッジ日時 2024-04-28 05:40:34
合計ジャッジ時間 17,115 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
15,104 KB
testcase_01 AC 7 ms
15,104 KB
testcase_02 AC 7 ms
15,104 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 WA -
testcase_17 WA -
testcase_18 AC 6 ms
15,232 KB
testcase_19 AC 7 ms
15,104 KB
testcase_20 WA -
testcase_21 AC 6 ms
15,232 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 7 ms
15,232 KB
testcase_25 AC 7 ms
15,232 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 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 272 ms
26,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://atcoder.jp/contests/otemae2019/submissions/6651746

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int N;
vector<pair<int, int>> G[500000];

ll ans = 0;

int subnum[500000];
int calc_subnum(int v, int from) {
	int ret = 1;
	for (int i = 0; i < G[v].size(); i++){
		int to = G[v][i].second;
		if (to == from)continue;
		ret += calc_subnum(to, v);
	}
	return subnum[v] = ret;
}

//eを通るu-v pathの個数
void calc1(int v, int from, int l) {
	ans += (ll)subnum[v] * (N - subnum[v]) % mod * l % mod * l;
	ans %= mod;
	for (int i = 0; i < G[v].size(); i++) {
		int to = G[v][i].second;
		if (to == from)continue;
		calc1(to, v, G[v][i].first);
	}
}
//根とe'の間にe
ll calc2(int v, int from) {
	ll ret = 0;
	for (int i = 0; i < G[v].size(); i++) {
		int to = G[v][i].second;
		if (to == from)continue;
		ll next = calc2(to, v);
		ans += next * (N - subnum[to]) % mod * G[v][i].first; ans %= mod;
		ret += next; ret += subnum[to] * (ll)G[v][i].first; ret %= mod;
	}
	return ret;
}//eとe'がバラバラ
ll calc3(int v, int from) {
	ll ret = 0;
	vector<ll> x;
	for (int i = 0; i < G[v].size(); i++) {
		int to = G[v][i].second;
		if (to == from)continue;
		ll next = calc3(to, v) + subnum[to] * (ll)G[v][i].first;
		x.push_back(next); ret += next; ret %= mod;
	}
	ll sum = 0;
	for (int i = 0; i < x.size(); i++) {
		sum += x[i];
	}
	sum %= mod;
	for (int i = 0; i < x.size(); i++) {
		ans += x[i] * (sum - x[i] + mod); ans %= mod;
	}
	return ret;
}
int main() {
	cin >> N;
	//入力
	for (int i = 0; i < N - 1; i++) {
		int A, B, C; cin >> A >> B >> C; A--; B--;
		G[A].push_back({C, B});
		G[B].push_back({C, A});
	}

	calc_subnum(0, -1);
	calc1(0, -1, 0);
	calc2(0, -1);
	//eとe'の上下を入れ替えてもう一度
	calc2(0, -1);
	calc3(0, -1);

	cout << ans << endl;
	return 0;
}
0