QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#473689#1222. 多边形windviolet50 289ms183852kbC++203.9kb2024-07-12 13:13:522024-07-12 13:13:53

Judging History

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

  • [2024-07-12 13:13:53]
  • 评测
  • 测评结果:50
  • 用时:289ms
  • 内存:183852kb
  • [2024-07-12 13:13:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD=998244353;
const int N=3e3+5; 
vector<int>out[N];
vector<int>outt[N];
void add(int u,int v){
	out[u].push_back(v);
}
int n,k;
int lf[N],lfcnt=0;
void dfs(int start,int father){
	bool flag=0;
	int lenn=out[start].size();
	for(int i=0;i<lenn;i++){
		dfs(out[start][i],start);
		flag=1;
	}
	if(!flag)lf[++lfcnt]=start;
}
int d[(1<<20)+5][22];
bool con[25][25];
void case20(){
	dfs(1,0); 
	for(int i=1;i<=n;i++){
		int lenn=out[i].size();
		for(int j=0;j<lenn;j++)con[i][out[i][j]]=con[out[i][j]][i]=1;
	}
	for(int i=1;i<=lfcnt;i++){
		for(int j=i+1;j<=lfcnt;j++){
			if(j-i<=k||i+lfcnt-j<=k)con[lf[i]][lf[j]]=con[lf[j]][lf[i]]=1;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)if(con[i][j]&&i!=j)outt[i].push_back(j);
	}
	/*for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)cout<<con[i][j]<<" ";
		cout<<"\n";
	}*/
	d[1][1]=1;
	for(int i=1;i<(1<<n);i++){
		for(int j=1;j<=n;j++){
			if(i&(1<<(j-1))){
				//cout<<i<<" "<<j<<" "<<d[i][j]<<"\n";
				int lenn=outt[j].size();
				for(int k=0;k<lenn;k++){
					if(i&(1<<(outt[j][k]-1)))continue;
					d[i|(1<<(outt[j][k]-1))][outt[j][k]]+=d[i][j];
					d[i|(1<<(outt[j][k]-1))][outt[j][k]]%=MOD;
				}
			}
		}
	}
	int ans=0;
	for(int i=2;i<=n;i++){
		if(!con[1][i])continue;
		ans+=d[(1<<n)-1][i];
		ans%=MOD;
	}
	cout<<ans/2<<"\n";
}
int f[N][3][3];
int tmp[N];
int tmplen=0;
int pre[N],suf[N];
void dp(int start,int father){
	int son=0;
	int lenn=out[start].size();
	for(int i=0;i<lenn;i++){
		dp(out[start][i],start);
		son++;
	}
	if(!son){
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				if(i!=j)f[start][i][j]=1;
			}
		}
		lfcnt++;
		return;
	}
	if(son==1){
		int v=out[start][0];
		f[start][0][1]=f[v][0][1];
		f[start][0][2]=f[v][0][2];
		f[start][1][0]=f[v][1][0];
		f[start][2][0]=f[v][2][0];
		return;
	}
	tmplen=0;
	for(int i=0;i<lenn;i++){
		tmp[++tmplen]=out[start][i];
	}
	pre[0]=1;
	for(int i=1;i<=tmplen;i++){
		pre[i]=pre[i-1]*f[tmp[i]][1][2]%MOD;
	}
	suf[tmplen+1]=1;
	for(int i=tmplen;i>=1;i--){
		suf[i]=suf[i+1]*f[tmp[i]][1][2]%MOD;
	}
	int ans=0;
	for(int i=0;i<=tmplen-2;i++){
		ans+=pre[i]*suf[i+3]%MOD*f[tmp[i+1]][1][0]%MOD*f[tmp[i+2]][0][2]%MOD;
		ans%=MOD;
	}
	f[start][1][2]=f[start][2][1]=ans;
	ans=f[tmp[1]][0][2];
	for(int i=2;i<=tmplen;i++){
		ans=ans*f[tmp[i]][1][2]%MOD;
	}
	f[start][0][2]=f[start][2][0]=ans;
	ans=f[tmp[tmplen]][0][1];
	//if(start==2)cout<<ans<<"?\n";
	for(int i=1;i<tmplen;i++){
		ans=ans*f[tmp[i]][2][1]%MOD;
		//if(start==2)cout<<tmp[i]<<"?\n";
	}
	//if(start==2)cout<<ans<<"?\n";
	f[start][0][1]=f[start][1][0]=ans;
}
void casek1(){
	dp(1,0);
	tmplen=0;
	int lenn=out[1].size();
	for(int i=0;i<lenn;i++){
		tmp[++tmplen]=out[1][i];
	}
	if(tmplen==1){
		cout<<0<<"\n";
		return;
	}
	if(lfcnt==2){
		cout<<1<<"\n";
		return;
	}
	if(tmplen==2){
		cout<<(f[tmp[1]][0][2]*f[tmp[2]][1][0]+f[tmp[1]][0][1]*f[tmp[2]][2][0])%MOD<<"\n";
		return;
	}
	/*for(int i=1;i<=n;i++){
		cout<<i<<":";
		for(int j=0;j<=2;j++){
			for(int k=0;k<=2;k++){
				cout<<f[i][j][k]<<" ";
			}
			cout<<"\n";
		}
	}*/
	pre[0]=1;
	for(int i=1;i<=tmplen;i++){
		pre[i]=pre[i-1]*f[tmp[i]][1][2]%MOD;
	}
	suf[tmplen+1]=1;
	for(int i=tmplen;i>=1;i--){
		suf[i]=suf[i+1]*f[tmp[i]][1][2]%MOD;
	}
	int ans=0;
	for(int i=0;i<=tmplen-2;i++){
		ans+=pre[i]*suf[i+3]%MOD*f[tmp[i+1]][1][0]%MOD*f[tmp[i+2]][0][2]%MOD;
		ans%=MOD;
	}
	int sum=f[tmp[1]][0][2]*f[tmp[tmplen]][1][0]%MOD;
	for(int i=2;i<tmplen;i++){
		sum=sum*f[tmp[i]][1][2]%MOD;
	}
	//cout<<ans<<" "<<sum<<"\n";
	cout<<(ans+sum)%MOD<<"\n";
}
signed main(){
	//freopen("6.txt","r",stdin);
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<n;i++){
		int v;
		scanf("%lld",&v);
		add(v,i+1); 
	} 
	for(int i=1;i<=n;i++)sort(out[i].begin(),out[i].end());
	if(n<=20){
		case20();
		return 0;
	}
	if(k==1){
		casek1();
		return 0;
	}
	return 0;
}

