Gym - 102431 K Gym - 102431K (CCPC2019final DSU on tree)

Christmas is still one month away, but Mr. Panda already starts the Christmas preparation. Mr. Panda is decorating a Christmas tree with a set of Russian dolls. There are nn Russian dolls numbered 1,2,…,n1,2,…,n. The ithith doll is designed to be perfectly nested inside the (i+1)th(i+1)th doll for all 1≤i≤n−11≤i≤n−1. Nesting dolls are stable only if they have neighboring ordinal numbers, otherwise the smaller one will slide out from the larger one. Dolls can be nested recursively. For example, the nn dolls can be nested all the way up from smallest to largest until there is only one doll left.

The Christmas tree happens to have nn nodes with one Russian doll dangling on each node, where the doll numbered 11 is put at the tree root. Mr. Panda will invite his friend Mr. Sheep to collect some dolls from the Christmas tree as gifts on Christmas Eve. Mr. Sheep will pick a tree node and collect all the dolls in the sub-tree with the selected node as the sub-tree root.

As there could be a lot of dolls, Mr. Sheep want to nest the dolls he collects for easy carrying. He wonders for each tree node, how many dolls will be ended up if he nests them as many as possible. Note that the dolls should be stably nested.

Input

The first line of input gives the number of test cases TT (1≤T≤101≤T≤10). TT test cases follow.

Each test case starts with a line containing an integer nn (1≤n≤2×1051≤n≤2×105), the number of dolls and also the number of tree nodes.

The next (n−1)(n−1) lines each contains two integers xx and yy (1≤x,y≤n1≤x,y≤n), denoting the dolls numbered xx and yy are neighbors in the Christmas tree.

It is guaranteed that the sum of nn in all cases is not greater than 106106.

Output

For each test case, the output consists one line starts with "Case #x:", where x is the test case number (starting from 11), followed by next nn integers. The ithith (1≤i≤n1≤i≤n) integer indicates the number of dolls will be ended up if Mr. Sheep selects the tree node that contains the doll numbered ii, collects all the dolls in the sub-tree, and nests the dolls stably as many as possible.

Example

Input

1
7
1 2
2 4
2 6
1 3
3 5
3 7

Output

Case #1: 1 3 3 1 1 1 1

题意:

统计所有子树中包含的连续数字段数

dsu on tree的板子题,but本人并没有明白update函数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 2e5 + 10;

int n, m;
vector<int>mp[N];
int siz[N], son[N], cnt;
bool flag[N], vis[N];
int ans[N];

void init() {
    cnt = 0;
    for(int i = 1; i <= n; ++i) mp[i].clear();
    memset(son, 0, sizeof(son));
    memset(flag, 0, sizeof(flag));
    memset(vis, 0, sizeof(vis));
}

void dfs1(int u, int fa) {
    siz[u] = 1;
    int sz = mp[u].size();
    for(int i = 0; i < sz; ++i) {
        int v = mp[u][i];
        if(v == fa) continue;
        dfs1(v, u);
        siz[u] += siz[v];
        if(siz[v] > siz[son[u]]) son[u] = v;
    }
}

void update(int u, int fa, bool flg) {
    int sum = flag[u - 1] + flag[u + 1];
    if(flg) {
        if(sum == 0) cnt++;
        else if(sum == 2) cnt--;
    }
    else {
        if(sum == 0) cnt--;
        else if(sum == 2) cnt++;
    }
    flag[u] = flg;
    int sz = mp[u].size();
    for(int i = 0; i < sz; ++i) {
        int v = mp[u][i];
        if(v == fa || vis[v]) continue;
        update(v, u, flg);
    }
}

void dfs2(int u, int fa, bool keep) {
    int sz = mp[u].size();
    for(int i = 0; i < sz; ++i) {
        int v = mp[u][i];
        if(v == fa || v == son[u]) continue;
        dfs2(v, u, 0);
    }
    if(son[u]) {
        dfs2(son[u], u, 1);
        vis[son[u]] = 1;
    }
    update(u, fa, 1);
    if(son[u]) vis[son[u]] = 0;
    ans[u] = cnt;
    if(!keep)
        update(u, fa, 0);
}

int main() {
    int t, kcase = 0, u, v;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        init();
        for(int i = 1; i < n; ++i) {
            scanf("%d%d", &u, &v);
            mp[u].push_back(v);
            mp[v].push_back(u);
        }
        dfs1(1, -1);
        dfs2(1, -1, 0);
        printf("Case #%d:", ++kcase);
        for(int i = 1; i <= n; ++i) {
            printf(" %d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/108943494
今日推荐