QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#336885#7997. 树 V 图0xyzWA 81ms39788kbC++141.9kb2024-02-24 23:15:262024-02-24 23:15:26

Judging History

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

  • [2024-02-24 23:15:26]
  • 评测
  • 测评结果:WA
  • 用时:81ms
  • 内存:39788kb
  • [2024-02-24 23:15:26]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353,_=3005;
int c[_],cnt,d[_],f[_][_],g[_][11],h[_],k,lg[_],n,p[_],s,T,u[_],v[_],w[_],z;
vector<int>a[_],b[_],o[_];
int get(int x,int y){
	return d[x]<d[y]?x:y;
}
void dfs(int x,int fa){
	d[x]=d[fa]+1;h[x]=fa;w[x]=++cnt;g[cnt][0]=x;
	for(auto y:a[x])
		if(y!=fa)dfs(y,x);
}
int dis(int x,int y){
	if(x==y)return 0;
	if(w[y]<w[x])swap(x,y);
	int l=lg[w[y]-w[x]];
	return d[x]+d[y]-d[h[get(g[w[x]+1][l],g[w[y]-(1<<l)+1][l])]]*2;
}
void dp(int x,int fa){
	for(auto i:o[x])f[x][i]=1;
	for(auto y:b[x])
		if(y!=fa){
			dp(y,x);
			for(auto i:o[x]){
				int sum=0;
				for(auto j:o[y]){
					int dj=dis(j,p[y]),di=dis(i,h[p[y]]);
					if(dj==di||x>y&&dj==di+1||x<y&&dj+1==di)sum=(sum+f[y][j])%mod;
				}
				f[x][i]=1ll*f[x][i]*sum%mod;
			}
		}
}
int main(){
	//freopen("qwq.in","r",stdin);
	//freopen("qwq.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n>>k;z=s=d[0]=cnt=0;lg[0]=-1;
		for(int i=1;i<=n;i++){
			a[i].clear();b[i].clear();o[i].clear();p[i]=0;
			for(int j=1;j<=n;j++)f[i][j]=0,lg[i]=lg[i>>1]+1; 
		}
		memset(f,0,sizeof(f));z=0;
		for(int i=1;i<n;i++)cin>>u[i]>>v[i],a[u[i]].push_back(v[i]),a[v[i]].push_back(u[i]);
		for(int i=1;i<=n;i++)cin>>c[i],o[c[i]].push_back(i);
		dfs(1,0);d[0]=1e9;
		for(int i=1;i<=10;i++)
			for(int j=1;j<=n-(1<<i-1);j++)g[j][i]=get(g[j][i-1],g[j+(1<<i-1)][i-1]);
		for(int i=1;i<n;i++)
			if(c[u[i]]^c[v[i]])b[c[u[i]]].push_back(c[v[i]]),b[c[v[i]]].push_back(c[u[i]]),z++;
		for(int i=1;i<=n;i++)
			if(d[p[c[i]]]>d[i])p[c[i]]=i;
		for(int i=1;i<=k;i++)
			if(!p[i])z=0;
		if(z!=k-1){
			cout<<"0\n";
			continue;
		}
		dp(c[1],0);
		//for(int i=1;i<=k;i++)
		//	for(auto j:o[i])cout<<i<<' '<<j<<' '<<f[i][j]<<'\n';
		for(auto i:o[c[1]])s=(s+f[c[1]][i])%mod;
		cout<<s<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 14ms
memory: 39072kb

input:

10
15 2
10 5
3 5
12 5
10 9
11 7
3 8
2 4
7 1
15 14
8 13
15 6
2 1
4 8
11 15
1 1 1 1 2 1 1 1 2 2 1 2 1 1 1
15 3
8 11
12 8
1 3
13 15
5 9
10 13
6 12
14 4
4 9
15 5
11 10
2 14
7 2
6 3
3 2 3 2 2 3 2 1 2 1 1 3 1 2 1
15 5
1 7
5 2
11 9
6 8
13 3
14 12
3 1
8 9
5 10
10 11
5 1
12 13
10 15
11 4
3 3 3 2 3 2 1 2 2 2 ...

output:

11
15
8
9
5
2
1
0
15
18

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 12ms
memory: 39172kb

input:

10
15 2
7 10
10 3
11 4
9 7
4 15
14 8
7 1
8 15
13 4
2 1
15 9
11 6
2 12
5 6
2 2 2 1 1 1 2 1 2 2 1 2 1 1 1
15 3
4 10
12 3
7 3
9 1
6 7
9 11
11 7
10 8
13 15
14 2
14 7
5 15
12 13
11 10
1 2 3 3 3 3 3 3 1 3 3 3 3 2 3
15 5
12 9
6 8
13 2
6 10
2 3
14 15
10 5
4 15
14 13
5 7
11 10
14 12
1 5
13 8
2 4 1 3 2 2 5 3 ...

output:

21
4
5
2
13
17
6
0
0
0

result:

ok 10 numbers

Test #3:

score: -100
Wrong Answer
time: 81ms
memory: 39788kb

input:

10
3000 2
2052 2631
688 2422
2401 352
654 1669
1566 2157
1187 334
179 178
948 1821
1084 99
878 793
410 336
2218 865
2558 236
2808 430
799 1238
1468 226
312 268
1860 991
2946 96
2540 241
2242 1103
299 1527
788 2026
1885 2104
229 1627
2461 2649
498 2642
1354 98
113 980
947 2790
1281 2232
2682 713
1841...

output:

2117
21248
731916691
0
3936
9543168
561554568
0
0
0

result:

wrong answer 1st numbers differ - expected: '2420', found: '2117'