QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#367209#5139. DFS Order 2Baiyu0123RE 2ms8796kbC++142.2kb2024-03-25 20:17:332024-03-25 20:17:34

Judging History

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

  • [2024-03-25 20:17:34]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:8796kb
  • [2024-03-25 20:17:33]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=505,mod=998244353;
vector<int> ed[maxn];
int d[maxn],siz[maxn],n;
int sum,g[maxn][maxn],G[20][maxn][maxn],Sum[20];
int f[maxn][maxn];
int fac[maxn],inv[maxn];
void lj(int x,int y) {
	ed[x].push_back(y);
	ed[y].push_back(x);
}
void init_dfs(int fa,int u) {
	d[u]=ed[u].size();siz[u]=1;
	for (int i=0;i<ed[u].size();i++) {
		if (ed[u][i]==fa) {
			for (int j=i+1;j<ed[u].size();j++) {
				ed[u][j-1]=ed[u][j];
			}
			d[u]--;
			break;
		}
	}
	for (int i=1;i<=d[u];i++) {
		int v=ed[u][i-1];
		init_dfs(u,v);
		siz[u]+=siz[v];
	}
}
void add(int &x,int y) {
	x=(x+y)%mod;
}
void output(int f[][maxn]) {
	for (int i=1;i<=n;i++) {
		for (int j=1;j<=n;j++) {
			cout<<f[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}
void getans(int u,int l,int r) {
	for (int i=l;i<=r;i++) {
		int v=ed[u][i-1];
		for (int p=sum;p>=0;p--) {
			for (int j=d[u]-1;j>=0;j--) {
				add(g[p+siz[v]][j+1],g[p][j]);
			}
		}
		sum+=siz[v];
	}
}
void calc(int u,int l,int r,int dep) {
	if (l==r) {
		int v=ed[u][l-1];
		for (int k=1;k<=n-siz[u]+1;k++) {
			for (int p=1;p<=siz[u]-1;p++) {
				for (int j=0;j<=d[u]-1;j++) {
					add(f[v][k+p],
						1ll*f[u][k]*g[p-1][j]%mod*fac[j]%mod*fac[d[u]-j-1]%mod*inv[d[u]]%mod);
				}
			}
		}
		return ;
	}
	int mid=(l+r)>>1;
	memcpy(G[dep],g,sizeof(g));Sum[dep]=sum;
	getans(u,mid+1,r);
	calc(u,l,mid,dep+1);
	memcpy(g,G[dep],sizeof(g));sum-Sum[dep];
	getans(u,l,mid);
	calc(u,mid+1,r,dep+1);
}
void dfs(int fa,int u) {
	if (d[u]==0) return;
	memset(g,0,sizeof(g));
	g[0][0]=1;
	calc(u,1,d[u],0);
	for (int i=1;i<=d[u];i++) {
		int v=ed[u][i-1];
		dfs(u,v);
	}
}
int qpow(int x,int y) {
	ll ret=1,bas=x;
	while (y) {
		if (y&1) ret=ret*bas%mod;
		bas=bas*bas%mod;
		y>>=1;
	}
	return ret;
}
int main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n;
	fac[0]=1;
	for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
	for (int i=0;i<=n;i++) inv[i]=qpow(fac[i],mod-2);
	for (int l=2;l<=n;l++) {
		int x,y;cin>>x>>y;
		lj(x,y);
	}
	init_dfs(1,1);
	f[1][1]=1;
	for (int i=1;i<=n;i++) f[1][1]=1ll*f[1][1]*fac[d[i]]%mod;
	dfs(1,1);
	output(f);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8288kb

input:

5
1 2
1 3
3 4
3 5

output:

4 0 0 0 0 
0 2 0 0 2 
0 2 2 0 0 
0 0 1 2 1 
0 0 1 2 1 


result:

ok 25 numbers

Test #2:

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

input:

10
9 2
9 6
10 5
1 5
1 6
9 3
5 8
4 3
7 9

output:

24 0 0 0 0 0 0 0 0 0 
0 0 0 4 2 2 8 2 2 4 
0 0 0 4 4 4 4 4 4 0 
0 0 0 0 4 4 4 4 4 4 
0 12 0 0 0 0 0 12 0 0 
0 12 0 0 12 0 0 0 0 0 
0 0 0 4 2 2 8 2 2 4 
0 0 6 6 0 0 0 0 6 6 
0 0 12 0 0 12 0 0 0 0 
0 0 6 6 0 0 0 0 6 6 


result:

ok 100 numbers

Test #3:

score: -100
Runtime Error

input:

100
18 100
91 87
28 83
11 98
51 52
24 91
72 53
18 19
89 16
77 35
26 25
73 16
96 70
56 44
69 10
63 30
54 95
39 66
58 98
8 71
58 65
74 73
2 64
12 19
32 81
31 54
43 41
84 59
55 75
72 81
59 37
10 94
93 2
64 47
13 32
36 84
28 22
30 28
25 77
47 6
80 52
54 17
23 40
47 88
49 53
65 27
99 59
25 70
91 9
74 1
7...

output:


result: