結果

問題 No.1124 Earthquake Safety
ユーザー fastmathfastmath
提出日時 2020-07-22 23:00:10
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,917 bytes
コンパイル時間 2,724 ms
コンパイル使用メモリ 133,992 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-09-05 03:27:09
合計ジャッジ時間 9,284 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 26 ms
4,380 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int MOD = 1000 * 1000 * 1000 + 7;
const int N = 2007;

int mod(int n) {
    n %= MOD;
    if (n < 0) return n + MOD;
    else return n;
}   
int fp(int a, int p) {
    int ans = 1, c = a;
    for (int i = 0; (1ll << i) <= p; ++i) {
        if ((p >> i) & 1) ans = mod(ans * c);
        c = mod(c * c);
    }   
    return ans;
}   
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }

int dist[N][N];
vector <int> g[N];
void dfs(int u, int p, int *dist) {
    for (int v : g[u]) {
        if (v != p) {
            dist[v] = dist[u] + 1;
            dfs(v, u, dist);
        }   
    }   
}   

int pw[N], inv[N];

int n;
int get(int u, int v, int k) {
    vector <int> a = {u, v, k};
    for (int i = 0; i < 3; ++i) {
        int u = a[i];
        int v = a[(i + 1) % 3];
        int k = a[(i + 2) % 3];
        if (dist[u][v] + dist[u][k] == dist[v][k]) {
            return inv[dist[v][k]];
        }
    }   
    return 0;
}   

int sum[N], h[N];
void calc(int u, int p) {   
    sum[u] = inv[h[u]];            
    for (int v : g[u]) {
        if (v != p) {
            h[v] = h[u] + 1;
            calc(v, u);
            sum[u] += sum[v];
            sum[u] %= MOD;
        }   
    }   
}   
int solve(int root) {
    h[root] = 0;
    calc(root, root);    

    vector <int> d;
    for (int v : g[root])
        d.app(v);

    int t = 0;
    for (int v : d) {
        t += sum[v];
        t %= MOD;
    }        

    int ans = ((t * t) % MOD) * t % MOD;
    for (int v : d) {
        ans -= 3 * mod(mod(sum[v] * sum[v]) * t);
        ans += 2 * mod(mod(sum[v] * sum[v]) * sum[v]);
        ans = mod(ans);
    }   
    return ans;
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    pw[0] = 1;
    for (int i = 1; i < N; ++i)
        pw[i] = pw[i - 1] * 2 % MOD;
    for (int i = 0; i < N; ++i)
        inv[i] = fp(pw[i], MOD - 2);

    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v); g[v].app(u);
    }   

    for (int i = 1; i <= n; ++i) {
        dfs(i, i, dist[i]);
    }   

    int ans = 0;

    for (int i = 1; i <= n; ++i) {
        ans += solve(i);
        ans %= MOD;
    }
            
    for (int a = 1; a <= n; ++a) {
        for (int b = 1; b <= n; ++b) {
            for (int c = 1; c <= n; ++c) {
                ans += get(a, b, c);
                ans %= MOD;
            }   
        }   
    }   
    cout << mod(ans * pw[n - 1]) << endl;
}
0