详细

Test #1:

score: 5
Accepted
time: 0ms
memory: 3828kb

input:

5 3
1 1 1 3


output:

2

result:

ok single line: '2'

Test #2:

score: 5
Accepted
time: 1ms
memory: 5824kb

input:

10 3
1 2 3 4 5 2 5 4 1

output:

16

result:

ok single line: '16'

Test #3:

score: 5
Accepted
time: 9ms
memory: 11132kb

input:

15 3
1 1 2 1 1 1 3 3 1 1 1 1 1 1

output:

23854

result:

ok single line: '23854'

Test #4:

score: 5
Accepted
time: 289ms
memory: 183852kb

input:

20 3
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 18 18

output:

24097065

result:

ok single line: '24097065'

Test #5:

score: 5
Accepted
time: 1ms
memory: 3828kb

input:

1000 1
1 2 3 4 5 6 7 8 7 10 11 10 6 14 15 16 17 18 19 19 21 18 23 23 17 26 27 26 29 30 30 32 29 16 35 36 35 15 39 39 41 41 43 43 14 46 46 48 49 48 5 52 53 54 53 56 56 58 58 52 61 62 63 63 65 66 67 66 69 69 71 71 65 74 62 76 76 78 79 79 81 78 83 83 85 85 87 61 89 90 89 92 92 4 95 96 97 98 98 97 96 10...

output:

0

result:

ok single line: '0'

Test #6:

score: 5
Accepted
time: 1ms
memory: 3744kb

input:

999 1
1 2 3 4 5 6 7 8 9 10 10 9 13 13 8 7 17 18 19 20 21 22 22 21 20 26 26 19 29 30 30 32 32 29 35 35 37 38 38 37 18 42 43 43 42 46 46 17 49 50 50 49 53 54 54 53 6 58 59 60 61 62 63 63 65 65 62 61 60 70 70 72 72 59 75 76 77 77 76 75 81 81 58 84 85 86 86 88 89 90 90 92 92 89 88 96 97 97 96 100 100 85...

output:

2

result:

ok single line: '2'

Test #7:

score: 5
Accepted
time: 0ms
memory: 3884kb

input:

1000 1
1 2 3 4 5 6 7 8 9 10 11 12 13 13 12 16 17 17 19 19 21 21 16 24 24 11 10 28 29 29 28 32 33 34 34 33 37 37 32 40 40 9 43 43 8 46 46 7 49 50 50 49 53 53 55 55 6 58 59 60 61 61 63 63 60 59 67 68 69 69 68 67 73 74 74 76 76 78 78 73 58 82 83 83 85 85 82 88 89 89 88 92 92 94 94 96 96 5 99 100 100 10...

output:

1

result:

ok single line: '1'

Test #8:

score: 5
Accepted
time: 1ms
memory: 3816kb

input:

1000 1
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36...

output:

106890205

result:

ok single line: '106890205'

Test #9:

score: 5
Accepted
time: 0ms
memory: 3928kb

input:

1000 1
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 1 3 2 4 6 3 1 2 3 7 1 6 7 1 4 2 2 6 4 7 7 1 48 48 48 49 49 49 50 50 50 51 51 51 52 52 52 53 53 53 54 54 54 55 55 55 56 56 56 57 57 57 58 58 58 59 59 59 60 60 60 61 61 61 62 62 62 63 63 63 64 64 64 65 65 65 66 66 66 67 67 67 68 68 68 69 69 69 7...

