QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#307723 | #7677. Easy Diameter Problem | 275307894a | WA | 3ms | 12268kb | C++14 | 2.9kb | 2024-01-19 07:19:54 | 2024-01-19 07:19:54 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=600+5,M=200+5,K=(1<<25)+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,m;vector<pii> S[N];
int x[N*2],y[N*2],cnt=-1;
void con(int u,int v){
S[u].emplace_back(v,++cnt);x[cnt]=u;y[cnt]=v;//cerr<<u<<' '<<v<<' '<<cnt<<'\n';
S[v].emplace_back(u,++cnt);x[cnt]=v;y[cnt]=u;//cerr<<v<<' '<<u<<' '<<cnt<<'\n';
}
int rt,r1,r2,d1[N],d2[N],len;ll C[N][N],frc[N],inv[N];
void dfs(int x,int La,int *d){
d[x]=d[La]+1;if(d[x]>d[r1]) r1=x;
for(auto i:S[x]) if(i.fi^La) dfs(i.fi,x,d);
}
int s1[N][N],s2[N*2][N],d[N][N];
ll f[N*2][N],g[N*2][N];
void Solve(){
int i,j,h;scanf("%d",&n);if(n==1){puts("1");return;}
for(i=1;i<n;i++) {int x,y;scanf("%d%d",&x,&y);con(x,i+n);con(y,i+n);}
r1=1;dfs(1,0,d1);r2=r1;dfs(r1,0,d2);r2=r1;dfs(r1,0,d1);len=(d1[r1]-1)/2;
for(i=1;i<=2*n;i++) if(d1[i]==len+1&&d2[i]==len+1) rt=i;
for(i=0;i<=n;i++) for(C[i][0]=j=1;j<=i;j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
inv[1]=1;for(i=2;i<=n;i++) inv[i]=(mod-inv[mod%i])*(mod/i)%mod;
for(frc[0]=inv[0]=i=1;i<=n;i++) frc[i]=frc[i-1]*i%mod,inv[i]=inv[i-1]*inv[i]%mod;
for(i=1;i<2*n;i++) dfs(i,0,d[i]);
for(i=1;i<2*n;i++){
for(j=1;j<2*n;j++) if(d[i][j]-1<=len) s1[d[i][j]-1][i]++;
for(auto o:S[i]) for(j=1;j<2*n;j++) if(d[i][j]-1<=len&&d[o.fi][j]==d[i][j]-1) s2[d[i][j]-1][o.se]++;
}
cerr<<s1[2][2]<<'\n';
int cts=0;
for(i=len;i;i--){
int *sz1=s1[i],*sz2=s2[i];
if(i==len)for(j=0;j<=cnt;j++) if(x[j]==rt){f[j][sz1[x[j]]-sz2[j]]=1;break;}
for(j=0;j<=cnt;j++)if((x[j]<=n)^(i&1)){
int u=x[j],v=y[j];
for(h=1;h<=sz1[u]-sz2[j];h++)if(f[j][h]){//cerr<<j<<' '<<h<<'\n';
for(int p=1;p<=sz2[j];p++){
ll w=f[j][h];
w=w*C[h-1+sz2[j]-p][h-1]%mod*frc[sz1[u]-sz2[j]]%mod;
f[j^1][p]=(f[j^1][p]+w)%mod;
}
for(auto o:S[u]) if(o.se^j){
for(int p=1;p<=h&&p<=sz2[o.se];p++){
ll w1=C[h-p+sz2[j]][sz2[j]]*C[sz1[u]-p-sz2[j]][sz1[u]-sz2[o.se]-sz2[j]]%mod;
ll w2=C[h-p-1+sz2[j]][sz2[j]]*C[sz1[u]-p-sz2[j]-1][sz1[u]-sz2[o.se]-sz2[j]]%mod;
// cerr<<w1<<' '<<w2<<'\n';
ll w=f[j][h]*(w1-w2+mod)%mod*frc[sz2[j]]%mod*frc[sz1[u]-sz2[o.se]-sz2[j]]%mod;
// cerr<<w<<'\n';
f[o.se^1][p]=(f[o.se^1][p]+w)%mod;
}
}
f[j][h]=0;
}
}
}
cerr<<cts<<'\n';
ll tot=0;for(i=0;i<=cnt;i++) tot+=f[i][1];printf("%lld\n",tot%mod);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12268kb
input:
3 1 2 3 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 12244kb
input:
5 4 1 4 5 1 2 1 3
output:
28
result:
ok 1 number(s): "28"
Test #3:
score: 0
Accepted
time: 2ms
memory: 10284kb
input:
7 5 7 2 5 2 1 1 6 3 6 4 1
output:
116
result:
ok 1 number(s): "116"
Test #4:
score: 0
Accepted
time: 2ms
memory: 10404kb
input:
100 89 60 66 37 59 74 63 49 69 37 9 44 94 22 70 30 79 14 25 33 23 4 78 43 63 30 95 7 6 59 56 31 21 56 58 43 95 28 81 19 63 40 58 87 81 38 100 55 78 23 37 78 86 70 33 11 52 17 42 19 19 13 70 100 97 79 66 67 66 27 82 55 15 27 68 33 97 26 46 20 70 93 1 10 69 79 81 45 58 95 24 63 79 51 85 11 12 43 41 64...
output:
748786623
result:
ok 1 number(s): "748786623"
Test #5:
score: -100
Wrong Answer
time: 3ms
memory: 11436kb
input:
300 109 123 221 101 229 22 114 101 110 258 50 79 26 1 238 47 140 271 77 213 140 125 19 179 111 96 194 114 57 101 1 251 19 13 92 238 114 116 193 140 153 238 1 173 252 220 1 22 124 197 196 214 196 224 1 138 201 90 184 56 266 154 71 184 46 15 256 27 1 69 1 32 22 182 237 196 111 1 279 91 139 196 114 80 ...
output:
132760816
result:
wrong answer 1st numbers differ - expected: '47013557', found: '132760816'