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=;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) 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=;
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-){int ret=;while(y){if(y&) ret=mul(x,ret);x=mul(x,x);y>>=;}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+;
int n;
struct node{
int nxt,to;
}e[*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][],g[N][];
void dfs(int x,int fa){
g[x][]=;
for(reg i=hd[x];i;i=e[i].nxt){
int y=e[i].to;
if(y==fa) continue;
dfs(y,x);
int f1=,f0=,g1=,g0=;
//exi
inc(f1,ad(mul(f[x][],g[y][]),mul(g[x][],f[y][]),mul(g[x][],g[y][])));
inc(f1,ad(mul(f[x][],g[y][]),mul(g[x][],f[y][]),mul(f[x][],g[y][]),mul(g[x][],f[y][])));
inc(g1,ad(mul(g[x][],g[y][]),mul(g[x][],g[y][]),mul(g[x][],g[y][])));
inc(f0,ad(mul(f[x][],g[y][]),mul(g[x][],f[y][])));
inc(g0,mul(g[x][],g[y][]));
//not
inc(f1,ad(mul(f[x][],ad(g[y][],g[y][])),mul(g[x][],ad(f[y][],f[y][]))));
inc(f0,ad(mul(f[x][],ad(g[y][],g[y][])),mul(g[x][],ad(f[y][],f[y][]))));
inc(g1,mul(g[x][],ad(g[y][],g[y][])));
inc(g0,mul(g[x][],ad(g[y][],g[y][]))); f[x][]=f1;f[x][]=f0;g[x][]=g1;g[x][]=g0;
}
}
int main(){
rd(n);
int x,y;
for(reg i=;i<n;++i){
rd(x);rd(y);add(x,y);add(y,x);
}
dfs(,);
int ans=mul(ad(f[][],f[][]),);
ot(ans);
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
*/

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

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

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

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

代码如下:

CF1067E Random Forest Rank的更多相关文章

  1. CodeForces 1067E Random Forest Rank

    题意 给定一棵 \(n\) 个节点的树,每条边有 \(\frac{1}{2}\) 的概率出现,这样会得出一个森林,求这个森林的邻接矩阵 \(A\) 的秩 \(\operatorname{rank} A ...

  2. Codeforces 1067E - Random Forest Rank(找性质+树形 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 一道不知道能不能算上自己 AC 的 D1E(?) 挺有意思的结论题,结论倒是自己猜出来了,可根本不会证( 开始搬运题解 ing: 碰到这样 ...

  3. [Machine Learning & Algorithm] 随机森林(Random Forest)

    1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...

  4. paper 85:机器统计学习方法——CART, Bagging, Random Forest, Boosting

    本文从统计学角度讲解了CART(Classification And Regression Tree), Bagging(bootstrap aggregation), Random Forest B ...

  5. paper 56 :机器学习中的算法:决策树模型组合之随机森林(Random Forest)

    周五的组会如约而至,讨论了一个比较感兴趣的话题,就是使用SVM和随机森林来训练图像,这样的目的就是 在图像特征之间建立内在的联系,这个model的训练,着实需要好好的研究一下,下面是我们需要准备的入门 ...

  6. 多分类问题中,实现不同分类区域颜色填充的MATLAB代码(demo:Random Forest)

    之前建立了一个SVM-based Ordinal regression模型,一种特殊的多分类模型,就想通过可视化的方式展示模型分类的效果,对各个分类区域用不同颜色表示.可是,也看了很多代码,但基本都是 ...

  7. Ensemble Learning 之 Bagging 与 Random Forest

    Bagging 全称是 Boostrap Aggregation,是除 Boosting 之外另一种集成学习的方式,之前在已经介绍过关与 Ensemble Learning 的内容与评价标准,其中“多 ...

  8. Aggregation(1):Blending、Bagging、Random Forest

    假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...

  9. Plotting trees from Random Forest models with ggraph

    Today, I want to show how I use Thomas Lin Pederson's awesome ggraph package to plot decision trees ...

随机推荐

  1. linux socket error code

    errno.00 is: Successerrno.01 is: Operation not permittederrno.02 is: No such file or directoryerrno. ...

  2. quatz调度-手动终止线程(1) 创建触发器,线程执行完毕后添加到cleaner list

    import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException;impor ...

  3. Python学习day07 - Python进阶(1) 内置方法

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. python web 分页组件

    闲来无事便写了一个易使用,易移植的Python Web分页组件.使用的技术栈是Python.Django.Bootstrap. 既然是易使用.易移植的组件,首先介绍一下其在django框架中的调用方式 ...

  5. 第12章 SQL联接

    第12章 SQL联接 关系数据库的3个支柱:选择.投影和联接. 两种基本的连接同等联接和非同等联接. 源表和目标表有相同的名称的列,就可以在他们之间执行自然联接,而无需指定连接列. 自然join us ...

  6. js 属性的遍历

    引自:http://es6.ruanyifeng.com/#docs/object 属性的遍历 ES6 一共有5种方法可以遍历对象的属性. (1)for...in for...in循环遍历对象自身的和 ...

  7. myeclipse中tomcat内存大小的设置

    刚刚安装了myeclipse9.0,又配置了tomcat7.0,想用ssh框架搭个项目试试tomcat7.0,没想到刚启动项目就会报错,在tomcat6.0中就不会有问题,上网查了那些都不起作用,后来 ...

  8. linux服务器之间传输文件

    转载:https://www.jb51.net/article/82608.htm 1. scp(最近就使用了scp) [优点]简单方便,安全可靠:支持限速参数 [缺点]不支持排除目录[用法]scp就 ...

  9. AlexNet模型

    AlexNet模型 <ImageNet Classification with Deep Convolutional Neural Networks>阅读笔记 一直在使用AlexNet,本 ...

  10. html常用标签详解3-a标签

    a标签 1.a标签的属性 a标签属于行内元素标签,双标签<a></a> href:a标签的跳转地址 target:打开方式(_self自身:_blank:新窗口) title: ...