結果

問題 No.1333 Squared Sum
ユーザー kwm_tkwm_t
提出日時 2021-09-24 04:02:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 2,946 bytes
コンパイル時間 4,103 ms
コンパイル使用メモリ 241,928 KB
実行使用メモリ 112,664 KB
最終ジャッジ日時 2023-09-18 20:08:07
合計ジャッジ時間 14,141 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 285 ms
19,908 KB
testcase_04 AC 287 ms
20,088 KB
testcase_05 AC 281 ms
20,184 KB
testcase_06 AC 284 ms
19,836 KB
testcase_07 AC 282 ms
19,872 KB
testcase_08 AC 289 ms
19,884 KB
testcase_09 AC 288 ms
19,864 KB
testcase_10 AC 288 ms
19,860 KB
testcase_11 AC 287 ms
19,792 KB
testcase_12 AC 295 ms
19,940 KB
testcase_13 AC 333 ms
112,512 KB
testcase_14 AC 382 ms
66,056 KB
testcase_15 AC 415 ms
101,152 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 411 ms
109,816 KB
testcase_27 AC 387 ms
68,700 KB
testcase_28 AC 418 ms
109,100 KB
testcase_29 AC 330 ms
112,664 KB
testcase_30 AC 98 ms
10,164 KB
testcase_31 AC 48 ms
7,332 KB
testcase_32 AC 162 ms
13,316 KB
testcase_33 AC 115 ms
11,296 KB
testcase_34 AC 225 ms
16,824 KB
testcase_35 AC 156 ms
13,248 KB
testcase_36 AC 78 ms
9,192 KB
testcase_37 AC 81 ms
9,368 KB
testcase_38 AC 98 ms
10,424 KB
testcase_39 AC 178 ms
14,692 KB
testcase_40 AC 193 ms
29,112 KB
testcase_41 AC 192 ms
29,312 KB
testcase_42 AC 191 ms
29,120 KB
testcase_43 AC 189 ms
29,304 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>ans;
	int sz;
	ReRooting(int _sz) :sz(_sz) {
		g.resize(sz);
		dp.reserve(sz);
		ans.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  mergeSub(const Data &lh, const Data &rh) {
		Data ret = lh;
		ret.sump += rh.sump;
		ret.sum += rh.sum;
		ret.count += rh.count;
		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) {
		for (auto e : g[v]) ans[v] = merge(ans[v], dp[e.to], e.cost);
		vector<Data>vdata;
		vector<mint>vcost;
		for (auto e : g[v]) vdata.push_back(dp[e.to]), vcost.push_back(e.cost);
		int vsz = vdata.size();
		vector<Data>sumL(vsz + 1), sumR(vsz + 1);
		rep(i, vsz)sumL[i + 1] = merge(sumL[i], vdata[i], vcost[i]);
		rep(i, vsz)sumR[i + 1] = merge(sumR[i], vdata[vsz - i - 1], vcost[vsz - i - 1]);
		for (int i = 0; i < g[v].size(); ++i) {
			auto e = g[v][i];
			if (p == e.to)continue;
			dp[v] = mergeSub(sumL[i], sumR[vsz - (i + 1)]);
			dfsR(e.to, v);
		}
	}
	void output() {
		mint out = 0;
		rep(i, sz)out += ans[i].sump;
		out *= inv_mod(2, mod);
		cout << out.val() << endl;
	}
	void solve() {
		ReadGraph();
		dfs(0);
		dfsR(0, -1);
		output();
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	ReRooting reroot(n);
	reroot.solve();
	return 0;
}
0