結果

問題 No.1333 Squared Sum
ユーザー 👑 jupirojupiro
提出日時 2021-01-24 18:38:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 3,398 bytes
コンパイル時間 1,285 ms
コンパイル使用メモリ 138,468 KB
実行使用メモリ 51,024 KB
最終ジャッジ日時 2023-09-01 22:02:01
合計ジャッジ時間 12,493 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 221 ms
22,824 KB
testcase_04 AC 219 ms
22,576 KB
testcase_05 AC 213 ms
22,840 KB
testcase_06 AC 218 ms
22,452 KB
testcase_07 AC 238 ms
22,520 KB
testcase_08 AC 252 ms
22,524 KB
testcase_09 AC 237 ms
22,848 KB
testcase_10 AC 241 ms
22,848 KB
testcase_11 AC 238 ms
22,460 KB
testcase_12 AC 248 ms
22,476 KB
testcase_13 AC 226 ms
50,900 KB
testcase_14 AC 270 ms
37,088 KB
testcase_15 AC 271 ms
47,552 KB
testcase_16 AC 1 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,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 250 ms
50,348 KB
testcase_27 AC 249 ms
37,944 KB
testcase_28 AC 259 ms
50,040 KB
testcase_29 AC 201 ms
51,024 KB
testcase_30 AC 66 ms
11,176 KB
testcase_31 AC 34 ms
7,788 KB
testcase_32 AC 102 ms
14,940 KB
testcase_33 AC 83 ms
12,492 KB
testcase_34 AC 162 ms
18,828 KB
testcase_35 AC 106 ms
14,916 KB
testcase_36 AC 60 ms
10,292 KB
testcase_37 AC 64 ms
10,504 KB
testcase_38 AC 74 ms
11,696 KB
testcase_39 AC 121 ms
16,572 KB
testcase_40 AC 153 ms
21,880 KB
testcase_41 AC 151 ms
21,892 KB
testcase_42 AC 160 ms
21,924 KB
testcase_43 AC 150 ms
21,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long  mod = 998244353;

struct mint {
	long long x;
	mint(long long x = 0) :x((x% mod + mod) % mod) {}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};
int main()
{

	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	int n; cin >> n;
	vector<vector<pair<int, ll>>> g(n);
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		ll w; cin >> u >> v >> w;
		u--; v--;
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	vector<mint> sz(n);
	vector<mint> len(n);
	vector<mint> dp(n);
	auto dfs1 = [&](auto&& self, int cur, int pre)->void {
		sz[cur] += 1;
		for (auto [nxt, w] : g[cur]) {
			if (nxt == pre) continue;
			self(self, nxt, cur);
			sz[cur] += sz[nxt];
			len[cur] += len[nxt] + mint(w) * sz[nxt];
			dp[cur] += mint(w * w) * sz[nxt] + mint(2) * w * len[nxt] + dp[nxt];
		}
	};
	mint res = 0;
	dfs1(dfs1, 0, -1);
	auto dfs2 = [&](auto&& self, int cur, int pre)->void {
		sz[cur] = 1;
		len[cur] = 0;
		dp[cur] = 0;
		for (auto [nxt, w] : g[cur]) {
			sz[cur] += sz[nxt];
			len[cur] += len[nxt] + mint(w) * sz[nxt];
			dp[cur] += mint(w * w) * sz[nxt] + mint(2) * w * len[nxt] + dp[nxt];
		}
		res += dp[cur];
		for (auto [nxt, w] : g[cur]) {
			if (nxt == pre) continue;
			mint psz = sz[cur];
			mint l = len[cur];
			mint d = dp[cur];
			sz[cur] -= sz[nxt];
			len[cur] -= len[nxt] + mint(w) * sz[nxt];
			dp[cur] -= mint(w * w) * sz[nxt] + mint(2) * w * len[nxt] + dp[nxt];
			self(self, nxt, cur);
			sz[cur] = psz;
			len[cur] = l;
			dp[cur] = d;
		}
	};
	dfs2(dfs2, 0, -1);
	res /= 2;
	cout << res.x << "\n";
}
0