教教为什么会 MLE
  • 板块学术版
  • 楼主kouylan
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/21 17:24
  • 上次更新2024/7/21 17:28:57
查看原帖
教教为什么会 MLE
123298
kouylan楼主2024/7/21 17:24
/*
luogu P4084
*/
#include <bits/stdc++.h>
using namespace std;

const int M = 1e9+7;

int n,m,root,fa[100005],f[100005][4],c[100005],w[4];
int ee=0,h[100005],nex[100005],to[100005];

void addedge(int x,int y)
{
	nex[++ee] = h[x], to[ee] = y, h[x] = ee;
}

void dfs(int x)
{
	for(int i=h[x];i;i=nex[i])
		if(to[i]!=fa[x])
		{
			fa[to[i]] = x;
			dfs(to[i]);
		}
	w[1] = w[2] = w[3] = 1;
	for(int i=h[x];i;i=nex[i])
		if(to[i]!=fa[x])
		{
			w[1] = w[1]*(f[to[i]][2]+f[to[i]][3])%M;
			w[2] = w[2]*(f[to[i]][1]+f[to[i]][3])%M;
			w[3] = w[3]*(f[to[i]][1]+f[to[i]][2])%M;
		}
	if(c[x]==0)
		f[x][1] = w[1], f[x][2] = w[2], f[x][3] = w[3];
	else
		f[x][c[x]] = w[c[x]];
	if(c[fa[x]]>0)
		f[x][c[fa[x]]] = 0;
}

int main()
{
	cin>>n>>m;
	for(int i=1,x,y;i<n;i++)
	{
		cin>>x>>y;
		addedge(x,y), addedge(y,x);
	}
	for(int i=1,x,y;i<=m;i++)
	{
		cin>>x>>y;
		c[x] = y, root = x;
	}
	dfs(root);
	cout<<f[root][c[root]]<<endl;
  
  	return 0;
}

40 分,后 6 个点全部 MLE。

2024/7/21 17:24
加载中...