結果

問題 No.1103 Directed Length Sum
ユーザー tonegawatonegawa
提出日時 2020-08-17 16:12:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 971 ms / 3,000 ms
コード長 1,597 bytes
コンパイル時間 1,113 ms
コンパイル使用メモリ 123,224 KB
実行使用メモリ 167,392 KB
最終ジャッジ日時 2024-04-19 18:56:56
合計ジャッジ時間 9,843 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
50,156 KB
testcase_01 AC 20 ms
50,184 KB
testcase_02 AC 358 ms
167,392 KB
testcase_03 AC 251 ms
97,272 KB
testcase_04 AC 496 ms
75,680 KB
testcase_05 AC 971 ms
94,192 KB
testcase_06 AC 287 ms
66,824 KB
testcase_07 AC 58 ms
54,248 KB
testcase_08 AC 85 ms
56,320 KB
testcase_09 AC 46 ms
52,584 KB
testcase_10 AC 129 ms
58,508 KB
testcase_11 AC 575 ms
77,808 KB
testcase_12 AC 293 ms
66,896 KB
testcase_13 AC 125 ms
58,800 KB
testcase_14 AC 38 ms
52,156 KB
testcase_15 AC 201 ms
63,244 KB
testcase_16 AC 662 ms
81,372 KB
testcase_17 AC 679 ms
82,752 KB
testcase_18 AC 118 ms
58,656 KB
testcase_19 AC 558 ms
78,652 KB
testcase_20 AC 48 ms
53,288 KB
testcase_21 AC 76 ms
55,784 KB
testcase_22 AC 445 ms
73,416 KB
testcase_23 AC 240 ms
64,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define rep(a,b,c) for(ll a=b;a<c;a++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void get(vll &a){re(i,a.size()) scanf("%lld",&a[i]);}
void get(vvl &a){re(i,a.size()) re(j,a[0].size()) scanf("%lld",&a[i][j]);}
void print(vll &a){re(i,a.size()) cout<<a[i]<<(i==a.size()-1?"\n":" ");}
void print(vvl &a){re(i,a.size()) re(j,a[0].size())cout<<a[i][j]<<(j==a[0].size()-1?"\n":" ");}


vvl G = VV(1000001, 0, 0, ll);
vll dist(1000001, 0), c(1000001, 0), ans(1000001, 0);
void f(ll now, ll from){
  for(auto to:G[now]){
    if(to==from) continue;
    f(to, now);
    ans[now] += dist[to] + c[to];
    dist[now] += dist[to] + c[to];
    c[now] += c[to];
  }
  c[now]++;
}
int main(int argc, char const *argv[]) {
  ll n;std::cin >> n;
  vll use(n+1, 0);
  re(i, n-1){
    ll a, b;scanf("%lld %lld", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
    use[b] = 1;
  }
  ll root = -1;
  for(int i=1;i<=n;i++) {
    if(!use[i]) {
      root = i;
      break;
    }
  }
  f(root, -1);
  ll s = 0;
  for(int i=1;i<=n;i++) s += ans[i];
  std::cout << s%1000000007 << '\n';
}
0