QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#242059 | #7607. The Doubling Game 2 | Jryno1 | WA | 10ms | 52412kb | C++14 | 2.2kb | 2023-11-06 21:57:14 | 2023-11-06 21:57:14 |
Judging History
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: 51272kb
input:
5 1 2 1 3 1 4 4 5
output:
21
result:
ok single line: '21'
Test #2:
score: 0
Accepted
time: 10ms
memory: 52412kb
input:
1
output:
1
result:
ok single line: '1'
Test #3:
score: -100
Wrong Answer
time: 9ms
memory: 50812kb
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:
554054109
result:
wrong answer 1st lines differ - expected: '508800953', found: '554054109'