結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 233 ms
17,496 KB
testcase_04 AC 223 ms
17,720 KB
testcase_05 AC 210 ms
17,688 KB
testcase_06 AC 203 ms
17,484 KB
testcase_07 AC 215 ms
17,388 KB
testcase_08 AC 215 ms
17,540 KB
testcase_09 AC 200 ms
17,452 KB
testcase_10 AC 204 ms
17,588 KB
testcase_11 AC 220 ms
17,536 KB
testcase_12 AC 216 ms
17,408 KB
testcase_13 AC 189 ms
38,148 KB
testcase_14 AC 244 ms
27,556 KB
testcase_15 AC 250 ms
35,684 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 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,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 251 ms
37,600 KB
testcase_27 AC 244 ms
27,952 KB
testcase_28 AC 254 ms
37,248 KB
testcase_29 AC 190 ms
38,124 KB
testcase_30 AC 62 ms
9,208 KB
testcase_31 AC 33 ms
6,576 KB
testcase_32 AC 104 ms
11,800 KB
testcase_33 AC 71 ms
9,964 KB
testcase_34 AC 162 ms
14,708 KB
testcase_35 AC 110 ms
11,784 KB
testcase_36 AC 58 ms
8,440 KB
testcase_37 AC 65 ms
8,540 KB
testcase_38 AC 69 ms
9,328 KB
testcase_39 AC 142 ms
12,976 KB
testcase_40 AC 144 ms
17,976 KB
testcase_41 AC 152 ms
17,992 KB
testcase_42 AC 149 ms
18,012 KB
testcase_43 AC 146 ms
18,128 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;
			merge(dp[e.to], mergeR(dp[v], dp[e.to], e.cost), 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