2019-2020 ACM-ICPC Latin American Regional Programming Contest I - Improve SPAM 树形dp?

Posted qingyuyyyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019-2020 ACM-ICPC Latin American Regional Programming Contest I - Improve SPAM 树形dp?相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<cstring>
#define int long long
using namespace std;
const int N=4010,mod=1e9+7;
int a[N];
int e[N*N],ne[N*N],idx,h[N];
int st[N];
int ans;
int ans1[N];
int read()
{
	int res=0,ch,flag=0;
	if((ch=getchar())==‘-‘)             //判断正负
		flag=1;
	else if(ch>=‘0‘&&ch<=‘9‘)           //得到完整的数
		res=ch-‘0‘;
	while((ch=getchar())>=‘0‘&&ch<=‘9‘)
		res=res*10+ch-‘0‘;
	return flag?-res:res;
}
void add(int a,int b)
{
	e[idx]=b;
	ne[idx]=h[a];
	h[a]=idx++;
}
int dp[N];
int res;
int dfs(int u,int fa)
{
	if(dp[u]!=-1)
		return dp[u];
	if(st[u]==0)
	{
		res++;
		dp[u]=1;
		return dp[u];
	}
	int sum=0;
	for(int i=h[u]; i!=-1; i=ne[i])
	{
		int j=e[i];
		if(j==fa)
			continue;
		sum=(sum+(dfs(j,u)%mod))%mod;
	}
	dp[u]=sum;
	return sum;
}
signed main()
{
	memset(dp,-1,sizeof dp);
	memset(h,-1,sizeof h);
	int n=read(),l=read();
	for(int i=1; i<=l; i++)
	{
		st[i]=1;
		int k=read();
		while(k--)
		{
			int a=i;
			int b=read();
			add(i,b);
		}
	}
	ans=dfs(1,-1);
	cout<<ans%mod<<" "<<res%mod<<endl;
}

以上是关于2019-2020 ACM-ICPC Latin American Regional Programming Contest I - Improve SPAM 树形dp?的主要内容,如果未能解决你的问题,请参考以下文章

2019-2020 ACM-ICPC Latin American Regional Programming Contest I - Improve SPAM 树形dp?

训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest

2017-2018 ACM-ICPC Latin American Regional Programming Contest

2017-2018 ACM-ICPC Latin American Regional Programming Contest GYM101889

2017-2018 ACM-ICPC Latin American Regional Programming Contest Solution

2020-2021 ACM-ICPC Latin American Regional Programming Contest(F. Fascinating Partitions)