QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#609767 | #7509. 01tree | cjx | WA | 14ms | 11812kb | C++20 | 3.3kb | 2024-10-04 13:57:22 | 2024-10-04 13:57:25 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
long long read(){
long long x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void write(long long x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N=1e5+10;
const ll mod=1e9+7;
int tt,n;
int head[N],ver[N<<1],nxt[N<<1],tot;
ll fac[N],inv[N],ans;
string s1,t1;
int s[N][3],t[N][3],dep[N],sz[N],son[N];
ll qmi(ll a,ll b){
ll ans=1;
while(b){
if(b&1)ans=ans*a%mod;
a=a*a%mod;b>>=1;
}
return ans;
}
void add(int x,int y){
ver[++tot]=y,nxt[tot]=head[x],head[x]=tot;
}
void dfs1(int x,int fa){
dep[x]=dep[fa]+1;
if(dep[x]&1){
if(s1[x-1]!='?')s1[x-1]='1'+'0'-s1[x-1];
if(t1[x-1]!='?')t1[x-1]='1'+'0'-t1[x-1];
}
if(s1[x-1]=='?')s[x][2]=1;
if(s1[x-1]=='1')s[x][1]=1;
if(t1[x-1]=='?')t[x][2]=1;
if(t1[x-1]=='1')t[x][1]=1;
for(int i=head[x];i;i=nxt[i]){
int y=ver[i];
if(y==fa)continue;
dfs1(y,x);sz[x]+=sz[y];
s[x][1]+=s[y][1];
s[x][2]+=s[y][2];
t[x][1]+=t[y][1];
t[x][2]+=t[y][2];
if(son[x]==-1||sz[y]>sz[son[x]])son[x]=y;
}
}
ll C(int n,int m){
if(n<0||m<0||n<m)return 0;
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
bool cmp(int x,int y){
return sz[x]>sz[y];
}
struct func{
int P,Q,S,T;
ll res;
void init(){
res=0;
for(int i=0;i<=Q;i++)
res=(res+C(P,i)*C(S-P,T-i)%mod)%mod;
}
void addp(ll op){
res=(res+op*C(P,Q)*C(S-P-1,T-Q-1)%mod+mod)%mod;
}
void addq(ll op){
res=(res+op*C(P,Q+1)*C(S-P,T-Q-1)%mod+mod)%mod;
}
void move(int p,int q){
while(P<p)addp(-1),P++;
while(P>p)P--,addp(1);
while(Q<q)addq(1),Q++;
while(Q>q)Q--,addq(-1);
}
}f1[N],f2[N];
int lst;
void dfs2(int x,int fa){
if(x>1){
int A=s[x][2]+t[x][2];
int B=s[x][2]+s[x][1]-t[x][1];
if(!lst){
f1[x].P=A;f1[x].Q=B-1;f1[x].S=s[1][2]+t[1][2];f1[x].T=s[1][2]+s[1][1]-t[1][1];
f2[x].P=f1[x].P-1;f2[x].Q=f1[x].Q-1;f2[x].S=f1[x].S-1;f2[x].T=f1[x].T-1;
f1[x].init();f2[x].init();
lst=x;
}
else{
f1[x]=f1[lst];f2[x]=f2[lst];
f1[x].move(A,B-1);
f2[x].move(A-1,B-2);
}
//printf("%d %d %d\n",s[1][2],s[1][1],t[1][1]);
//printf("x=%d A=%d B=%d S=%d T=%d\n",x,A,B,f1[x].S,f1[x].T);
ans=(ans+B*f1[x].res%mod-A*f2[x].res%mod+mod)%mod;
}
if(son[x]!=-1)dfs2(son[x],x);
for(int i=head[x];i;i=nxt[i]){
int y=ver[i];
if(y==fa||y==son[x])continue;
dfs2(y,x);
}
}
void solve(){
dfs1(1,0);
ans=0;
lst=0;dfs2(1,0);
for(int i=2;i<=n;i++){
s[i][1]=s[1][1]-s[i][1];
s[i][2]=s[1][2]-s[i][2];
t[i][1]=t[1][1]-t[i][1];
t[i][2]=t[1][2]-t[i][2];
}
lst=0;dfs2(1,0);
write(ans);puts("");
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
fac[0]=inv[0]=1;
for(int i=1;i<N;i++){
fac[i]=fac[i-1]*i%mod;
inv[i]=qmi(fac[i],mod-2);
}
tt=read();
while(tt--){
n=read();
for(int i=1;i<3;i++)
for(int j=1;j<=n;j++)
s[j][i]=t[j][i]=0;
for(int i=1;i<=n;i++){
head[i]=0;sz[i]=1;son[i]=-1;
}
tot=1;
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y);add(y,x);
}
cin>>s1>>t1;
solve();
}
return 0;
}
/*
3
2
1 2
00
11
3
1 2
2 3
???
???
3
1 2
2 3
??1
0?0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 11808kb
input:
3 2 1 2 00 11 3 1 2 2 3 ??? ??? 3 1 2 2 3 ??1 0?0
output:
1 16 1
result:
ok 3 number(s): "1 16 1"
Test #2:
score: -100
Wrong Answer
time: 14ms
memory: 11812kb
input:
1000 23 1 2 1 3 1 4 2 5 5 6 4 7 3 8 4 9 8 10 8 11 8 12 1 13 7 14 10 15 7 16 7 17 5 18 18 19 12 20 9 21 21 22 6 23 00?10?0000??1?00111?010 011??1?10?01?110?0??101 6 1 2 1 3 1 4 4 5 3 6 000?10 1???01 25 1 2 2 3 2 4 4 5 5 6 2 7 4 8 5 9 7 10 8 11 11 12 5 13 11 14 3 15 6 16 14 17 1 18 4 19 6 20 4 21 5 22...
output:
53545 24 11724 2228 162 14 711 28 550 1680 52 2 13 988 9480 2342 626416 0 71780 1 88 39207 19448 4 37395 9602 3253496 38057200 1066 3 303732 1608 281022 11718 170 78 15 1219376 29364 9092 7 0 2 7014 1942448 170371 75845 167217 16554 64 904 564290 14822 1127090 1758504 1227646 14833316 14954550 36055...
result:
wrong answer 231st numbers differ - expected: '136', found: '240'