QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#242062#7607. The Doubling Game 2Jryno1RE 7ms54032kbC++142.2kb2023-11-06 21:58:412023-11-06 21:58:43

Judging History

你现在查看的是最新测评结果

  • [2023-11-06 21:58:43]
  • 评测
  • 测评结果:RE
  • 用时:7ms
  • 内存:54032kb
  • [2023-11-06 21:58:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
//thanks for sinsop
#define int long long 
const int maxn=6e5+10,mod=1e9+7;
int n,lg[maxn],ans[maxn],siz[maxn];//ans:don't move
vector<int>UPF[maxn],DOWF[maxn];
//UPF:x -> fat  DOWF:fat -> x
vector<int>ed[maxn];
bool cmp(int x,int y){
	return siz[x]<siz[y];
}
void dfs(int x,int fat){
	vector<int>V;
	siz[x]=1;
	for(int i=0;i<ed[x].size();i++){
		int v=ed[x][i];
		if(v==fat)continue;
		dfs(v,x);
		siz[x]+=siz[v];
		V.push_back(v);
	}
	sort(V.begin(),V.end(),cmp);
	int sz=1;
	vector<int>UP(2),DOW(2);
	UP[0]=1;
	//UP:all sons go up(or don't); DOW:I went down
	for(auto v:V){
		if(v==fat)continue;
		vector<int>F(1<<UPF[v].size()+1),G(1<<UPF[v].size()+1);
		for(int S=0;S<sz;S++){
			//the son doesn't go up(or down) at all
			F[S]=(F[S]+UP[S]*ans[v]%mod)%mod;
			G[S]=(G[S]+DOW[S]*ans[v]%mod)%mod;
			//the son go up
			for(int k=0;k<UPF[v].size();k++){
				if((1<<k)&S)continue;
				F[S|(1<<k)]+=UPF[v][k]*UP[S]%mod;
				F[S|(1<<k)]%=mod;
				if(S>(1<<k)){
					G[S|(1<<k)]+=UPF[v][k]*DOW[S]%mod;//no effect(this one must go up)
					G[S|(1<<k)]%=mod;
				} else {
					G[S|(1<<k)]+=DOWF[v][k]*UP[S]%mod;//this one must go down
					G[S|(1<<k)]%=mod;
				}
			}
		}
		swap(F,UP),swap(G,DOW);
		sz=(1<<UPF[v].size());
		vector<int>().swap(F),vector<int>().swap(G);
	}
//	cout<<x<<" "<<sz<<endl;
	sz=lg[sz];
	UPF[x].resize(sz+1),DOWF[x].resize(sz+1);
	for(int i=0;i<=sz;i++){
		int S=(1<<i)-1;//full
//		cout<<"---"<<x<<" "<<S<<" "<<UP[S]<<endl;
		ans[x]+=UP[S],ans[x]%=mod;//not moving
		ans[x]+=DOW[S],ans[x]%=mod;
		UPF[x][i]+=UP[S],DOWF[x][i]+=UP[S];//mov
		UPF[x][i]%=mod,DOWF[x][i]%=mod;
		for(int j=0;j<=i-2;j++)DOWF[x][j]=(DOWF[x][j]+UP[S^(1<<j)]+DOW[S^(1<<j)])%mod;
	}
//	cout<<x<<" "<<ans[x]<<endl;
//	while(!UPF[x].back()&&!DOWF[x].back())UPF[x].pop_back(),DOWF[x].pop_back();
}
signed main(){
	cin.tie(0),cout.tie(0),ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		ed[u].push_back(v),ed[v].push_back(u);
	}
//	lg[1]=1;
	for(int i=2;i<maxn;i++)lg[i]=lg[i>>1]+1;
	dfs(1,0);
	cout<<ans[1]<<endl;
	return 0;
}
/*
5
1 2
1 3
1 4
4 5
*/

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 51280kb

input:

5
1 2
1 3
1 4
4 5

output:

21

result:

ok single line: '21'

Test #2:

score: 0
Accepted
time: 4ms
memory: 51708kb

input:

1

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 7ms
memory: 52736kb

input:

128
11 32
116 81
65 4
117 47
5 81
104 30
61 8
82 59
95 20
92 29
29 127
97 39
123 33
59 128
115 33
83 67
74 16
77 33
64 73
124 123
8 127
61 51
101 122
35 90
119 116
112 27
81 93
109 123
54 1
119 100
116 16
65 47
67 27
22 105
76 87
36 39
27 96
72 31
91 123
21 105
118 12
110 48
121 72
14 115
24 16
106 ...

output:

508800953

result:

ok single line: '508800953'

Test #4:

score: 0
Accepted
time: 4ms
memory: 52068kb

input:

256
53 177
57 242
74 90
107 104
209 169
132 70
152 142
71 168
143 99
91 130
202 140
49 165
209 193
209 137
159 188
247 48
49 21
20 208
155 185
120 231
83 87
37 84
143 18
106 8
114 79
191 158
208 256
133 252
215 92
199 108
166 168
39 217
85 69
204 139
100 75
111 6
230 198
79 130
26 155
155 38
55 81
1...

output:

999869740

result:

ok single line: '999869740'

Test #5:

score: 0
Accepted
time: 7ms
memory: 54032kb

input:

512
507 193
168 152
48 369
273 170
101 349
160 261
438 197
446 224
125 264
210 131
272 218
361 85
226 119
57 33
229 89
37 317
130 417
30 470
435 300
499 417
132 260
196 430
119 117
157 260
207 151
368 277
188 371
214 330
484 228
96 94
97 442
251 461
443 248
163 207
306 147
346 90
457 112
436 222
364...

output:

37387055

result:

ok single line: '37387055'

Test #6:

score: -100
Runtime Error

input:

1024
340 598
1 851
245 819
414 736
996 316
300 284
924 407
532 557
362 178
1006 469
397 373
742 77
112 37
406 892
703 666
496 825
1002 100
875 856
263 975
227 6
288 389
661 437
160 626
833 770
912 837
405 628
466 686
45 629
59 13
163 991
1017 422
208 247
344 376
709 956
570 272
996 954
518 454
267 3...

output:


result: