結果

問題 No.1333 Squared Sum
ユーザー kwm_tkwm_t
提出日時 2021-09-24 02:02:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 2,650 bytes
コンパイル時間 4,543 ms
コンパイル使用メモリ 236,212 KB
実行使用メモリ 35,220 KB
最終ジャッジ日時 2023-09-18 20:07:04
合計ジャッジ時間 12,337 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 187 ms
17,488 KB
testcase_04 AC 192 ms
17,528 KB
testcase_05 AC 191 ms
17,484 KB
testcase_06 AC 192 ms
17,472 KB
testcase_07 AC 203 ms
17,612 KB
testcase_08 AC 190 ms
17,420 KB
testcase_09 AC 187 ms
17,420 KB
testcase_10 AC 175 ms
17,416 KB
testcase_11 AC 191 ms
17,400 KB
testcase_12 AC 194 ms
17,484 KB
testcase_13 AC 177 ms
35,172 KB
testcase_14 AC 228 ms
26,064 KB
testcase_15 AC 220 ms
32,796 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 222 ms
34,688 KB
testcase_27 AC 207 ms
26,264 KB
testcase_28 AC 226 ms
34,440 KB
testcase_29 AC 174 ms
35,220 KB
testcase_30 AC 54 ms
9,116 KB
testcase_31 AC 30 ms
6,588 KB
testcase_32 AC 92 ms
11,752 KB
testcase_33 AC 67 ms
9,896 KB
testcase_34 AC 129 ms
14,576 KB
testcase_35 AC 85 ms
11,764 KB
testcase_36 AC 47 ms
8,440 KB
testcase_37 AC 49 ms
8,556 KB
testcase_38 AC 59 ms
9,368 KB
testcase_39 AC 109 ms
13,244 KB
testcase_40 AC 128 ms
18,240 KB
testcase_41 AC 130 ms
18,168 KB
testcase_42 AC 137 ms
18,004 KB
testcase_43 AC 136 ms
18,060 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 });
		}
	}
	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) {
		for (auto e : g[v]) {
			if (p == e.to)continue;
			Data sub = mergeR(dp[v], dp[e.to], e.cost);
			dp[e.to] = 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