跳到正文

题解

题解:P1623 [CEOI 2007] 树的匹配 Treasury

正负君约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年9月16日
收录
2026年10月20日
更新
2026年11月6日
篇幅
约 10 分钟
获赞
1
题目
P1623

版本

  1. v1当前

树形 DP、高精度。

题意

给定一棵有 nn 个点的树,求最大匹配数以及方案数。

思路

首先这题要高精度,支持加法、乘法和 max⁡\max。

最大匹配数

设 fu,0/1f_{u,0/1} 表示节点 uu 和儿子匹配或不选的最大匹配数。则有

fu,0=∑max⁡(fv,0,fv,1)fu,1=∑max⁡(fv,0,fv,1)+max⁡(fv,0−max⁡(fv,0,fv,1))+1f_{u,0}=\sum\max(f_{v,0},f_{v,1})\\ f_{u,1}=\sum\max(f_{v,0},f_{v,1})+\max(f_{v,0}-\max(f_{v,0},f_{v,1}))+1

答案即为 max⁡(fu,0,fu,1)\max(f_{u,0},f_{u,1})。

最大匹配方案数

设 gu,0/1g_{u,0/1} 表示节点 uu 和儿子匹配或不选的最大匹配方案数,单单 gg 是不够的,我们需要再定义一个价值函数 h(u)h(u) 与辅助量 Δv=fv,0−max⁡(fv,0,fv,1)\Delta_v=f_{v,0}-\max(f_{v,0},f_{v,1}),来表示应该取哪一个状态。则有

h(u)={gu,0fu,0>fu,1gu,1fu,0<fu,1gu,0+gu,1fu,0=fu,1gu,0=∏h(v)gu,1=∑max⁡Δvgu,0×gv,0h(v)h(u)=\begin{cases} g_{u,0}&f_{u,0}>f_{u,1}\\ g_{u,1}&f_{u,0}<f_{u,1}\\ g_{u,0}+g_{u,1}&f_{u,0}=f_{u,1}\\ \end{cases}\\ g_{u,0}=\prod h(v)\\ g_{u,1}=\sum_{\max\Delta_v}\frac{g_{u,0}\times g_{v,0}}{h(v)}

其中 gu,0h(v)\frac{g_{u,0}}{h(v)} 可以通过前后缀积 prei,sufipre_i,suf_i 优化,即 gu,0h(v)=prei−1×sufi+1\frac{g_{u,0}}{h(v)}=pre_{i-1}\times suf_{i+1},以此来避免高精度除法。

答案即为 h(1)h(1)。

代码
C++
#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;
}
原文
存档评论1 条抓取于