結果

問題 No.1124 Earthquake Safety
ユーザー 👑 kmjpkmjp
提出日時 2020-08-04 02:58:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 304 ms / 3,000 ms
コード長 1,671 bytes
コンパイル時間 3,695 ms
コンパイル使用メモリ 201,172 KB
実行使用メモリ 45,540 KB
最終ジャッジ日時 2023-10-11 21:44:19
合計ジャッジ時間 21,948 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
17,660 KB
testcase_01 AC 9 ms
17,420 KB
testcase_02 AC 9 ms
17,400 KB
testcase_03 AC 9 ms
17,468 KB
testcase_04 AC 8 ms
17,712 KB
testcase_05 AC 8 ms
17,572 KB
testcase_06 AC 9 ms
17,624 KB
testcase_07 AC 12 ms
17,728 KB
testcase_08 AC 72 ms
20,916 KB
testcase_09 AC 276 ms
27,112 KB
testcase_10 AC 121 ms
45,540 KB
testcase_11 AC 9 ms
17,448 KB
testcase_12 AC 9 ms
17,644 KB
testcase_13 AC 9 ms
17,596 KB
testcase_14 AC 292 ms
27,116 KB
testcase_15 AC 296 ms
27,168 KB
testcase_16 AC 297 ms
27,236 KB
testcase_17 AC 294 ms
27,116 KB
testcase_18 AC 293 ms
27,108 KB
testcase_19 AC 295 ms
27,364 KB
testcase_20 AC 293 ms
27,296 KB
testcase_21 AC 292 ms
27,152 KB
testcase_22 AC 304 ms
27,120 KB
testcase_23 AC 276 ms
27,008 KB
testcase_24 AC 269 ms
27,036 KB
testcase_25 AC 259 ms
27,208 KB
testcase_26 AC 266 ms
27,280 KB
testcase_27 AC 245 ms
28,208 KB
testcase_28 AC 249 ms
27,980 KB
testcase_29 AC 270 ms
31,408 KB
testcase_30 AC 266 ms
31,280 KB
testcase_31 AC 279 ms
38,368 KB
testcase_32 AC 282 ms
37,152 KB
testcase_33 AC 285 ms
36,512 KB
testcase_34 AC 289 ms
38,288 KB
testcase_35 AC 243 ms
26,972 KB
testcase_36 AC 216 ms
26,800 KB
testcase_37 AC 224 ms
28,076 KB
testcase_38 AC 215 ms
27,908 KB
testcase_39 AC 199 ms
27,584 KB
testcase_40 AC 202 ms
28,308 KB
testcase_41 AC 195 ms
27,992 KB
testcase_42 AC 194 ms
28,388 KB
testcase_43 AC 190 ms
28,648 KB
testcase_44 AC 189 ms
28,400 KB
testcase_45 AC 186 ms
28,332 KB
testcase_46 AC 181 ms
28,532 KB
testcase_47 AC 180 ms
28,228 KB
testcase_48 AC 180 ms
28,032 KB
testcase_49 AC 194 ms
27,896 KB
testcase_50 AC 9 ms
17,448 KB
testcase_51 AC 9 ms
17,404 KB
testcase_52 AC 9 ms
17,400 KB
testcase_53 AC 8 ms
17,456 KB
testcase_54 AC 8 ms
17,616 KB
testcase_55 AC 9 ms
17,672 KB
testcase_56 AC 9 ms
17,700 KB
testcase_57 AC 9 ms
17,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
vector<int> E[302020];
const ll mo=1000000007;

ll A[303030],B[303030];
ll p2[303030],r2;

ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

void dfs(int cur,int pre) {
	A[cur]=B[cur]=1;
	FORR(e,E[cur]) if(e!=pre) {
		dfs(e,cur);
		ll a=(A[cur]+A[cur]+A[e])*r2%mo;
		ll b=(B[cur]+B[cur]+B[e]+2*(A[cur]*A[e]))%mo*r2%mo;
		A[cur]=a;
		B[cur]=b;
	}
}

void dfs2(int cur,int pre, ll PA, ll PB) {
	ll a=(A[cur]+A[cur]+PA)*r2%mo;
	ll b=(B[cur]+B[cur]+PB+2*(A[cur]*PA))%mo*r2%mo;
	A[cur]=a;
	B[cur]=b;
	
	FORR(e,E[cur]) if(e!=pre) {
		ll XA=((A[cur]-A[e]*r2%mo)+mo)%mo;
		ll XB=((B[cur]-XA*A[e]%mo-B[e]*r2%mo)%mo+mo)%mo;
		dfs2(e,cur,XA,XB);
	}
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	r2=(mo+1)/2;
	p2[0]=1;
	FOR(i,300101) p2[i+1]=p2[i]*2%mo;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}
	
	dfs(0,0);
	dfs2(0,0,0,0);
	ll ret=0;
	FOR(i,N) ret+=B[i]*p2[N-1]%mo;
		
	cout<<ret%mo<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0