题目链接

luogu P1623 [CEOI2007]树的匹配Treasury

题解

f[i][0/1]表示当前位置没用/用了

转移暴力就可以了

code

// luogu-judger-enable-o2
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using std::vector;
using std::max;
using std::min;
inline int read() {
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar() ;}
while(c<='9'&&c>='0') x=x*10+c-'0',c=getchar() ;
return x*f;
}
const int maxn = 1007;
struct node{
int v,next;
}edge[maxn];
int dp[maxn][2],head[maxn],num=0;
inline void add_edge(int u,int v) {
edge[++num].v=v;edge[num].next=head[u];head[u]=num;
}
int n;
void init() {
n=read();
for(int num,k,i=1;i<=n;++i) {
k=read(),num=read();
for(int j=1;j<=num;++j)
add_edge(k,read());
}
}
struct Int {
static const int Bit=1e4;//压3位
int a[107],size;
inline void operator = (const int &x) {
memset(a,0,sizeof a) ;a[size=1]=x;
}
inline Int operator + (const Int t ) {
Int c=*this;
int sz =max(c.size,t.size);
for(int i=1;i<=sz;++i)
c.a[i]+=t.a[i],c.a[i+1]+=c.a[i]/Bit,c.a[i]%=Bit;
if(c.a[sz+1])sz++;
c.size=sz;
return c;
}
inline Int operator * (const Int t) {
int sz=size+t.size-1;
Int c;c=0;
for(int i=1;i<=size;++i)
for(int j=1;j<=t.size;++j)
c.a[i+j-1]+=a[i]*t.a[j];
for(int i=1;i<=sz;++i)
c.a[i+1]+=c.a[i]/Bit,c.a[i]%=Bit;
if(c.a[sz+1])sz++;c.size=sz;
return c;//XD
}
inline void print() {
printf("%d",a[size]);
for(int i=size-1;i;--i)
printf("%04d",a[i]);
}
}cnt[maxn][2];
inline void operator *= (Int &a,Int b) {a=a*b;}
inline void operator += (Int &a,Int b) {a=a+b;}
void dfs(int x) {
//int num = vec[x].size();
cnt[x][0]=1;cnt[x][1]=1;
for(int i=head[x];i;i=edge[i].next) {
int v=edge[i].v;
dfs(v);
if(dp[x][1]) {
dp[x][1]+=dp[v][1];
if(dp[v][0]!=dp[v][1]) cnt[x][1]*=cnt[v][1];
else cnt[x][1]*=cnt[v][0]+cnt[v][1];
}
if(dp[x][0]+dp[v][0]+1 > dp[x][1]) dp[x][1] = dp[x][0]+dp[v][0]+1,cnt[x][1]=cnt[x][0]*cnt[v][0];
else if(dp[x][0]+dp[v][0]+1 == dp[x][1]) cnt[x][1] += cnt[x][0]*cnt[v][0];
dp[x][0]+=dp[v][1];
if(dp[v][0]!=dp[v][1]) cnt[x][0]*=cnt[v][1];
else cnt[x][0]*=cnt[v][0]+cnt[v][1];
}
if(!dp[x][1]) cnt[x][1]=0;
}
int main() {
init();
dfs(1);
printf("%d\n",dp[1][1]);
if(dp[1][1]!=dp[1][0]) cnt[1][1].print();
else cnt[1][1]+=cnt[1][0],cnt[1][1].print();
return 0;
}

luogu P1623 [CEOI2007]树的匹配Treasury的更多相关文章

  1. [CEOI2007] 树的匹配Treasury

    类型:树形 DP 传送门:>Here< 题意:给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配是多少,并且计算出有多少种最大匹配. 解题思路 首先树形Dp是很明显的,$f[i][ ...

  2. [CEOI2007]树的匹配Treasury(树形DP+高精)

    题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...

  3. Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)

    Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...

  4. [luogu P3384] [模板]树链剖分

    [luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...

  5. bzoj5123 [Lydsy12月赛]线段树的匹配

    题意: 线段树是这样一种数据结构:根节点表示区间 [1, n]:对于任意一个表示区间 [l, r] 的节点,若 l < r, 则取 mid = ⌊l+r/2⌋,该节点的左儿子为 [l, mid] ...

  6. 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索

    题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...

  7. [Luogu 2590] ZJOI2008 树的统计

    [Luogu 2590] ZJOI2008 树的统计 裸树剖不解释. 比板子还简单. #include <algorithm> #include <cstdio> #inclu ...

  8. 【luogu P2590 [ZJOI2008]树的统计】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2590 我想学树剖QAQ #include <cstdio> #include <cstri ...

  9. 【luogu P3372 线段树1】 模板

    线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...

随机推荐

  1. B. Minimum Ternary String (这个B有点狠)

    B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. 移动端list布局,左边固定,右边自适应

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  3. HDU3790---(双权最短路径)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    M ...

  4. 洛谷 PT2 First Step (ファーストステップ)

    题目背景 知らないことばかりなにもかもが(どうしたらいいの?) 一切的一切 尽是充满了未知数(该如何是好) それでも期待で足が軽いよ(ジャンプだ!) 但我仍因满怀期待而步伐轻盈(起跳吧!) 温度差なん ...

  5. [转]树莓派gpio口控制

    0.前言     树莓派现在越来越火,网上树莓派的资料也越来越多.树莓派源自英国,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层出不穷,下面推荐几个. [[开发语言]——python [[ ...

  6. Python学习笔记 - day4 - 流程控制

    Python流程控制 Python中的流程控制主要包含两部分:条件判断和循环. Python的缩进和语法 为什么要在这里说缩进和语法,是因为将要学习的条件判断和分支将会涉及到多行代码,在java.c等 ...

  7. 百度面试题——top K算法

    需求 从一亿个数据中,找出其中最小的10个数. 分析 最笨的方法就是将这一亿个数据,按从小到大进行排序,然后取前10个.这样的话,即使使用时间复杂度为nlogn的快排或堆排,由于元素会频繁的移动,效率 ...

  8. Linux ALSA介绍

    1. 介绍 ALSA(即Advanced Linux Sound Architecture), 是目前Linux的主流音频体系结构, 提供了音频和MIDI的支持, 其架构图如下所示 TIP: 笔者的代 ...

  9. 算法题之Leetcode分糖果

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  10. java.lang.NumberFormatException: multiple points 异常

    平时使用SimpleDateFormat的时候都是在单线程的情况下使用的,今天在改写别人的代码,发现每个类中都会写大量的SimpleDateFormat实例.作为一个程序特有的洁癖开始对代码进行优化. ...