Counting Offspring

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2759    Accepted Submission(s): 956

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
思路:dfs序+线段树;
首先dfs序线性化,然后我们知道,如果某点是另个点的孩子节点,那么他必然在被另一个点的父亲节点所包括,所以从小到大查询[l[i],r[i]]区间的和,往线段树加点单点更新。复杂度n*log(n);
 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 }
 
Sample Output
0 0 0 0 0 1 6 0 3 1 0 0 0 2 0
 

Counting Offspring(hdu3887)的更多相关文章

  1. hdu3887 Counting Offspring

    Counting Offspring HDU - 3887 问你对于每个节点,它的子树上标号比它小的点有多少个 /* 子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它 ...

  2. HDU3887 Counting Offspring [2017年6月计划 树上问题03]

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. HDU 3887 Counting Offspring(DFS序+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. 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 ...

  6. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  7. 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 ...

  8. 杭电 3887 Counting Offspring

    根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...

  9. HDU 3887 Counting Offspring (树状数组+人工模拟栈)

    对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...

随机推荐

  1. 零基础学习java------36---------xml,MyBatis,入门程序,CURD练习(#{}和${}区别,模糊查询,添加本地约束文件) 全局配置文件中常用属性 动态Sql(掌握)

    一. xml  1. 文档的声明 2. 文档的约束,规定了当前文件中有的标签(属性),并且规定了标签层级关系 其叫html文档而言,语法要求更严格,标签成对出现(不是的话会报错) 3. 作用:数据格式 ...

  2. 答应我,这次必须搞懂!痛点难点Promise。(小点心async/await,基于Promise的更优方案)

    Promise 出现的原因 在 Promise 出现以前,我们处理一个异步网络请求,大概是这样: // 请求 代表 一个异步网络调用. // 请求结果 代表网络请求的响应. 请求1(function( ...

  3. 100个Shell脚本——【脚本8】每日生成一个文件

    [脚本8]每日生成一个文件 要求:请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为)2017-07-05.log, 并且把磁盘的使用情况写到到这个文件中,(不用考虑c ...

  4. Linux磁盘分区(一)之fdisk命令

    Linux磁盘分区(一)之fdisk命令转自:https://www.cnblogs.com/machangwei-8/p/10353683.html 一.fdisk 的介绍fdsik 能划分磁盘成为 ...

  5. 【AWS】【TroubleShooting】EC2实例无法使用SSH远程登陆(EC2 failure for SSH connection)

    1. Login AWS web console and check the EC2 instance.

  6. web管理的Powerdns

    在powerdns服务器上安装相应的包(基于epel源) [root@powerdns ~]# yum install pdns pdns-backend-mysql -y 在master-maira ...

  7. 【Java 8】Optional 使用

    一.前言 如果要给 Java 所有异常弄个榜单,我会选择将 NullPointerException 放在榜首.这个异常潜伏在代码中,就像个遥控炸弹,不知道什么时候这个按钮会被突然按下(传入 null ...

  8. 【C/C++】习题3-7 DNA/算法竞赛入门经典/数组与字符串

    [题目] 输入m组n长的DNA序列,要求找出和其他Hamming距离最小的那个序列,求其与其他的Hamming距离总和. 如果有多个序列,求字典序最小的. [注]这道题是我理解错误,不是找出输入的序列 ...

  9. 【科研】科研【合同】盖章流程/横向&#183;非涉密/电子科技大学

    [前置手续] 一.在科研管理系统里填单子,立项. 二.科研管理系统审核通过后,对于对面给好的合同,在合同系统里选择[合同业务发起-发起非标准合同],填单子. 三.会有一系列的审核,审核完成后打印合同备 ...

  10. 解决Vulnhub靶机分配不到IP问题

    没法找到他的 ip,可能是网卡配置问题 (之前打开 .ova 文件默认联网方式是桥接,改成NAT) 在开机选择的时候,摁 e 编辑一下 把 ro 改成 rw single init=/bin/bash ...