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.
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
Author
bnugong
Source
题意:给你一棵树,你需要求每个子树中序号比其小的个数;
首先我们先建立图,之后他告诉了我们起始点。把一个图转换成可以利用线段树/树状数组处理的线性结构,就用到了DFS序列。
DFS序:通过DFS为每个点重新分配位置,并获得每个点包含所有子节点的左右范围。
例如:
10 5 5 3 5 8 3 4 3 1 2 1 6 7 8 7 9 8 8 10我们dfs序,记录首次访问和再次访问的顺序。例如5为1和10. 我们如果要查5的个数,就要从1-10查询。 例如3的为2和5,我们查询2-5..
之后就是普通的树状数组了。
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.
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
Author
bnugong
Source
题意:给你一棵树,你需要求每个子树中序号比其小的个数;
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int tree[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int c)
{
while(x<N)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int getnum(int x)
{
int sum=0;
while(x)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int query(int L,int R)
{
return getnum(R)-getnum(L-1);
}
struct Edge
{
int v,next;
}edge[N<<1];
int head[N<<1];
int in[N],out[N],tot;
int n,p,cnt;
void init()
{
memset(tree,0,sizeof(tree));
memset(head,-1,sizeof(head));
cnt=1;
tot=0;
}
void add(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u,int fa)
{
in[u]=++tot;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa)continue;
dfs(v,u);
}
out[u]=tot;
}
int main()
{
while(~scanf("%d%d",&n,&p))
{
init();
if(n==0&&p==0)break;
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(p,-1);
for(int i=1;i<=n;i++)
{
printf("%d%c",query(in[i],out[i]),((i==n)?'\n':' '));
update(in[i],1);
}
}
return 0;
}
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.
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
Author
bnugong
Source
题意:给你一棵树,你需要求每个子树中序号比其小的个数;