結果

問題 No.1333 Squared Sum
ユーザー kwm_tkwm_t
提出日時 2021-09-24 01:02:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 2,739 bytes
コンパイル時間 5,508 ms
コンパイル使用メモリ 236,756 KB
実行使用メモリ 37,452 KB
最終ジャッジ日時 2023-09-18 20:06:24
合計ジャッジ時間 13,484 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 223 ms
20,040 KB
testcase_04 AC 207 ms
19,600 KB
testcase_05 AC 211 ms
19,824 KB
testcase_06 AC 224 ms
19,908 KB
testcase_07 AC 227 ms
20,040 KB
testcase_08 AC 226 ms
19,796 KB
testcase_09 AC 224 ms
19,856 KB
testcase_10 AC 220 ms
19,908 KB
testcase_11 AC 225 ms
19,632 KB
testcase_12 AC 227 ms
19,584 KB
testcase_13 AC 196 ms
37,344 KB
testcase_14 AC 251 ms
28,272 KB
testcase_15 AC 250 ms
35,148 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 256 ms
36,996 KB
testcase_27 AC 258 ms
28,764 KB
testcase_28 AC 268 ms
36,688 KB
testcase_29 AC 194 ms
37,452 KB
testcase_30 AC 66 ms
10,160 KB
testcase_31 AC 34 ms
7,100 KB
testcase_32 AC 109 ms
13,164 KB
testcase_33 AC 77 ms
10,952 KB
testcase_34 AC 168 ms
16,468 KB
testcase_35 AC 105 ms
13,244 KB
testcase_36 AC 53 ms
9,140 KB
testcase_37 AC 55 ms
9,500 KB
testcase_38 AC 66 ms
10,464 KB
testcase_39 AC 129 ms
14,636 KB
testcase_40 AC 149 ms
20,328 KB
testcase_41 AC 144 ms
20,324 KB
testcase_42 AC 146 ms
20,464 KB
testcase_43 AC 132 ms
20,472 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 ReRooting {
	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<vector<Edge>>g;
	vector<Data>dp;
	vector<Data>dpR;
	int sz;
	ReRooting(int _sz) :sz(_sz) {
		g.resize(sz);
		dp.reserve(sz);
		dpR.resize(sz);
	}
	void ReadGraph() {//★★
		rep(i, sz - 1) {
			int a, b, c; cin >> a >> b >> c; a--; b--;
			g[a].push_back({ b,c });
			g[b].push_back({ a,c });
		}
	}
	Data merge(const Data &lh, const Data &rh, mint cost) {//★★
		Data ret = lh;
		ret.sump += rh.sump + rh.sum * cost * 2 + cost * cost * (rh.count + 1);
		ret.sum += rh.sum + cost * (rh.count + 1);
		ret.count += rh.count + 1;
		return ret;
	}
	Data mergeR(const Data &lh, const Data &rh, mint cost) {//★★
		Data ret = lh;
		ret.sump -= rh.sump + rh.sum * cost * 2 + cost * cost * (rh.count + 1);
		ret.sum -= rh.sum + cost * (rh.count + 1);
		ret.count -= rh.count + 1;
		return ret;
	}
	void dfs(int v, int p = -1) {
		Data val;
		for (auto e : g[v]) {
			if (p == e.to)continue;
			dfs(e.to, v);
			val = merge(val, dp[e.to], e.cost);
		}
		dp[v] = val;
	}
	void dfsR(int v, int p = -1, mint cost = 0) {
		dpR[v] = dp[v];
		if (-1 != p) {
			Data sub = mergeR(dpR[p], dp[v], cost);
			dpR[v] = merge(dpR[v], sub, cost);
		}
		for (auto e : g[v]) {
			if (p == e.to)continue;
			dfsR(e.to, v, e.cost);
		}
	}
	void output() {//★★
		mint ans = 0;
		rep(i, sz)ans += dpR[i].sump;
		ans *= inv_mod(2, mod);
		cout << ans.val() << endl;
	}
	void solve() {
		ReadGraph();
		dfs(0);
		dfsR(0);
		output();
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	ReRooting reroot(n);
	reroot.solve();
	return 0;
}
0