水题求助
查看原帖
水题求助
151578
残碑小筑楼主2021/11/1 22:02

本地没有问题为什么会全输出0啊?

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int read() {
	int s=0,w=1; char ch=getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') w=-1; ch=getchar();}
	while(ch <= '9' && ch >= '0') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
const int N=1e5;
const int P=1e9+7;
int n,k;
struct Tree{
	int t,nex;
} e[2*N]; int tot,head[N],color[N];
void add(int x,int y) {
	e[++tot].t=y; e[tot].nex=head[x]; head[x]=tot;
}
ll f[N][3],ans;
void dfs(int x,int fath) {
	if(!color[x]) f[x][0]=f[x][1]=f[x][2]=1;
	else f[x][color[x]]=1;
	for(int i=head[x];i;i=e[i].nex) {
		int y=e[i].t;
		if(y == fath) continue;
		dfs(y,x);
		if(!color[x]) {
			f[x][0]=f[x][0]*(f[y][1]+f[y][2])%P;
			f[x][1]=f[x][1]*(f[y][0]+f[y][2])%P;
			f[x][2]=f[x][2]*(f[y][1]+f[y][0])%P;
		}
		else {
			ll sum=0;
			for(int j=0;j<3;j++) if(j != color[y]) sum+=f[y][j];
			f[x][color[x]]*=sum%P;
		}
	}
}
int main() {
	//freopen("P4084_1 - 副本.in","r",stdin);
	scanf("%d %d\n",&n,&k);
	int u,v;
	for(int i=1;i<n;i++) {
		scanf("%d %d\n",&u,&v);
		add(u,v); add(v,u);
	}
	for(int i=1;i<=k;i++) color[read()]=read()-1;
	memset(f,0,sizeof(f));
	dfs(1,0);
	ans=((f[1][0]+f[1][1])%P+f[1][2])%P;
	printf("%lld\n",ans);
	return 0;
}
2021/11/1 22:02
加载中...