Counting Offspring(hdu3887)
Counting Offspring
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2759 Accepted Submission(s): 956
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.
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.
1 #include<stdio.h>
2 #include<algorithm>
3 #include<queue>
4 #include<stdlib.h>
5 #include<iostream>
6 #include<string.h>
7 #include<set>
8 #include<map>
9 #include<vector>
10 using namespace std;
11 typedef long long LL;
12 typedef vector<int>Ve;
13 vector<Ve>vec(100005);
14 bool flag[100005];
15 int l[100005];
16 int r[100005];
17 int id[100005];
18 int cn = 0;
19 void dfs(int n);
20
21 int tree[100005*4];
22 void up(int l,int r,int k,int nn,int mm);
23 int ask(int l,int r,int k,int nn,int mm);
24 int main(void)
25 {
26 int n,p;
27 while(scanf("%d %d",&n,&p),n!=0&&p!=0)
28 { cn = 0;
29 for(int i = 0;i < 100005;i++)
30 vec[i].clear();
31 memset(flag,0,sizeof(flag));
32 for(int i = 0;i < n-1;i++)
33 {
34 int x,y;
35 scanf("%d %d",&x,&y);
36 vec[x].push_back(y);
37 vec[y].push_back(x);
38 }
39 dfs(p);
40 memset(tree,0,sizeof(tree));
41 for(int i = 1;i <= n;i++)
42 {
43 if(i == 1)
44 printf("%d",ask(l[i],r[i],0,1,cn));
45 else printf(" %d",ask(l[i],r[i],0,1,cn));
46 up(l[i],l[i],0,1,cn);
47 }
48 printf("\n");
49 }
50 return 0;
51 }
52 void dfs(int n)
53 {
54 flag[n] = true;
55 l[n] = ++cn;
56 for(int i = 0;i < vec[n].size();i++)
57 {
58 int d = vec[n][i];
59 if(!flag[d])
60 dfs(d);
61 }r[n] = cn;
62 }
63 void up(int l,int r,int k,int nn,int mm)
64 {
65 if(l > mm||r < nn)
66 {
67 return ;
68 }
69 else if(l <= nn&&r >= mm)
70 {
71 tree[k]++;return ;
72 }
73 up(l,r,2*k+1,nn,(nn+mm)/2);
74 up(l,r,2*k+2,(nn+mm)/2+1,mm);
75 tree[k] = tree[2*k+1]+tree[2*k+2];
76 }
77 int ask(int l,int r,int k,int nn,int mm)
78 {
79 if(l > mm||r < nn)
80 {
81 return 0;
82 }
83 else if(l <= nn&&r >= mm)
84 {
85 return tree[k];
86 }
87 else
88 {
89 int nx = ask(l,r,2*k+1,nn,(nn+mm)/2);
90 int ny = ask(l,r,2*k+2,(nn+mm)/2+1,mm);
91 return nx + ny;
92 }
93 }
Counting Offspring(hdu3887)的更多相关文章
- hdu3887 Counting Offspring
Counting Offspring HDU - 3887 问你对于每个节点,它的子树上标号比它小的点有多少个 /* 子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它 ...
- HDU3887 Counting Offspring [2017年6月计划 树上问题03]
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3887 Counting Offspring(DFS序+树状数组)
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg
这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y 以节点x的权值修改为y. 2.Q x ...
- 杭电 3887 Counting Offspring
根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...
- HDU 3887 Counting Offspring (树状数组+人工模拟栈)
对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...
随机推荐
- BAT的一些题
114.java中实现多态的机制是什么 答:重写,重载.方法的重写Overriding和重载Overloading是Java多态性的不同表现. 重写Overriding是父类与子类之间多态性的一种表 ...
- TOMCAT 搭建
第一步:下载 软件 和 JDK 第二个:https://www.oracle.com/java/technologies/javase-jdk16-downloads.html 传输到Linux里. ...
- 巩固javaweb的第二十一天
巩固内容:对输入信息进行验证 JavaScript 语言 在 Web 应用中需要在客户端执行的功能可以使用 JavaScript 语言编写,在使用的时候 需要把 JavaScript 代码放在下面的两 ...
- Hadoop入门 集群常用知识与常用脚本总结
目录 集群常用知识与常用脚本总结 集群启动/停止方式 1 各个模块分开启动/停止(常用) 2 各个服务组件逐一启动/停止 编写Hadoop集群常用脚本 1 Hadoop集群启停脚本myhadoop.s ...
- 学习java 7.15
学习内容: 进程:正在运行的程序 是系统进行资源分配和调用的独立单位 每个进程都有它自己的内存空间和系统资源 线程:是进程中的单个顺序控制流,是一条执行路径 单线程:一个进程如果只有一条执行路径,则称 ...
- Spark(十七)【SparkStreaming需求练习】
目录 一.环境准备 1.pom文件 2.bean 3.工具类 JDBCUtils Properties工具类 3.创建BaseApp 需求一:动态添加黑名单 需求二:广告点击量实时统计 需求三:最近一 ...
- Linux基础命令---nslookup查询域名工具
nslookup nslookup是一个查询DNS域名的工具,它有交互和非交互两种工作模式. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- linux 定时导出sql查询结果文件
如果想在服务器端生成sql查询结果的txt文件. 大体思路就是: 1.创建一个到处txt文件的sql脚本. set ARRAYSIZE 50 --从数据库往客户端一次发送记录数 set linesiz ...
- MyBatis中sql实现时间查询的方法
<if test="startTime != null and startTime !=''"> AND lTime >= #{startTime} </i ...
- spring注解-扩展原理
AnnotationConfigApplicationContext(IOC容器)的有参构造方法中,在refresh()里对这些组件进行初始化 BeanPostProcessor bean后置处理器, ...