QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#307605#7677. Easy Diameter Problem275307894aWA 5ms9380kbC++143.0kb2024-01-18 21:16:052024-01-18 21:16:06

Judging History

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

  • [2024-01-18 21:16:06]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:9380kb
  • [2024-01-18 21:16:05]
  • 提交

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],y[N],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 siz[N][N],d[N];
void make(int x,int La,int ds,int *siz){
	d[x]=d[La]+1;siz[x]=(d[x]==ds+1);
	for(auto i:S[x]) if(i.fi^La) make(i.fi,x,ds,siz),siz[x]+=siz[i.fi];
}
ll f[N][N/2],g[N][N/2];
void Solve(){
	int i,j,h;scanf("%d",&n);
	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;
	cerr<<rt<<'\n';
	for(i=len;i;i--){
		if(i&1) for(j=n+1;j<2*n;j++) make(j,0,i,siz[j]);
		else for(j=1;j<=n;j++) make(j,0,i,siz[j]);
		if(i==len)for(j=0;j<=cnt;j++) if(x[j]==rt){f[j][siz[x[j]][x[j]]-siz[x[j]][y[j]]]=1;break;}
		Mc(g,f);Me(f,0);
		for(j=0;j<=cnt;j++)if((x[j]<=n)^(i&1)){
			int u=x[j],v=y[j];
			for(h=1;h<=siz[u][u]-siz[u][v];h++)if(g[j][h]){
				// cerr<<i<<' '<<u<<' '<<v<<' '<<h<<' '<<g[j][h]<<'\n';
				// if(i==2&&u==1&&v==6&&h==2) g[j][h]=0;
				// if(i==2&&u==4&&v==6&&h==1) g[j][h]=0;
				for(int p=1;p<=siz[u][v];p++){
					ll w=g[j][h]*frc[siz[u][u]-siz[u][v]-h]%mod;
					w=w*C[h-1+siz[u][v]-p][h-1]%mod*frc[siz[u][u]-siz[u][v]]%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<=siz[u][o.fi];p++){
						ll w1=C[h-p+siz[u][v]][siz[u][v]]*C[siz[u][u]-p-siz[u][v]][siz[u][u]-siz[u][o.fi]-siz[u][v]]%mod;
						ll w2=C[h-p-1+siz[u][v]][siz[u][v]]*C[siz[u][u]-p-siz[u][v]-1][siz[u][u]-siz[u][o.fi]-siz[u][v]]%mod;
						// cerr<<w1<<' '<<w2<<'\n';
						ll w=g[j][h]*(w1-w2+mod)%mod*frc[siz[u][v]]%mod*frc[siz[u][u]-siz[u][o.fi]-siz[u][v]]%mod;
						// cerr<<w<<'\n';
						f[o.se^1][p]=(f[o.se^1][p]+w)%mod;
					}
				}
			}
		}
	}
	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';
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 8932kb

input:

3
1 2
3 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 2ms
memory: 8872kb

input:

5
4 1
4 5
1 2
1 3

output:

28

result:

ok 1 number(s): "28"

Test #3:

score: 0
Accepted
time: 0ms
memory: 8348kb

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: -100
Wrong Answer
time: 5ms
memory: 9380kb

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:

333099876

result:

wrong answer 1st numbers differ - expected: '748786623', found: '333099876'