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的更多相关文章
- CodeForces 1067E Random Forest Rank
题意 给定一棵 \(n\) 个节点的树,每条边有 \(\frac{1}{2}\) 的概率出现,这样会得出一个森林,求这个森林的邻接矩阵 \(A\) 的秩 \(\operatorname{rank} A ...
- Codeforces 1067E - Random Forest Rank(找性质+树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 一道不知道能不能算上自己 AC 的 D1E(?) 挺有意思的结论题,结论倒是自己猜出来了,可根本不会证( 开始搬运题解 ing: 碰到这样 ...
- [Machine Learning & Algorithm] 随机森林(Random Forest)
1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...
- paper 85:机器统计学习方法——CART, Bagging, Random Forest, Boosting
本文从统计学角度讲解了CART(Classification And Regression Tree), Bagging(bootstrap aggregation), Random Forest B ...
- paper 56 :机器学习中的算法:决策树模型组合之随机森林(Random Forest)
周五的组会如约而至,讨论了一个比较感兴趣的话题,就是使用SVM和随机森林来训练图像,这样的目的就是 在图像特征之间建立内在的联系,这个model的训练,着实需要好好的研究一下,下面是我们需要准备的入门 ...
- 多分类问题中,实现不同分类区域颜色填充的MATLAB代码(demo:Random Forest)
之前建立了一个SVM-based Ordinal regression模型,一种特殊的多分类模型,就想通过可视化的方式展示模型分类的效果,对各个分类区域用不同颜色表示.可是,也看了很多代码,但基本都是 ...
- Ensemble Learning 之 Bagging 与 Random Forest
Bagging 全称是 Boostrap Aggregation,是除 Boosting 之外另一种集成学习的方式,之前在已经介绍过关与 Ensemble Learning 的内容与评价标准,其中“多 ...
- Aggregation(1):Blending、Bagging、Random Forest
假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...
- 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 ...
随机推荐
- BASE64Encoder及BASE64Decoder查看源代码方法
一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类.这人个类 ...
- 07_jQuery对象初识(五)事件(非常重要)
1. 目前为止学过的绑定事件的方式 1. 在标签里面写 onclick=foo(this); 2. 原生DOM的JS绑定 DOM对象.onclick=function(){...} 3. jQuery ...
- 关于python DataFrame的学习记录
df_1 = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]}) print df_1 默认左边行index0往上递增,AB为顶部标识,数组内为内容 loc— ...
- 2019-3-16-win10-uwp-鼠标移动到图片上切换图片
title author date CreateTime categories win10 uwp 鼠标移动到图片上切换图片 lindexi 2019-03-16 14:43:46 +0800 201 ...
- Apache 禁用IP 访问 和 HTTP 跳转 HTTPS
如果需要 禁用IP 访问并且 需要 HTTP 跳转到 HTTPS <VirtualHost *:80> ServerName xxx.xxx.com RewriteEngine On Re ...
- [Swoole系列入门教程 3] 心跳检测
一.Swoole 的4大知识点: 1.TCP/UDP服务器 2.微服务 3.协程 二.同步与异步: 同步买奶茶:小明点单交钱,然后等着拿奶茶: 异步买奶茶:小明点单交钱,店员给小明一个小票,等小明奶茶 ...
- Odoo QWeb
1.web 模块 注意,OpenERP 模块中 web 部分用到的所有文件必须被放置在模块内的 static 文件夹里.这是强制性的,出于安全考虑. 事实上,我们创建的文件夹 CSS,JS 和 XML ...
- Vue.nextTick()的介绍和使用场景
每次都很好奇这个干嘛的,然后百度之后还是不明白.今天就彻彻底底好好的弄明白这是干嘛的!! 首先看一下vue文档 nextTick(),是将回调函数延迟在下一次DOM更新数据后调用,简单的理解是:当数据 ...
- windows console 控制台自启动
var fileName = Assembly.GetExecutingAssembly().Location; System.Diagnostics.Process.Start(fileName);
- sublime中用less实现css预编译
实现css预编译的方式有很多,听说glup很流行而且功能也很强大,但是就目前的工作而言,仅要css预编译和YUIcompress就够了,接下来切入正题 Less 是一门 CSS 预处理语言,它扩展了 ...