[杂题 背包DP] Codeforces 793E Tinkoff Challenge
来源:互联网 发布:ubuntu 字体路径 编辑:程序博客网 时间:2024/06/05 17:39
因为都经过根 那么只要考虑根的那么些子树就好了
然后要求 a-b之间是n/2个点 c-d之间是n/2个点
四个点的顺序是 a-c-b-d 或 a-d-b-c
那么背包做一下就好了
If solution of both knapsacks exists, then answer is «YES», otherwise «NO». We can always order items from both knapsacks in needed way. Exactly, all subtrees that are present only in solution of a-b knapsack we place on path between a and c, subtrees that are present in solutions of both knapsacks we place between c and b, subtrees that are present only in solution of c-d knapsack we place between b and d, and subtrees that aren’t present in any solutions we place between d and a.
ps.题解下面的评论里提及了优化方法
#include<cstdio>#include<cstdlib>#include<algorithm>#include<bitset>using namespace std;#define read(x) scanf("%d",&(x))const int N=5005;struct edge{ int u,v,next;}G[N];int head[N],inum;inline void add(int u,int v,int p){ G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p; }#define V G[p].vint n;int a,b,c,d;int A,B,C,D;int size[N],tot;int vst[N],pnt;inline void dfs(int u,int fa,int z){ for (int p=head[u];p;p=G[p].next) if (V!=fa) dfs(V,u,z); if (!head[u]){ size[z]++; tot++; if (u==a) A=z,vst[z]=1; if (u==b) B=z,vst[z]=1; if (u==c) C=z,vst[z]=1; if (u==d) D=z,vst[z]=1; }}bitset<N> t,ab,cd;int main(){ int x; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(a); read(b); read(c); read(d); for (int i=2;i<=n;i++) read(x),add(x,i,++inum); t[0]=1; for (int p=head[1];p;p=G[p].next){ ++pnt; dfs(V,1,pnt); if (!vst[pnt]) t|=t<<size[pnt]; } if (tot&1) return printf("NO\n"),0; ab=t; for (int i=0;i<size[A];i++) ab|=ab<<i; for (int i=0;i<size[B];i++) ab|=ab<<i; cd=t; for (int i=0;i<size[C];i++) cd|=cd<<i; for (int i=0;i<size[D];i++) cd|=cd<<i; if ((tot/2-1-size[C]>=0 && ab[tot/2-1-size[C]] && tot/2-1-size[B]>=0 && cd[tot/2-1-size[B]]) || (tot/2-1-size[D]>=0 && ab[tot/2-1-size[D]] && tot/2-1-size[B]>=0 && cd[tot/2-1-size[B]])) printf("YES\n"); else printf("NO\n"); return 0;}
阅读全文
0 0
- [杂题 背包DP] Codeforces 793E Tinkoff Challenge
- [Segment tree Beats! || 分块] Codeforces 793F Tinkoff Challenge
- Tinkoff Challenge
- Tinkoff Challenge
- Tinkoff Challenge
- Codeforces 235E. Number Challenge DP
- [扫描线 二分图最大匹配 线段树优化网络流] Codeforces 793G Tinkoff Challenge
- [Updating] Tinkoff Challenge
- [占坑]Tinkoff Challenge
- Codeforces 148E Porcelain [预处理+dp背包]
- CodeForces 148E Porcelain dp+背包(水
- codeforces 148E Porcelain(DP, 分组背包)
- CodeForces 148E Porcelain(dp+背包)
- Codeforces 864E Fire【排序+背包dp】
- Codeforces Round #360 (Div. 2) E dp 类似01背包
- codeforces 864E Fire (排序+背包dp)
- codeforces 864E 背包
- codeforces 235E. Number Challenge (反演)
- JAVA 类加载时机与过程
- codeforce811b Vladik and Complicated Book(都是不好好想题惹的祸)
- static关键字
- Hibernate学习(3) (继承映射,配置文件和注解版)
- 数据库连接池
- [杂题 背包DP] Codeforces 793E Tinkoff Challenge
- Eclipse使用笔记
- OpenMP
- C# winform 初学者各种问题集合(已经本人验证能有效解决问题)
- Makefile的编写等
- 全球顶级15个技术类博客
- Django Error: No module named XXX
- C语言实现int转换字符串的一种实现
- 彻头彻尾理解 ConcurrentHashMap