output:

420850835

result:

ok single line: '420850835'

Test #10:

score: 5
Accepted
time: 1ms
memory: 3804kb

input:

1000 1
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36...

output:

570335790

result:

ok single line: '570335790'

Test #11:

score: 0
Wrong Answer
time: 0ms
memory: 3676kb

input:

1000 2
1 2 3 4 5 6 7 8 8 7 6 12 12 5 15 4 17 18 19 20 21 22 23 22 25 26 26 28 28 30 30 25 21 34 35 36 36 35 39 39 41 41 34 44 45 45 47 44 20 50 51 52 53 52 51 56 57 57 56 50 61 62 63 64 65 65 67 67 69 64 71 72 73 74 75 75 74 73 72 80 80 82 63 84 84 86 86 62 89 90 90 89 93 94 95 95 97 94 99 99 93 102...

output:


result:

wrong answer 1st lines differ - expected: '178991712', found: ''

Test #12:

score: 0
Wrong Answer
time: 0ms
memory: 3864kb

input:

1000 2
1 2 3 4 5 6 7 8 9 9 11 8 13 13 7 16 17 16 19 19 21 6 5 24 25 25 27 24 29 29 31 4 33 34 35 36 36 35 39 34 41 42 42 41 33 46 47 48 48 47 46 3 53 54 55 56 55 58 58 54 53 62 63 62 65 65 67 2 69 70 71 72 72 74 71 76 77 77 79 79 76 70 83 84 85 86 87 88 89 90 90 92 93 94 93 92 89 98 99 99 98 102 103...

output:


result:

wrong answer 1st lines differ - expected: '460227781', found: ''

Test #13:

score: 0
Wrong Answer
time: 0ms
memory: 3860kb

input:

1000 2
1 2 3 4 5 6 7 8 7 6 11 5 13 13 15 15 17 17 4 20 20 22 23 24 25 26 26 25 24 30 23 32 22 34 35 36 37 38 38 37 36 42 43 44 44 46 43 48 48 50 50 42 53 54 55 55 57 58 58 57 61 61 54 53 65 65 67 67 35 70 71 70 34 74 75 76 77 78 78 77 81 82 82 84 84 81 76 88 89 88 91 92 93 93 92 91 97 98 97 100 100 ...

output:


result:

wrong answer 1st lines differ - expected: '125424813', found: ''

Test #14:

score: 0
Wrong Answer
time: 0ms
memory: 3696kb

input:

1000 2
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36...

output:


result:

wrong answer 1st lines differ - expected: '23756983', found: ''

Test #15:

score: 0
Wrong Answer
time: 0ms
memory: 3656kb

input:

1000 2
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 3 4 19 11 19 15 10 1 3 21 10 9 17 20 5 12 16 7 12 12 11 2 4 7 4 20 3 21 2 4 16 2 11 16 16 8 4 15 14 12 1 6 7 11 19 8 1 6 2 2 ...

output:


result:

wrong answer 1st lines differ - expected: '325532742', found: ''

Test #16:

score: 0
Wrong Answer
time: 0ms
memory: 3792kb

input:

1000 2
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36...

output:


result:

wrong answer 1st lines differ - expected: '116800683', found: ''

Test #17:

score: 0
Wrong Answer
time: 0ms
memory: 3680kb

input:

1000 3
1 2 3 4 5 6 5 8 9 10 11 12 12 11 15 10 17 18 17 20 21 22 21 20 9 26 27 28 28 27 26 32 32 34 8 36 36 38 39 40 40 39 43 43 45 38 47 48 48 47 4 52 52 54 54 3 57 58 59 60 61 61 60 64 59 66 67 67 69 66 71 72 73 72 71 76 77 77 76 58 81 82 83 83 82 86 87 88 89 88 91 92 92 91 95 95 87 98 86 100 100 1...

output:


result:

wrong answer 1st lines differ - expected: '767516039', found: ''

Test #18:

score: 0
Wrong Answer
time: 0ms
memory: 3868kb

input:

1000 3
1 2 3 4 5 6 7 7 6 10 11 12 10 5 15 15 4 18 19 20 21 22 22 21 25 26 26 25 29 20 31 32 32 34 34 31 37 19 39 39 18 42 43 44 45 46 45 48 48 50 44 52 52 54 43 56 57 58 59 59 61 62 62 61 58 66 67 67 66 57 71 72 72 71 56 76 77 78 77 76 81 81 42 84 85 86 86 85 89 90 89 92 93 92 84 96 3 98 99 100 101 ...

output:


result:

wrong answer 1st lines differ - expected: '776061489', found: ''

Test #19:

score: 0
Wrong Answer
time: 0ms
memory: 3760kb

input:

1000 3
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36...

output:


result:

wrong answer 1st lines differ - expected: '357633930', found: ''

Test #20:

score: 0
Wrong Answer
time: 0ms
memory: 3764kb

input:

1000 3
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36...

output:


result:

wrong answer 1st lines differ - expected: '568729964', found: ''