結果
問題 | No.872 All Tree Path |
ユーザー | heno239 |
提出日時 | 2019-08-30 22:19:25 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 280 ms / 3,000 ms |
コード長 | 2,666 bytes |
コンパイル時間 | 1,415 ms |
コンパイル使用メモリ | 120,256 KB |
実行使用メモリ | 95,616 KB |
最終ジャッジ日時 | 2024-05-01 18:31:22 |
合計ジャッジ時間 | 5,038 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 249 ms
21,760 KB |
testcase_01 | AC | 247 ms
21,888 KB |
testcase_02 | AC | 245 ms
21,760 KB |
testcase_03 | AC | 181 ms
95,616 KB |
testcase_04 | AC | 6 ms
9,472 KB |
testcase_05 | AC | 262 ms
21,760 KB |
testcase_06 | AC | 280 ms
21,888 KB |
testcase_07 | AC | 278 ms
21,760 KB |
testcase_08 | AC | 21 ms
10,752 KB |
testcase_09 | AC | 21 ms
10,880 KB |
testcase_10 | AC | 21 ms
10,880 KB |
testcase_11 | AC | 22 ms
10,880 KB |
testcase_12 | AC | 23 ms
10,880 KB |
testcase_13 | AC | 7 ms
9,600 KB |
testcase_14 | AC | 7 ms
9,728 KB |
testcase_15 | AC | 8 ms
9,600 KB |
testcase_16 | AC | 8 ms
9,472 KB |
testcase_17 | AC | 7 ms
9,600 KB |
testcase_18 | AC | 7 ms
9,600 KB |
testcase_19 | AC | 8 ms
9,472 KB |
ソースコード
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<bitset> #include<stack> #include<unordered_map> #include<utility> #include<cassert> using namespace std; //#define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = mod * mod; typedef pair<int, int> P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef pair<ll, ll> LP; typedef vector<ll> vec; typedef long double ld; typedef pair<ld, ld> LDP; const ld eps = 1e-5; struct edge { int to, cost; }; vector<edge> G[1 << 18]; LP ansch[1 << 18]; void dfs(int id, int fr) { ll sum = 0; ll num = 1; rep(j, G[id].size()) { int to = G[id][j].to; if (to == fr)continue; dfs(to, id); num += ansch[to].second; sum += ansch[to].first + ansch[to].second*G[id][j].cost; } ansch[id] = { sum,num }; } ll ans[1 << 18]; void dfs2(int id, int fr,LP cfr) { vector<int> ids; vector<int> costs; vector<LP> pre; rep(j, G[id].size()) { int to = G[id][j].to; if (to == fr)continue; ids.push_back(to); costs.push_back(G[id][j].cost); pre.push_back({ ansch[to].first + ansch[to].second*G[id][j].cost,ansch[to].second }); } if (fr < 0) { ll sum = 0, num = 1; rep(i, ids.size()) { sum += pre[i].first; num += pre[i].second; } ans[id] = sum; rep(i, ids.size()) { int to = ids[i]; ll tosum = sum - pre[i].first; ll tonum = num - pre[i].second; dfs2(to, id, { tosum + tonum * costs[i],tonum }); } } else { pre.push_back(cfr); ll sum = 0, num = 1; rep(i, pre.size()) { sum += pre[i].first; num += pre[i].second; } ans[id] = sum; rep(i, ids.size()) { int to = ids[i]; ll tosum = sum - pre[i].first; ll tonum = num - pre[i].second; dfs2(to, id, { tosum + tonum * costs[i],tonum }); } } } void solve() { int n; cin >> n; rep(i, n-1) { int u, v, w; cin >> u >> v >> w; u--; v--; G[u].push_back({ v,w }); G[v].push_back({ u,w }); } dfs(0, -1); dfs2(0, -1, {}); ll sum = 0; rep(i, n)sum += ans[i]; /*rep(i, n) { cout << ans[i] << endl; }*/ cout << sum << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(5); //init(); solve(); //cout << "finish" << endl; //stop return 0; }