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<=105) ( 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
2011 Multi-University Training Contest 5 - Host by BNU
题意:
给你一颗树,在以i为根节点的子树中,输出编号比它小的节点总个数;
分析:
用DFS序预处理一下,没有更新直接查询,发现复杂度过不去。有个小技巧需要用,初始为空的线性结构,按编号依次查询区间,查询后更新把这个点插进去,统计区间和就行。本题需要注意输入时没有指明边的方向,建树时建双边;这道题卡空间,线段树会MLE。。。
附:树状数组详解
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 1;
int c[MAXN];
int in[MAXN], out[MAXN];
vector<int> G[MAXN];
int time, n, p;
void init() {
time = 0;
for(int i = 0; i < MAXN; i++) {
G[i].clear();
}
memset(c, 0, sizeof(c));
}
void dfs(int x, int fa) {
in[x] = ++time;
for(int i = 0; i < G[x].size(); i++) {
int cnt = G[x][i];
if(cnt == fa) continue;
dfs(cnt, x);
}
out[x] = time;
}
int lowbit(int x) {
return x & (-x);
}
void updata(int k, int v) {
while(k <= n) {
c[k] += v;
k += lowbit(k);
}
}
int query(int k) {
int sum = 0;
while(k > 0) {
sum += c[k];
k -= lowbit(k);
}
return sum;
}
int main() {
while(scanf("%d %d", &n, &p) && (n + p)) {
init();
for(int i = 1; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(p, -1);
for(int i = 1; i <= n; i++) {
int L = in[i];
int R = out[i];
if(i > 1) printf(" ");
printf("%d", query(R) - query(L - 1));
updata(L, 1);
}
puts("");
}
return 0;
}
空间开辟太大,线段树会MLE:
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 1;
int sum[MAXN * 4];
int in[MAXN], out[MAXN];
vector<int> G[MAXN];
int time;
void init() {
time = 0;
for(int i = 0; i < MAXN; i++) {
G[i].clear();
}
memset(sum, 0, sizeof(sum));
}
void dfs(int x, int fa) {
in[x] = ++time;
for(int i = 0; i < G[x].size(); i++) {
int cnt = G[x][i];
if(cnt == fa) continue;
dfs(cnt, x);
}
out[x] = time;
}
void pushup(int root) {
sum[root] = sum[root << 1] + sum[root << 1 | 1];
}
void updata(int root, int L, int R, int x) {
if(L == R) {
sum[root] = 1;
return ;
}
int mid = (L + R) >> 1;
if(x <= mid) updata(root << 1, L, mid, x);
else if(x > mid) updata(root << 1 | 1, mid + 1, R, x);
pushup(root);
}
int query(int root, int L, int R, int l, int r) {
if(l <= L && R <= r) {
return sum[root];
}
int ans = 0;
int mid = (L + R) >> 1;
if(l <= mid) ans += query(root << 1, L, mid, l, r);
if(r > mid) ans += query(root << 1 | 1, mid + 1, R, l, r);
return ans;
}
int main() {
int n, p;
while(scanf("%d %d", &n, &p) && (n + p)) {
init();
for(int i = 1; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(p, -1);
for(int i = 1; i <= n; i++) {
int L = in[i];
int R = out[i];
if(i > 1) printf(" ");
printf("%d", query(1, 1, n, L, R));
updata(1, 1, n, L);
}
puts("");
}
return 0;
}