CodeForces 379F-New Year Tree(LCA+直径)

New Year Tree

time limit per test 2 seconds
memory limit per test 256 megabytes

You are a programmer and you have a New Year Tree (not the traditional fur tree, though) — a tree of four vertices: one vertex of degree three (has number \(1\)), connected with three leaves (their numbers are from \(2\) to \(4\)).

On the New Year, programmers usually have fun. You decided to have fun as well by adding vertices to the tree. One adding operation looks as follows:

First we choose some leaf of the tree with number v.
Let's mark the number of vertices on the tree at this moment by variable n, then two vertexes are added to the tree, their numbers are \(n+1\) and \(n+2\), also you get new edges, one between vertices \(v\) and \(n+1\) and one between vertices \(v\) and \(n+2\).
Your task is not just to model the process of adding vertices to the tree, but after each adding operation print the diameter of the current tree. Come on, let's solve the New Year problem!

Input
The first line contains integer \(q (1 ≤ q ≤ 5*10^5)\) — the number of operations. Each of the next \(q\) lines contains integer \(v_i (1 ≤ v_i ≤ n)\) — the operation of adding leaves to vertex \(v_i\). Variable \(n\) represents the number of vertices in the current tree.

It is guaranteed that all given operations are correct.

Output
Print q integers — the diameter of the current tree after each operation.

Examples
input

5
2
3
4
8
5

output

3
4
4
5
6

Solution

首先要知道一个定理,在一棵树中,每次加1个节点,直径最多增长1
而且一定是有一个顶点是没有移动的(感性认识一下就能发现)。
那么我们每次要一个节点新增两个相邻的节点该怎么办??
其实是跟增长一个节点一样的,因为这两个点是连向同一个点的
这样的话我们就只需要维护直径的两个端点就好了(有多个的话随便两个)
我们先令1为根,每次新增点的时候维护倍增数组
再与当前直径的两个端点用\(lca\)计算一下距离,如果直径大了就更新
时间复杂度是\(O(nlogn)\)

Warning
有一个需要特别注意的地方,数组的范围大小至少开两倍(因为要加两个点)
我就是因为没有注意这个细节结果TLE了???!!!

Code

#include <cstdio>
#include <algorithm>
#define N 2000010
#define P 21

using namespace std;

inline int read() {
    int x = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x;
}

struct Node{
    int to, nxt;
}e[N];

int tot, cnt, q, d1, d2, prelen, lst[N], dep[N], c[N][P];

inline void add(int u, int v) {
    e[++tot].to = v;
    e[tot].nxt = lst[u];
    lst[u] = tot;
}

void dfs(int x, int fa) {
    dep[x] = dep[fa] + 1;
    c[x][0] = fa;
    for (int i = 1; i < P; ++i)
        c[x][i] = c[c[x][i - 1]][i - 1];
    for (int i = lst[x]; i; i = e[i].nxt) {
        if (e[i].to == fa) continue;
        dfs(e[i].to, x);
    }
}

inline int lca(int x, int y) {
    if (dep[x] != dep[y]) {
        if (dep[x] < dep[y]) swap(x, y);
        int jump = dep[x] - dep[y];
        for (int i = 0; i < P; ++i)
            if (jump & (1 << i)) x = c[x][i];
    }
    if (x == y) return x;
    for (int i = P - 1; i >= 0; --i)
        if (c[x][i] != c[y][i]) {
            x = c[x][i];
            y = c[y][i];
        }
    return c[x][0];
}

inline void init() {
    add(1, 2);
    add(1, 3);
    add(1, 4);
    cnt = 4;
    q = read();
    d1 = 2;
    d2 = 3;
    dfs(1, 1);
    prelen = 2;
}

inline void solve() {
    for (int i = 1; i <= q; i++) {
        int x;
        x = read();
        add(x, ++cnt);
        add(x, ++cnt);
        dfs(x, c[x][0]);
        int l1 = lca(x, d1);
        int l2 = lca(x, d2);
        int len1 = dep[x] + dep[d1] - dep[l1] * 2, len2 = dep[x] + dep[d2] - dep[l2] * 2;
        if (len1 + 1 <= prelen && len2 + 1 <= prelen) printf("%d\n", prelen);
        else
        if (len1 > len2) {
            prelen = len1 + 1;
            printf("%d\n", prelen);
            d2 = cnt;
        }
        else {
            prelen = len2 + 1;
            printf("%d\n", prelen);
            d1 = cnt;
        }
    }
}

int main() {
    init();
    solve();
    return 0;
}

转载于:https://www.cnblogs.com/Fxkkks/p/9922443.html

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值