結果

問題 No.1333 Squared Sum
ユーザー kwm_tkwm_t
提出日時 2021-09-24 02:04:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 2,600 bytes
コンパイル時間 3,902 ms
コンパイル使用メモリ 234,796 KB
実行使用メモリ 38,328 KB
最終ジャッジ日時 2023-09-18 20:07:16
合計ジャッジ時間 11,941 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 225 ms
17,412 KB
testcase_04 AC 224 ms
17,448 KB
testcase_05 AC 221 ms
17,532 KB
testcase_06 AC 207 ms
17,452 KB
testcase_07 AC 227 ms
17,524 KB
testcase_08 AC 211 ms
17,480 KB
testcase_09 AC 207 ms
17,452 KB
testcase_10 AC 217 ms
17,460 KB
testcase_11 AC 216 ms
17,604 KB
testcase_12 AC 215 ms
17,516 KB
testcase_13 AC 193 ms
38,328 KB
testcase_14 AC 252 ms
27,504 KB
testcase_15 AC 256 ms
35,428 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 263 ms
37,608 KB
testcase_27 AC 243 ms
27,960 KB
testcase_28 AC 257 ms
37,596 KB
testcase_29 AC 190 ms
38,116 KB
testcase_30 AC 62 ms
9,224 KB
testcase_31 AC 33 ms
6,456 KB
testcase_32 AC 106 ms
11,796 KB
testcase_33 AC 75 ms
9,900 KB
testcase_34 AC 165 ms
14,680 KB
testcase_35 AC 109 ms
11,728 KB
testcase_36 AC 55 ms
8,440 KB
testcase_37 AC 57 ms
8,824 KB
testcase_38 AC 78 ms
9,492 KB
testcase_39 AC 135 ms
13,056 KB
testcase_40 AC 148 ms
17,992 KB
testcase_41 AC 149 ms
17,988 KB
testcase_42 AC 146 ms
17,976 KB
testcase_43 AC 137 ms
18,052 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;
	int sz;
	ReRooting(int _sz) :sz(_sz) {
		g.resize(sz);
		dp.reserve(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 });
		}
	}
	void merge(Data &lh, const Data &rh, mint cost) {//★★
		lh.sump += rh.sump + rh.sum * cost * 2 + cost * cost * (rh.count + 1);
		lh.sum += rh.sum + cost * (rh.count + 1);
		lh.count += rh.count + 1;
	}
	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);
			merge(val, dp[e.to], e.cost);
		}
		dp[v] = val;
	}
	void dfsR(int v, int p = -1) {//★
		for (auto e : g[v]) {
			if (p == e.to)continue;
			Data sub = mergeR(dp[v], dp[e.to], e.cost);
			merge(dp[e.to], sub, e.cost);
			dfsR(e.to, v);
		}
	}
	void output() {//★
		mint ans = 0;
		rep(i, sz)ans += dp[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