QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#312913#7997. 树 V 图InabaMeguruWA 1ms3832kbC++141.6kb2024-01-24 14:45:572024-01-24 14:45:58

Judging History

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

  • [2024-01-24 14:45:58]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3832kb
  • [2024-01-24 14:45:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
const int MAXN=3e3+5;
const int MOD=998244353;
int n,k,a[MAXN],fa[MAXN],dep[MAXN],dis[MAXN];
bool vis[MAXN];
ll s[MAXN],f[MAXN];
vector<int> edg[MAXN],blk[MAXN]; 
int find(int x){
	while(x^fa[x]) x=fa[x]=fa[fa[x]];
	return x;
}
void dfs(int u,int fa){
	for(int v:edg[u]) if(v!=fa){
		dep[v]=dep[u]+1;
		dfs(v,u);
	}
}
void dfs0(int u,int fa,int o){
	if(a[u]==o) s[dis[u]]+=f[u];
	for(int v:edg[u]) if(v!=fa)
		dis[v]=dis[u]+1,dfs0(v,u,o);
}
void dfs1(int id){
	for(int u:blk[id]) for(int v:edg[u]) if(a[v]!=id&&dep[v]>dep[u]){
		dfs1(a[v]);
		memset(s,0,sizeof(s));
		dis[u]=0;dfs0(u,0,a[v]); 
		for(int w:blk[id]){
			if(a[v]>id){
				f[w]=(s[dis[w]]+s[dis[w]+1])%MOD*f[w]%MOD;
			}else{
				f[w]=(s[dis[w]+1]+s[dis[w]+2])%MOD*f[w]%MOD;
			}
		}
	}
}
void solve(){
	memset(vis,0,sizeof(vis));
	cin>>n>>k;
	for(int i=1;i<=n;i++) edg[i].clear(),blk[i].clear(),fa[i]=i;
	for(int i=1;i<n;i++){
		int u,v;cin>>u>>v;
		edg[u].push_back(v);
		edg[v].push_back(u);
	}
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vis[a[i]]=true;
		blk[a[i]].push_back(i);
	}
	for(int i=1;i<=k;i++) if(!vis[i]){
		cout<<0<<endl;return;
	}
	int cnt=n;
	for(int i=1;i<=n;i++) for(int j:edg[i])
		if(a[i]==a[j]&&find(i)!=find(j)) fa[find(i)]=find(j),cnt--; 
	if(cnt!=k){cout<<0<<endl;return;}
	for(int i=1;i<=n;i++) f[i]=1;
	dfs(1,0);dfs1(a[1]);
	ll ans=0;
	for(int i:blk[1]) ans+=f[i];
	cout<<ans%MOD<<endl;
	return;
}
int main(){
	ios::sync_with_stdio(false);
	int _;cin>>_;
	while(_--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3832kb

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
9
1
2
2
2
2
1
15
18

result:

wrong answer 2nd numbers differ - expected: '15', found: '9'