zoukankan      html  css  js  c++  java
  • AtCoder Regular Contest 121

    E - Directed Tree

    解法

    实际上我们需要满足对于 (a_i=j)(j) 不是 (i) 的祖先。我们将 “全部” 变成 “有一个”,即算出不满足条件的种类数(容易发现不满足条件的可以一对一对数),问题可以得到简化。

    定义 (dp_{i,j})(i) 的子树有 (j) 对不合法的点的方案数(注意 (dp_{1,0}) 并不是最终答案,因为我们并未考虑合法的点如何组合)。枚举 (i) 的子树,这就是个很裸的 (mathcal O(n^2)) 背包。

    需要注意的是在遍历结束时将 (i) 与子树中的点进行不合法的配对。

    答案容斥一下就是:

    [ ext{Ans}=sum_{i=0}^n (-1)^i imes dp_{1,i} imes (n-i)! ]

    其中 ((n-i)!) 是计算合法的配对,但由于这样会产生不合法的配对,实际上这是 “至少有 (i) 对不合法点的方案数”。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    #define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
    #define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
    #define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define print(x,y) write(x),putchar(y)
    #define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
    template <typename T> void Out(T x) {cerr<<x<<"
    ";}
    template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}
    
    template <class T> inline T read(const T sample) {
        T x=0; int f=1; char s;
        while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
        while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
        return x*f;
    }
    template <class T> inline void write(const T x) {
        if(x<0) return (void) (putchar('-'),write(-x));
        if(x>9) write(x/10);
        putchar(x%10^48);
    }
    template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
    template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
    template <class T> inline T fab(const T x) {return x>0?x:-x;}
    template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
    template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
    
    const int maxn=2005,mod=998244353;
    
    int n,siz[maxn],dp[maxn][maxn],fac[maxn],tmp[maxn];
    vector <int> e[maxn];
    
    void dfs(int u) {
    	dp[u][0]=1;
    	for(int i=0;i<e[u].size();++i) {
    		int v=e[u][i]; dfs(v);
    		fep(j,siz[u],0) fep(k,siz[v],0)
    			tmp[j+k]=(tmp[j+k]+1ll*dp[u][j]*dp[v][k]%mod)%mod;
    		siz[u]+=siz[v];
    		rep(j,0,siz[u]) dp[u][j]=tmp[j],tmp[j]=0;
    	}
    	fep(i,siz[u],0) dp[u][i+1]=(dp[u][i+1]+1ll*dp[u][i]*(siz[u]-i)%mod)%mod;
    	++siz[u];
    }
    
    void init() {
    	fac[0]=1;
    	rep(i,1,n) fac[i]=1ll*fac[i-1]*i%mod;
    } 
    
    int main() {
    	n=read(9); int ans=0;
    	rep(i,2,n) e[read(9)].push_back(i);
    	dfs(1); init();
    	rep(i,0,n)
    		if(!(i&1)) ans=(ans+1ll*dp[1][i]*fac[n-i]%mod)%mod;
    		else ans=(ans-1ll*dp[1][i]*fac[n-i]%mod+mod)%mod;
    	print(ans,'
    ');
    	return 0;
    }
    
  • 相关阅读:
    css 和 svg 实现蚂蚁行军效果
    ASP.NET Core使用Swagger实现接口文档并分组
    Centos7+DockerCompose部署ASP.NET Core3.1应用
    Centos7+Docker部署ASP.NET Core3.1应用
    ASP.NET Core下的Cache
    在asp.net web form项目中添加webapi接口
    windows服务中对外提供API接口
    ASP.NET Core使用log4net记录日志
    SSL踩坑ERR_SSL_VERSION_OR_CIPHER_MISMATCH
    C# 调用微信接口上传素材和发送图文消息
  • 原文地址:https://www.cnblogs.com/AWhiteWall/p/14841890.html
Copyright © 2011-2022 走看看