結果

問題 No.1124 Earthquake Safety
ユーザー 👑 potato167potato167
提出日時 2023-04-12 16:30:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 644 ms / 3,000 ms
コード長 2,730 bytes
コンパイル時間 2,623 ms
コンパイル使用メモリ 212,648 KB
実行使用メモリ 144,000 KB
最終ジャッジ日時 2024-04-17 04:37:15
合計ジャッジ時間 26,885 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 12 ms
5,376 KB
testcase_08 AC 133 ms
15,872 KB
testcase_09 AC 507 ms
41,088 KB
testcase_10 AC 411 ms
144,000 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 541 ms
41,088 KB
testcase_15 AC 539 ms
41,088 KB
testcase_16 AC 530 ms
41,216 KB
testcase_17 AC 542 ms
41,088 KB
testcase_18 AC 535 ms
40,996 KB
testcase_19 AC 537 ms
41,104 KB
testcase_20 AC 536 ms
41,088 KB
testcase_21 AC 538 ms
41,004 KB
testcase_22 AC 534 ms
41,088 KB
testcase_23 AC 518 ms
41,004 KB
testcase_24 AC 518 ms
41,088 KB
testcase_25 AC 511 ms
41,472 KB
testcase_26 AC 501 ms
41,484 KB
testcase_27 AC 510 ms
47,104 KB
testcase_28 AC 513 ms
47,148 KB
testcase_29 AC 557 ms
66,048 KB
testcase_30 AC 558 ms
66,136 KB
testcase_31 AC 618 ms
105,660 KB
testcase_32 AC 611 ms
97,936 KB
testcase_33 AC 640 ms
93,868 KB
testcase_34 AC 644 ms
102,656 KB
testcase_35 AC 489 ms
40,816 KB
testcase_36 AC 469 ms
40,832 KB
testcase_37 AC 465 ms
41,832 KB
testcase_38 AC 466 ms
41,728 KB
testcase_39 AC 466 ms
41,344 KB
testcase_40 AC 477 ms
42,112 KB
testcase_41 AC 441 ms
41,728 KB
testcase_42 AC 436 ms
42,112 KB
testcase_43 AC 447 ms
42,368 KB
testcase_44 AC 442 ms
42,380 KB
testcase_45 AC 439 ms
41,984 KB
testcase_46 AC 442 ms
42,392 KB
testcase_47 AC 433 ms
42,112 KB
testcase_48 AC 436 ms
43,520 KB
testcase_49 AC 448 ms
46,528 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 2 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
#define all(p) p.begin(),p.end()
const int mod=1e9+7;

template <typename T>
struct ReRooting {
    using F = function<T(T, int)>;
    using F2 = function<T(T, T)>;
    int V;
    vector<vector<int>> G;
    vector<vector<T>> dp;
    // dp_v = g(merge(f(dp_c1,c1), f(dp_c2,c2),..., f(dp_ck,ck)), v)
    F f, g;
    F2 merge;
    T mi;  // identity of merge
    ReRooting(int V, F f, F2 merge, T mi, F g)
        : V(V), f(f), merge(merge), mi(mi), g(g) {
        G.resize(V);
        dp.resize(V);
    }
    void add_edge(int a, int b) {
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    T dfs1(int p, int v) {
        T res = mi;
        for (int i = 0; i < G[v].size(); ++i) {
            if (G[v][i] == p) continue;
            dp[v][i] = dfs1(v, G[v][i]);
            res = merge(res, f(dp[v][i], G[v][i]));
        }
        return g(res, v);
    }
    void dfs2(int p, int v, T from_par) {
        for (int i = 0; i < G[v].size(); ++i) {
            if (G[v][i] == p) {
                dp[v][i] = from_par;
                break;
            }
        }
        vector<T> pR(G[v].size() + 1);
        pR[G[v].size()] = mi;
        for (int i = G[v].size(); i > 0; --i)
            pR[i - 1] = merge(pR[i], f(dp[v][i - 1], G[v][i - 1]));
        T pL = mi;
        for (int i = 0; i < G[v].size(); ++i) {
            if (G[v][i] != p) {
                T val = merge(pL, pR[i + 1]);
                dfs2(v, G[v][i], g(val, v));
            }
            pL = merge(pL, f(dp[v][i], G[v][i]));
        }
    }
    void calc(int root = 0) {
        for (int i = 0; i < V; ++i) dp[i].resize(G[i].size());
        dfs1(-1, root);
        dfs2(-1, root, mi);
    }
    T solve(int v) {
        T ans = mi;
        for (int i = 0; i < G[v].size(); ++i)
            ans = merge(ans, f(dp[v][i], G[v][i]));
        return g(ans, v);
    }
};

int main(){
	int N;
	cin>>N;
	using F=pair<ll,ll>;
	auto merge=[&](F l,F r)->F{
		return {(l.first+r.first)%mod,(l.second+r.second+2ll*l.first*r.first)%mod};
	};
	ll rev=(mod+1)/2;
	auto g=[&](F l,int v)->F{
		l.second=(l.first*2ll+l.second)%mod;
		l.first++;
		return l;
	};
	auto f=[&](F l,int v)->F{
		l.second++;
		l.first=(l.first*rev)%mod;
		l.second=(l.second*rev)%mod;
		return l;
	};
	ReRooting<F> R(N,f,merge,{0,0},g);
	rep(i,0,N-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		R.add_edge(a,b);
	}
	R.calc();
	ll ans=0;
	ll base=1;
	rep(i,0,N-1) base=(base*2ll)%mod;
	rep(i,0,N){
		auto v=R.solve(i);
		//cout<<(((v.first*base)%mod)*2ll)%mod<<" "<<(((v.second*base)%mod)*2ll)%mod<<"\n";
		ans+=base;
		ans+=(base*v.second)%mod;
		ans%=mod;
	}
	cout<<ans<<"\n";
}
0