# 计蒜客2018蓝桥A组模拟赛（一）青出于蓝胜于蓝（dfs序+线段树）

1 <= n <= 100000

## 代码：

#include <bits/stdc++.h>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
#define lson rt*2,l,(l+r)/2
#define rson rt*2+1,(l+r)/2+1,r
typedef unsigned long long ull;
typedef long long ll;
const int MAXN = 100005;
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int tree[MAXN << 2], n, p, l[MAXN], r[MAXN], times;
vector<int>G[MAXN];

void init() {
times = 0;
ms(tree, 0);
}

void pushup(int rt) {
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}

void dfs(int u, int fa) {
l[u] = ++times;
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (v != fa) {
dfs(v, u);
}
}
r[u] = times;
}

int query(int L, int R, int rt, int l, int r) {
if (L <= l && R >= r) {
return tree[rt];
}
int ret = 0;
if (L <= (l + r) / 2)   ret += query(L, R, lson);
if (R > (l + r) / 2)    ret +=query(L,R,rson);
return ret;
}

void update(int k, int rt, int l, int r) {
if (l == r) {
tree[rt] = 1;
return;
}
if (k <= (l + r) / 2)   update(k, lson);
else    update(k, rson);
pushup(rt);
}

int main() {
ios::sync_with_stdio(false);
cin >> n >> p;
init();
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(p, -1);
// for(int i=1;i<=n;i++){
//  cout <<i << " " << l[i] << " " << r[i] << endl;
// }
for (int i = 1; i <= n; i++) {
cout << query(l[i], r[i], 1, 1, n) << " \n"[i == n];
update(l[i], 1, 1, n);
}

return 0;
}