题解:P1623 [CEOI 2007] 树的匹配 Treasury
树形 DP、高精度。
题意
给定一棵有
思路
首先这题要高精度,支持加法、乘法和
最大匹配数
设
答案即为
最大匹配方案数
设
其中
答案即为
::::success[代码]
#include<iostream>
#include<iomanip>
#include<vector>
#define int long long
using namespace std;
struct BigInt
{
static const int BASE=10000;
static const int WIDTH=4;
vector<int>s;
BigInt()
{
s.push_back(0);
}
BigInt(long long num)
{
*this=num;
}
BigInt(const string& str)
{
*this=str;
}
BigInt& operator=(long long num)
{
s.clear();
if(num==0)
{
s.push_back(0);
return *this;
}
while(num>0)
{
s.push_back(num%BASE);
num/=BASE;
}
return *this;
}
BigInt& operator=(const string& str)
{
s.clear();
int len=str.size();
for(int i=len; i>0; i-=WIDTH)
{
int start=max(0ll,i-WIDTH);
int num=0;
for(int j=start; j<i; ++j)num=num*10+(str[j]-'0');
s.push_back(num);
}
trim();
return *this;
}
void trim()
{
while(s.size()>1&&s.back()==0)s.pop_back();
}
bool operator<(const BigInt& o)const
{
if(s.size()!=o.s.size())return s.size()<o.s.size();
for(int i=s.size()-1; i>=0; --i)
if(s[i]!=o.s[i])return s[i]<o.s[i];
return false;
}
bool operator==(const BigInt& o)const
{
return s==o.s;
}
bool operator>(const BigInt& o)const
{
return o<*this;
}
bool operator<=(const BigInt& o)const
{
return !(o<*this);
}
bool operator>=(const BigInt& o)const
{
return !(*this<o);
}
bool operator!=(const BigInt& o)const
{
return !(*this==o);
}
BigInt operator+(const BigInt& o)const
{
BigInt r;
r.s.clear();
int carry=0;
int n=max(s.size(),o.s.size());
for(int i=0; i<n||carry; ++i)
{
int sum=carry;
if(i<(int)s.size())sum+=s[i];
if(i<(int)o.s.size())sum+=o.s[i];
r.s.push_back(sum%BASE);
carry=sum/BASE;
}
return r;
}
BigInt operator*(const BigInt& o)const
{
BigInt r;
r.s.resize(s.size()+o.s.size(),0);
for(int i=0; i<(int)s.size(); ++i)
{
long long carry=0;
for(int j=0; j<(int)o.s.size()||carry; ++j)
{
long long cur=r.s[i+j]+carry;
if(j<(int)o.s.size())cur+=(long long)s[i]*o.s[j];
r.s[i+j]=cur%BASE;
carry=cur/BASE;
}
}
r.trim();
return r;
}
BigInt operator+=(const BigInt& o)
{
return *this=*this+o;
}
BigInt operator*=(const BigInt& o)
{
return *this=*this*o;
}
friend ostream& operator<<(ostream& out,const BigInt& x)
{
out<<x.s.back();
for(int i=x.s.size()-2; i>=0; --i)
out<<setw(WIDTH)<<setfill('0')<<x.s[i];
return out;
}
friend istream& operator>>(istream& in,BigInt& x)
{
string str;
if(!(in>>str))return in;
x=str;
return in;
}
};
BigInt max(const BigInt& a,const BigInt& b)
{
return a<b?b:a;
}
BigInt max(const BigInt& a,long long b)
{
return max(a,BigInt(b));
}
BigInt max(long long a,const BigInt& b)
{
return max(BigInt(a),b);
}
const int N=1e3+10,INF=1e18;
int n,edgecnt,head[N],f[N][2];
BigInt g[N][2];
struct edge
{
int from,to,nxt;
} e[N*2];
void add(int u,int v)
{
e[++edgecnt]= {u,v,head[u]};
head[u]=edgecnt;
return;
}
BigInt h(int u)
{
if(f[u][0]>f[u][1])return g[u][0];
if(f[u][0]<f[u][1])return g[u][1];
return g[u][0]+g[u][1];
}
void dfs(int u,int fa)
{
int delta=-INF;
BigInt pre[N],suf[N];
vector<int>son;
son.push_back(1);
g[u][0]=1;
g[u][1]=0;
for(int i=head[u],v; i; i=e[i].nxt)
{
v=e[i].to;
if(v==fa)continue;
dfs(v,u);
son.push_back(v);
f[u][0]+=max(f[v][0],f[v][1]);
g[u][0]*=h(v);
delta=max(delta,f[v][0]-max(f[v][0],f[v][1]));
}
int k=son.size()-1;
if(!k)
{
f[u][0]=0;
f[u][1]=-1;
g[u][0]=1;
g[u][1]=0;
return;
}
f[u][1]=f[u][0]+delta+1;
int v;
pre[0]=suf[k+1]=1;
for(int i=1; i<=k; ++i)
{
v=son[i];
pre[i]=pre[i-1]*h(v);
}
for(int i=k; i>0; --i)
{
v=son[i];
suf[i]=suf[i+1]*h(v);
}
for(int i=1; i<=k; ++i)
{
v=son[i];
if(delta==f[v][0]-max(f[v][0],f[v][1]))g[u][1]+=pre[i-1]*suf[i+1]*g[v][0];
}
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
for(int i=1,u,v,m; i<=n; ++i)
{
cin>>u>>m;
while(m--)
{
cin>>v;
add(u,v);
add(v,u);
}
}
dfs(1,0);
cout<<max(f[1][0],f[1][1])<<'\n'<<h(1)<<'\n';
return 0;
}
::::