Tree Cutting (Hard Version) CodeForces - 1118F2 (树形DP,计数)
大意:给定树, 每个点有颜色, 一个合法的边集要满足删除这些边后, 每个连通块内颜色仅有一种, 求所有合法边集的个数
$f[x][0/1]$表示子树$x$中是否还有与$x$连通的颜色
对于每种颜色已经确定了一个连通块, 连通块内部一定不能断边, 有转移
$$f[x][1]=\prod (f[y][0]+f[y][1]),f[x][0]=0$$
能断边的部分只能为不同颜色连通块间的无色结点, 有转移
$$f[x][0]=\prod (f[y][0]+f[y][1]), f[x][1]=\sum\limits_y (f[y][1]\prod\limits_{z!=y}(f[z][0]+f[z][1])) $$
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head
#ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k;
vector<int> g[N], q;
int col[N], c[N], cnt[N];
int f[N][2], prod[N]; void dfs(int x, int fa) {
cnt[x] = col[x]>0;
for (int y:g[x]) if (y!=fa) {
dfs(y,x);
if (!col[x]) col[x]=col[y];
else if (col[y]&&col[x]!=col[y]) {
puts("0"), exit(0);
}
cnt[x] += cnt[y];
}
q.clear();
for (int y:g[x]) if (y!=fa) q.pb(y);
prod[0] = 1;
int sz = q.size();
REP(i,0,sz-1) {
int y = q[i];
prod[i+1]=(ll)prod[i]*(f[y][0]+f[y][1])%P;
}
if (col[x]) f[x][1]=prod[sz];
else {
f[x][0]=prod[sz];
int tmp = 1;
PER(i,0,sz-1) {
int y = q[i];
f[x][1] = (f[x][1]+(ll)tmp*f[y][1]%P*prod[i]%P)%P;
tmp = (ll)tmp*(f[y][0]+f[y][1])%P;
}
}
if (c[col[x]]==cnt[x]) cnt[x]=col[x]=0;
} int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) scanf("%d", col+i);
REP(i,1,n) ++c[col[i]];
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs(1,0);
printf("%d\n", f[1][1]);
}
Tree Cutting (Hard Version) CodeForces - 1118F2 (树形DP,计数)的更多相关文章
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces 1118 F2. Tree Cutting (Hard Version) 优先队列+树形dp
题目要求将树分为k个部分,并且每种颜色恰好在同一个部分内,问有多少种方案. 第一步显然我们需要知道哪些点一定是要在一个部分内的,也就是说要求每一个最小的将所有颜色i的点连通的子树. 这一步我们可以将所 ...
- 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)
题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...
- Codeforces Round #277 (Div. 2)D(树形DP计数类)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CodeForces 219D 树形DP
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 解题:CF1118F2 Tree Cutting (Hard Version)
题面 好题不问Div(这是Div3最后一题,不得不说Mike真是强=.=) 首先同一个颜色的点的LCA要和它们在一个划分出的块里,那么我们先按颜色把所有点到它们的LCA的路径涂色,如果这个过程中出现了 ...
- Codeforces 1153D 树形DP
题意:有一个游戏,规则如下:每个点有一个标号,为max或min, max是指这个点的值是所有子节点中值最大的那一个,min同理.问如何给这颗树的叶子节点赋值,可以让这棵树的根节点值最大. 思路:很明显 ...
- Codeforces 1088E 树形dp+思维
比赛的时候看到题意没多想就放弃了.结果最后D也没做出来,还掉分了,所以还是题目做的太少,人太菜. 回到正题: 题意:一棵树,点带权值,然后求k个子连通块,使得k个连通块内所有的点权值相加作为分子除以k ...
随机推荐
- 数据仓库基础(四)ODS、元数据
本文转载自:http://www.cnblogs.com/evencao/archive/2013/06/14/3135691.html ODS的概念:是一个面向主题的.集成的.可变的.反应当前细节的 ...
- python中repr和eval可以用来在数据结构和字符串间互转
在这个功能上,repr和str的作用一样,把一个数据结构转换成字符串,例如: >>> str([1,2,3,4])'[1, 2, 3, 4]' >>> repr([ ...
- python进程join()函数理解
Join()是主程序等我这个进程执行完毕了,程序才往下走
- printf("%d",5.01)和printf("%f",5)的输出结果
printf(); printf("%d\n",5.01); printf(); printf(.f); 输出结果: 看到结果,会感觉非常奇怪.1处怎么会输出0呢?2又为何会显示这 ...
- win10锁屏界面无法更新
win10的锁屏界面都是巨硬公司推送过来的,质量还不错,最近锁屏界面无法更新,解决方案如下: 以管理员身份运行cmd,分别运行如下两个命令 del /f /s /q /a "%userpro ...
- 20145225 《网络对抗》逆向及Bof基础实践
实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- noip 2012 提高组 day2 部分题解
这道题有多种解法,我用的是扩展欧几里得算法求到的答案 #include<iostream> #include<fstream> #include<cstdio> u ...
- 【转载】浅谈JavaScript,let和var定义变量的区别
了解JS与ES5与ES6区别 JS语言 JavaScript一种动态类型.弱类型.基于原型的客户端脚本语言,用来给HTML网页增加动态功能. 动态: 在运行时确定数据类型.变量使用之前不需要类型声明, ...
- HDU 4734 (数位DP)题解
思路: dp[pos][pre]代表长度为pos的不大于pre的个数 #include<iostream> #include<cstdio> #include<cstri ...
- 【第十五章】 springboot + pojo默认值设置
我们有时需要给POJO设置默认值 pojo设置(推荐) 1.User package com.xxx.firstboot.domain; import lombok.Getter; import lo ...