HDU3887(树dfs序列+树状数组)
Counting Offspring
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2424 Accepted Submission(s): 838
Problem Description
You are given a tree, it’s root is p, and the node is numbered from 1 to n. Now define f(i) as the number of nodes whose number is less than i in all the succeeding nodes of node i. Now we need to calculate f(i) for any possible i.
Input
Multiple cases (no more than 10), for each case:
The first line contains two integers n (0<n<=10^5) and p, representing this tree has n nodes, its root is p.
Following n-1 lines, each line has two integers, representing an edge in this tree.
The input terminates with two zeros.
Output
For each test case, output n integer in one line representing f(1), f(2) … f(n), separated by a space.
Sample Input
15 7
7 10
7 1
7 9
7 3
7 4
10 14
14 2
14 13
9 11
9 6
6 5
6 8
3 15
3 12
0 0
Sample Output
0 0 0 0 0 1 6 0 3 1 0 0 0 2 0
思路: dfs序+树状数组。c++可调整栈的大小.#pragma comment(linker,"/STACK:1024000000,1024000000") 两个1024000000均为字节数。
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN+MAXN];
int n,root;
int head[MAXN],tot;
int lch[MAXN],rch[MAXN],key;
int bit[MAXN];
int res[MAXN];
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
}
void dfs(int u,int fa)
{
lch[u]=++key;
for(int i=head[u];i!=-;i=es[i].net)
{
int v=es[i].to;
if(v!=fa)
{
dfs(v,u);
}
}
rch[u]=key;
}
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=(i&-i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&-i);
}
return s;
}
int main()
{
while(scanf("%d%d",&n,&root)!=EOF&&(n+root)!=)
{
memset(bit,,sizeof(bit));
memset(head,-,sizeof(head));
memset(res,,sizeof(res));
tot=;
key=;
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(root,-);
for(int i=;i<=n;i++)
{
res[i]=sum(rch[i])-sum(lch[i]-);
add(lch[i],);
}
for(int i=;i<n;i++)
{
printf("%d ",res[i]);
}
printf("%d\n",res[n]);
}
return ;
}
第二版:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN=;
vector<int> arc[MAXN];
int n,root;
int bit[MAXN];
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=(i&(-i));
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&(-i));
}
return s;
}
int res[MAXN],vis[MAXN];
void dfs(int u)
{
vis[u]=;
res[u]=sum(u);
add(u,);
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(!vis[to])
{
dfs(to);
}
}
res[u]=sum(u)-res[u]-;
}
int main()
{
while(scanf("%d%d",&n,&root)!=EOF&&(n||root))
{
memset(bit,,sizeof(bit));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) arc[i].clear();
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
arc[u].push_back(v);
arc[v].push_back(u);
}
dfs(root);
for(int i=;i<n;i++)
{
printf("%d ",res[i]);
}
printf("%d\n",res[n]);
}
return ;
}
HDU3887(树dfs序列+树状数组)的更多相关文章
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2022 Solved: 1158[Submit][Sta ...
- bzoj2434 fail树 + dfs序 + 树状数组
https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- dfs序 线段树 dfs序列 主席树
并查集 #include<stdio.h> ]; void sset(int x) { ;i<=x;i++) stt[i]=i; } int ffind(int x) { if(x= ...
- POJ3321(dfs序列+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25711 Accepted: 7624 Descr ...
- 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组
Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...
- POJ 2763 Housewife Wind(DFS序+LCA+树状数组)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11419 Accepted: 3140 D ...
- BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!
真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...
随机推荐
- 3.6《深入理解计算机系统》笔记(四)虚拟存储器,malloc,垃圾回收【插图】
概述 ●我们电脑上运行的程序都是使用虚拟存储,跟物理内存根本不搭边. ●既然虚拟内存是在磁盘上的,为什么它又运行这么好,并没有感觉卡顿?这要感谢程序的局部性! ●虚拟存储器的调度是一个操作系统必须做好 ...
- Qt使用QCustomPlot开发
一.入门 1.下载源文件http://www.qcustomplot.com/: 2.把.cpp和.h放在工程目录下,并将cpp和h加入工程: 3.在.pro中:QT += printsupport: ...
- gcc编译c、c++入门
一.c语言 1.在当前目录下新建c文件 $:vim hello.c 2.按i进入编辑模式.按esc退出编辑模式,输入源代码 #include <stdio.h> int main(void ...
- Virtual Container Hosts(VCHs) 介绍
In vSphere Integrated Containers, you deploy virtual container hosts (VCHs) that serve as Docker API ...
- [Android]动态加载/热部署框架汇总
1.DroidPlugin 用途:动态加载 使用案例:360手机助手 GitHub地址:https://github.com/Qihoo360/DroidPlugin ppt介绍:https://gi ...
- centos 源码安装php5.5
系统环境: CentOS 6.5 / 7.0 x86_64 Fedora 20 x86_64下载 PHP 源码包 # wget http://cn2.php.net/distributions/php ...
- 【codevs1028】花店橱窗布置(费用流)
这几天刚学了费用流,找到了这道题来练一练手. 题目: 题目描述 Description 假设以最美观的方式布置花店的橱窗,有F束花,V个花瓶,我们用美学值(一个整数)表示每束花放入每个花瓶所产生的美学 ...
- java定时任务Quartz Demo(2.X)
直接上代码 public class HelloQuartz implements Job{ @Override public void execute(JobExecutionContext Jec ...
- 在 Students 的 Index 页面增加列标题链接(排序),分页,过滤和分组功能
3-1 在 Students 的 Index 页面增加列标题链接 为 Index 页面增加排序的功能,我们需要修改 Student 控制器的 Index 方法,还需要为 Student 视图增加代码 ...
- SoapUI使用方法-01发送http请求
soap ui http://blog.csdn.net/russ44/article/details/51680083 一.发送HTTP请求消息 1.打开soapUI. 2.新建一个项目,实例如下: ...