結果

問題 No.1333 Squared Sum
ユーザー kwm_tkwm_t
提出日時 2021-09-23 15:50:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,576 bytes
コンパイル時間 5,557 ms
コンパイル使用メモリ 231,260 KB
実行使用メモリ 40,644 KB
最終ジャッジ日時 2023-09-18 19:54:11
合計ジャッジ時間 14,695 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,284 KB
testcase_01 AC 8 ms
12,404 KB
testcase_02 AC 7 ms
12,224 KB
testcase_03 AC 235 ms
19,712 KB
testcase_04 AC 239 ms
19,584 KB
testcase_05 AC 233 ms
19,648 KB
testcase_06 AC 237 ms
19,620 KB
testcase_07 AC 237 ms
19,660 KB
testcase_08 AC 235 ms
19,656 KB
testcase_09 AC 243 ms
19,708 KB
testcase_10 AC 240 ms
19,656 KB
testcase_11 AC 240 ms
19,652 KB
testcase_12 AC 239 ms
19,708 KB
testcase_13 AC 206 ms
40,492 KB
testcase_14 AC 269 ms
29,668 KB
testcase_15 AC 268 ms
37,924 KB
testcase_16 AC 8 ms
12,512 KB
testcase_17 AC 8 ms
12,448 KB
testcase_18 AC 8 ms
12,520 KB
testcase_19 AC 8 ms
12,544 KB
testcase_20 AC 8 ms
12,500 KB
testcase_21 AC 8 ms
12,444 KB
testcase_22 AC 8 ms
12,588 KB
testcase_23 AC 8 ms
12,492 KB
testcase_24 AC 7 ms
12,496 KB
testcase_25 AC 8 ms
12,460 KB
testcase_26 AC 268 ms
40,112 KB
testcase_27 AC 263 ms
30,200 KB
testcase_28 AC 271 ms
39,744 KB
testcase_29 AC 200 ms
40,644 KB
testcase_30 AC 68 ms
15,400 KB
testcase_31 AC 41 ms
14,084 KB
testcase_32 AC 120 ms
16,808 KB
testcase_33 AC 87 ms
15,960 KB
testcase_34 AC 183 ms
18,356 KB
testcase_35 AC 121 ms
16,732 KB
testcase_36 AC 59 ms
15,144 KB
testcase_37 AC 66 ms
15,084 KB
testcase_38 AC 81 ms
15,740 KB
testcase_39 AC 147 ms
17,556 KB
testcase_40 AC 156 ms
20,284 KB
testcase_41 AC 150 ms
20,296 KB
testcase_42 AC 158 ms
20,424 KB
testcase_43 AC 146 ms
20,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
//const bool debug = false;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,long long>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
struct Edge {
	int to;
	mint cost;
	Edge(int _to, mint _cost) :to(_to), cost(_cost) {}
};
struct Data {
	mint sump;
	mint sum;
	mint count;
	Data() :sump(0), sum(0), count(0) {}
	Data(mint _sump, mint _sum, mint _count) :sump(_sump), sum(_sum), count(_count) {}
};
vector<Edge>g[200005];
vector<Data>dp(200005);
vector<Data>dpR(200005);
void dfs(int v, int p = -1) {
	Data val = { 0,0,0 };
	for (auto e : g[v]) {
		if (p == e.to)continue;
		dfs(e.to, v);
		val.sump += dp[e.to].sump + dp[e.to].sum * e.cost * 2 + e.cost * e.cost * (dp[e.to].count + 1);
		val.sum += dp[e.to].sum + e.cost * (dp[e.to].count + 1);
		val.count += dp[e.to].count + 1;
	}
	dp[v] = val;
}
void dfsR(int v, int p = -1, mint cost = 0) {
	dpR[v] = dp[v];
	if (-1 != p) {
		Data sub = dpR[p];
		sub.sump -= dp[v].sump + dp[v].sum * cost * 2 + cost * cost * (dp[v].count + 1);
		sub.sum -= dp[v].sum + cost * (dp[v].count + 1);
		sub.count -= dp[v].count + 1;

		dpR[v].sump += sub.sump + sub.sum * cost * 2 + cost * cost * (sub.count + 1);
		dpR[v].sum += sub.sum + cost * (sub.count + 1);
		dpR[v].count += sub.count + 1;
	}
	for (auto e : g[v]) {
		if (p == e.to)continue;
		dfsR(e.to, v, e.cost);
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	rep(i, n - 1) {
		int a, b, c; cin >> a >> b >> c; a--; b--;
		g[a].push_back({ b,c });
		g[b].push_back({ a,c });
	}
	dfs(0);
	//rep(i, n)cout << dp[i].sump.val() << " " << dp[i].sum.val() << " " << dp[i].count.val() << endl;
	dfsR(0);
	//rep(i, n)cout << dpR[i].sump.val() << " " << dpR[i].sum.val() << " " << dpR[i].count.val() << endl;
	mint ans = 0;
	rep(i, n)ans += dpR[i].sump;
	ans *= inv_mod(2, mod);
	cout << ans.val() << endl;
	return 0;
}
0