codechef Heavy-light Decompositions
Heavy-light Decompositions Problem Code: HLDOTSSubmit
Heavy-light decomposition of a tree is a powerful tool that often helps in the most difficult tree data structure problems.
Heavy-light decomposition is to be built on a rooted tree. In this problem, the node with the number 1 should be considered the root of a tree. Heavy light decomposition is a colouring of edges of the tree. Each edge is either heavy or light. For each non leaf node (node having degree greater than 1), from all the edges emanating from it into the subtree rooted at this vertex should have exactly one heavy edge.
The heavy-light decomposition is called correct, if you can reach any node from the root node by using no more that ⌊ log2 N ⌋ light edges, where N is the number of nodes in the tree.
Given a tree, calculate the number of its' correct heavy-light decompositions. As answer could be very large, please print it modulo 19101995.
Input
There is exactly one test case.
The first line of input consists of a single integer N, denoting the number of the nodes in the tree.
Each of the following N - 1 lines contains a pair of integers, denoting the numbers of the nodes that are connected with an edge. The nodes are enumerated by positive integers in the range [1; N].
Output
Output the number of correct heavy-light decompositions of the given tree. Since this number can be huge, please output it modulo 19101995.
Constraints
- (Subtask 1): 1 ≤ N ≤ 20 - 21 point.
- (Subtask 2): 1 ≤ N ≤ 1000 - 34 points.
- (Subtask 3): 1 ≤ N ≤ 100000 - 45 points.
Example
Input:
7
1 2
3 1
3 4
3 5
2 6
2 7 Output:
8
Explanation
Example case 1. Input is a complete binary tree. It consists of 7 nodes, therefore you can't have more than ⌊log2 7⌋ = ⌊(2.80735492206)⌋ = 2 light edges on the path from the root node to any other one. But the tree's height is 2, so you can choose the decomposition in any way you like. All the decompositions will be correct ones. There are three nodes that has outgoing edges from them (in the direction opposite to the root's one), their numbers are 1, 2 and 3. Each of them has 2 outgoing edges from which you can colour exactly one of them heavy, so overall you'll have 2 * 2 * 2 = 8 options of creating the correct heavy-light decompositions.
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const long long maxn=1e5+10,mod=19101995;
long long n,f[maxn][20],g[maxn][20],ans[maxn][20],fa[maxn],sz,fr[2*maxn]; long long aa;char cc;
long long read() {
aa=0;cc=getchar();
while(cc<'0'||cc>'9') cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
return aa;
} long long fir[maxn],nxt[2*maxn],to[2*maxn],e=0;
void add(long long x,long long y) {
to[++e]=y;nxt[e]=fir[x];fir[x]=e;
to[++e]=x;nxt[e]=fir[y];fir[y]=e;
} long long rs;
long long qp(long long x,long long k) {
rs=1;if(x<=1) return x;
while(k) {
if(k&1) (rs*=x)%=mod;
x=x*x%mod;k>>=1;
}
return rs;
} void dfs(long long pos) {
long long z,tt=0,tot;
for(long long y=fir[pos];y;y=nxt[y]) {
if((z=to[y])==fa[pos]) continue;
fa[z]=pos; dfs(z);
}
for(long long i=0;i<=sz;++i) f[0][i]=1ll;
for(long long y=fir[pos];y;fr[nxt[y]]=y,y=nxt[y]) {
if((z=to[y])==fa[pos]) continue; tt++;
for(long long i=0;i<=sz;++i) (f[tt][i]=f[tt-1][i]*ans[z][i])%=mod;
}
tot=tt;
for(long long i=0;i<=sz;++i) g[tt][i]=1ll;
fr[fir[pos]]=0;
for(long long y=fr[0];y;y=fr[y]) {
if((z=to[y])==fa[pos]) continue; tt--;
for(long long i=0;i<=sz;++i) (g[tt][i]=g[tt+1][i]*ans[z][i])%=mod;
}
if(tot>1) for(long long y=fir[pos];y;y=nxt[y]) {
if((z=to[y])==fa[pos]) continue; tt++;
for(long long i=1;i<=sz;++i) (ans[pos][i]+=f[tt-1][i-1]*g[tt][i-1]%mod*ans[z][i])%=mod;
}
else if(!tot)for(long long i=0;i<=sz;++i) ans[pos][i]=1;
else {
if(to[fr[0]]==fa[pos]) fr[0]=fr[fr[0]];
for(long long i=0;i<=sz;++i) ans[pos][i]=ans[to[fr[0]]][i];
}
} int main() {
n=read();long long x,y;
sz=(long long)((double)log(n)/(double)log(2)+1e-8);
for(long long i=1;i<n;++i) {
x=read();y=read();
add(x,y);
}
dfs(1);
printf("%lld",ans[1][sz]);
return 0;
}
/*
7
1 2
3 2
4 3
5 1
6 2
7 3 right answer:
6
*/
对拍的rand:
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;
const int maxn=15;
int n; int main() {
srand((unsigned)time(NULL));
n=rand()%maxn+2;int x;
printf("%d\n1 2\n",n);
for(int i=3;i<=n;++i) {
x=rand()%(i-1)+1;
printf("%d %d\n",i,x);
}
return 0;
}
codechef Heavy-light Decompositions的更多相关文章
- Heavy Light Decomposition
Note 1.DFS1 mark all the depth mark fathers mark the heavy/light children mark the size of each subt ...
- 树链剖分I 原理
树链剖分(Heavy Light Decomposition, HLD)是一种将对[树上两点间的路径]上[边或点]的[修改与查询]转化到[序列]上来处理的方法. 目的:将树的边或点转化到一个线性结构( ...
- ACM/ICPC 之 拓扑排序-反向(POJ3687)
难点依旧是题意....需要反向构图+去重+看题 POJ3687-Labeling Balls 题意:1-N编号的球,输出满足给定约束的按原编号排列的重量序列,如果有多组答案,则输出编号最小的Ball重 ...
- poj1013
题目大意:假造的银币 Sally Jones有一些游客给的银币,但是只有11枚是真正的银币(有一枚是假的),从颜色和大小是无法区分真比还是假币的,但是它的重量和真币是不同的,Sally Jones它是 ...
- Java8 in action(1) 通过行为参数化传递代码--lambda代替策略模式
[TOC] 猪脚:以下内容参考<Java 8 in Action> 需求 果农需要筛选苹果,可能想要绿色的,也可能想要红色的,可能想要大苹果(>150g),也可能需要红的大苹果.基于 ...
- TCP与UDP区别小结
TCP(Transmission Control Protocol):传输控制协议 UDP(User Datagram Protocol):用户数据报协议 主要从连接性(Connectiv ...
- POJ1013 称硬币
题目链接:http://poj.org/problem?id=1013 题目大意 有12枚硬币.其中有11枚真币和1枚假币.假币和真币重量不同,但不知道假币比真币轻还是重.现在,用一架天平称了这些币三 ...
- 神奇的树上启发式合并 (dsu on tree)
参考资料 https://www.cnblogs.com/zhoushuyu/p/9069164.html https://www.cnblogs.com/candy99/p/dsuontree.ht ...
- HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树
题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...
随机推荐
- Shell 语法之用户输入
bash shell 提供了一些不同的方法从用户处获取数据,这些方法包括命令行参数.命令行选项和直接读取键盘输入. 命令行参数 bash shell 将在命令行中输入的所有参数赋值给一些特殊变量,称为 ...
- python实例5-表格打印
编写一个名为printTable()的函数,它接受字符串的列表的列表,将它显示在组织良好的表格中,每列右对齐.假定所有内层列表都包含同样数目的字符串.例如,该值可能看起来像这样: table_data ...
- 通过pyppeteer来爬取今日头条
import asyncio from pyppeteer import launch async def main(): browser = await launch() page = await ...
- Oracle VM VirtualBox安装增强功能和共享文件夹的方法
一.介绍下如何安装增强功能 1)选择安装增强功能的安装包有2种方法,分别如下: 第一种:找到设备->安装增强功能 第二种 1)找到设备->点击选择虚拟盘. 2)找到VirtualBox-& ...
- Luogu P2678 跳石头(二分)
P2678 跳石头 题意 题目背景 一年一度的"跳石头"比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起 ...
- H5C3--background中cover,背景样式,提升响应区域+精灵图的使用
一.cover的使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 文件上传之Java篇
最近做了一个下载文档的功能,于是联想到了上传功能,于是自己研究了一下后台语言是java的情况下怎样实现将文件上传到指定的目录,以下是项目的语言: 1.后台用jfinal框架 2.前台用jquery提交 ...
- fastjson string[]转 json字符串
@RequestMapping(value = "/friendlink_list/province_list", produces = {"application/js ...
- 常用长度单位PX/EM/PT/百分比转换公式与对照表
PX.PT.EM.ex和in等都是我们常用的长度单位,尤其在网页的字体大小中经常用到.但是你知道PX.PT和EM之间是如何进行准换的吗?这里icech为大家找到了一个px.pt.em和percent大 ...
- 学习线程池源码--ThreadPoolExecutor
1 创建ThreadPoolExecutor ThreadPollExecutor有四个构造函数,但本质上都是调用这一个构造函数. public ThreadPoolExecutor(int core ...