CF1067E Random Forest Rank

CF1067E Random Forest Rank 

可以证明:

一个树的邻接矩阵的秩,等于最大匹配数*2(虽然我只能证明下界是最大匹配)

而树的最大匹配可以贪心,

不妨用DP模拟这个过程

f[x][0/1]表示,x为根的子树,所有情况下,按照贪心使得x被选/没有没选,的最大匹配的总和

g[x][0/1]为方案数。

转移时候讨论即可。

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Modulo{
const int mod=998244353;
int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;}
void inc(int &x,int y){x=ad(x,y);}
int mul(int x,int y){return (ll)x*y%mod;}
void inc2(int &x,int y){x=mul(x,y);}
int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
template<class ...Args>il int ad(const int a,const int b,const Args &...args) {return ad(ad(a,b),args...);}
template<class ...Args>il int mul(const int a,const int b,const Args &...args) {return mul(mul(a,b),args...);}
}
using namespace Modulo;
namespace Miracle{
const int N=5e5+5;
int n;
struct node{
    int nxt,to;
}e[2*N];
int hd[N],cnt;
void add(int x,int y){
    e[++cnt].nxt=hd[x];
    e[cnt].to=y;
    hd[x]=cnt;
}
int f[N][2],g[N][2];
void dfs(int x,int fa){
    g[x][0]=1;
    for(reg i=hd[x];i;i=e[i].nxt){
        int y=e[i].to;
        if(y==fa) continue;
        dfs(y,x);
        int f1=0,f0=0,g1=0,g0=0;
        //exi
        inc(f1,ad(mul(f[x][0],g[y][0]),mul(g[x][0],f[y][0]),mul(g[x][0],g[y][0])));
        inc(f1,ad(mul(f[x][1],g[y][0]),mul(g[x][1],f[y][0]),mul(f[x][1],g[y][1]),mul(g[x][1],f[y][1])));
        inc(g1,ad(mul(g[x][0],g[y][0]),mul(g[x][1],g[y][0]),mul(g[x][1],g[y][1])));
        inc(f0,ad(mul(f[x][0],g[y][1]),mul(g[x][0],f[y][1])));
        inc(g0,mul(g[x][0],g[y][1]));
        //not
        inc(f1,ad(mul(f[x][1],ad(g[y][0],g[y][1])),mul(g[x][1],ad(f[y][0],f[y][1]))));
        inc(f0,ad(mul(f[x][0],ad(g[y][0],g[y][1])),mul(g[x][0],ad(f[y][0],f[y][1]))));
        inc(g1,mul(g[x][1],ad(g[y][0],g[y][1])));
        inc(g0,mul(g[x][0],ad(g[y][0],g[y][1])));

        f[x][1]=f1;f[x][0]=f0;g[x][1]=g1;g[x][0]=g0;
    }
}
int main(){
    rd(n);
    int x,y;
    for(reg i=1;i<n;++i){
        rd(x);rd(y);add(x,y);add(y,x);
    }
    dfs(1,0);
    int ans=mul(ad(f[1][0],f[1][1]),2);
    ot(ans);
    return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
*/

 然后CF讨论里一个julao提出一个更简单的方法

直接计算f[x]表示x和某个儿子有匹配的概率

根据期望的线性性,直接f[x]相加就是答案。

那么,f[x]=1-无法匹配的概率,

代码如下:

转载于:https://www.cnblogs.com/Miracevin/p/10990131.html

猜你喜欢

转载自blog.csdn.net/weixin_34341117/article/details/93718416