Codeforces 1276D/1259G Tree Elimination (树形DP)
题目链接
http://codeforces.com/contest/1276/problem/D
题解
我什么DP都不会做,吃枣药丸……
设\(f_{u,j}\)表示\(u\)子树内,\(j=0\)要求\(u\)点在轮到其父边之前被删,\(j=1\)要求\(u\)点被其父边删掉,\(j=2\)要求\(u\)点在其父边之后被删或者最后没有被删。
转移: 设儿子有\(s\)个,分别为\(v_1,v_2,...,v_s\), 且按边的编号从小到大排序,父边编号位于\(d\)和\((d+1)\)之间。
枚举被哪条边删除。
\]
\]
\]
维护前后缀积即可。
时间复杂度\(O(n)\).
代码
#include<bits/stdc++.h>
#define llong long long
#define pii pair<int,int>
#define mkpr make_pair
using namespace std;
inline int read()
{
int x = 0,f = 1; char ch = getchar();
for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
return x*f;
}
const int N = 2e5;
const int P = 998244353;
vector<pii> adj[N+3];
int fa[N+3],fae[N+3];
llong aux1[N+3],aux2[N+3];
llong f[N+3][3];
int n,en;
void dfs(int u)
{
sort(adj[u].begin(),adj[u].end()); int faid = -1,adjn = adj[u].size();
for(int i=0; i<adj[u].size(); i++)
{
int o = adj[u][i].first,v = adj[u][i].second;
if(v==fa[u]) {faid = i; continue;} fa[v] = u,fae[v] = o;
dfs(v);
}
aux1[0] = 1ll;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i].second; if(v==fa[u]) {aux1[i+1] = aux1[i]; continue;}
aux1[i+1] = aux1[i]*(f[v][0]+f[v][1])%P;
}
aux2[adj[u].size()+1] = 1ll;
for(int i=(int)adj[u].size()-1; i>=0; i--)
{
int v = adj[u][i].second; if(v==fa[u]) {aux2[i+1] = aux2[i+2]; continue;}
aux2[i+1] = aux2[i+2]*(f[v][0]+f[v][2])%P;
}
f[u][0] = 0ll;
for(int i=0; i<faid; i++)
{
int v = adj[u][i].second;
llong tmp = aux1[i]*f[v][2]%P*aux2[i+2]%P; f[u][0] = (f[u][0]+tmp)%P;
}
if(faid!=-1) {f[u][1] = aux1[faid]*aux2[faid+2]%P;}
f[u][2] = 0ll;
for(int i=faid+1; i<adj[u].size(); i++)
{
int v = adj[u][i].second;
llong tmp = aux1[i]*f[v][2]%P*aux2[i+2]%P; f[u][2] = (f[u][2]+tmp)%P;
}
f[u][2] = (f[u][2]+aux1[adjn])%P;
}
int main()
{
scanf("%d",&n);
for(int i=1; i<n; i++)
{
int u,v; scanf("%d%d",&u,&v);
adj[u].push_back(mkpr(i,v)); adj[v].push_back(mkpr(i,u));
}
dfs(1);
printf("%I64d\n",f[1][2]);
return 0;
}
Codeforces 1276D/1259G Tree Elimination (树形DP)的更多相关文章
- Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)
题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...
- Codeforces 804D Expected diameter of a tree(树形DP+期望)
[题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...
- codeforces 161 D. Distance in Tree(树形dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最 ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- HDU5834 Magic boy Bi Luo with his excited tree(树形DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...
- BZOJ-3227 红黑树(tree) 树形DP
个人认为比较好的(高端)树形DP,也有可能是人傻 3227: [Sdoi2008]红黑树(tree) Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1 ...
- codeforces 337D Book of Evil (树形dp)
题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...
- 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
随机推荐
- Java Web 深入分析(6) Tomcat
tomcat是什么:汤姆猫?Javaweb服务器? Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache ...
- call、apply、bind一直是不求甚解!
一直感觉代码中有call和apply就很高大上(看不懂),但是都草草略过,今天非要弄明白!以前总是死记硬背:call.apply.bind 都是用来修改函数中的this,传参时,call是一个个传参, ...
- SQLAlchemy 在查询期间丢失与MySQL服务器的连接
遇到问题 pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') 建立的 pymysq ...
- Java基础加强-读取配置文件和内省
Java读取配置文件 1.采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来. String path = "/WEB-INF/jdbc_conne ...
- 前阿里P8架构师谈如何设计优秀的API
随着大数据.公共平台等互联网技术的日益成熟,API接口的重要性日益凸显,从公司的角度来看,API可以算作是公司一笔巨大的资产,公共API可以捕获用户.为公司做出许多贡献.对于个人来说,只要你编程,你就 ...
- python 爬虫抓取 MOOC 中国课程的讨论区内容
一:selenium 库 selenium 每次模拟浏览器打开页面,xpath 匹配需要抓取的内容.可以,但是特别慢,相当慢.作为一个对技术有追求的爬虫菜鸡,狂补了一些爬虫知识.甚至看了 scrapy ...
- spring-boot-actuator 常用配置
management: endpoints: web: base-path: "/" exposure: include: "*" endpoint: heal ...
- Rocketmq 集群部署
10.1.0.178 配置文件 broker-a-m.properties brokerClusterName=PaymentClusterbrokerName=broker-anamesrvAddr ...
- How to resolve emulator cannot be launched issue by command line
Issue: Resolution: 1. Open the system variables, find the Path and edit it, add below item : C:\User ...
- 连接mongodb服务器
连接mongodb有几种方法 一种是使用mongodb编译时生成的客户端进行连接,就是我们之前介绍过的mongo客户端 另一种是使用各种驱动进行连接 这次使用mongo客户端进行连接,之前我们启动了一 ...