zoukankan      html  css  js  c++  java
  • New Barns

    New Barns

    时间限制: 1 Sec  内存限制: 128 MB

    题目描述

    Farmer John notices that his cows tend to get into arguments if they are packed too closely together, so he wants to open a series of new barns to help spread them out.
    Whenever FJ constructs a new barn, he connects it with at most one bidirectional pathway to an existing barn. In order to make sure his cows are spread sufficiently far apart, he sometimes wants to determine the distance from a certain barn to the farthest possible barn reachable from it (the distance between two barns is the number of paths one must traverse to go from one barn to the other).

    FJ will give a total of Q (1≤Q≤105) queries, each either of the form "build" or "distance". For a build query, FJ builds a barn and links it with at most one previously built barn. For a distance query, FJ asks you the distance from a certain barn to the farthest barn reachable from it via a series of pathways. It is guaranteed that the queried barn has already been built. Please help FJ answer all of these queries.

    输入

    The first line contains the integer Q. Each of the next Q lines contains a query. Each query is of the form "B p" or "Q k", respectively telling you to build a barn and connect it with barn pp, or give the farthest distance, as defined, from barn k. If p=−1, then the new barn will be connected to no other barn. Otherwise, p is the index of a barn that has already been built. The barn indices start from 1, so the first barn built is barn 1, the second is barn 2, and so on.

    输出

    Please write one line of output for each distance query. Note that a barn which is connected to no other barns has farthest distance 0.

    样例输入

    7
    B -1
    Q 1
    B 1
    B 2
    Q 3
    B 2
    Q 2
    

    样例输出

    0
    2
    1
    

    提示

    The example input corresponds to this network of barns:
      (1) 
           
         (2)---(4)
        /
      (3)
    In query 1, we build barn number 1. In query 2, we ask for the distance of 1 to the farthest connected barn. Since barn 1 is connected to no other barns, the answer is 0. In query 3, we build barn number 2 and connect it to barn 1. In query 4, we build barn number 3 and connect it to barn 2. In query 5, we ask for the distance of 3 to the farthest connected barn. In this case, the farthest is barn 1, which is 2 units away. In query 6, we build barn number 4 and connect it to barn 2. In query 7, we ask for the distance of 2 to the farthest connected barn. All three barns 1, 3, 4 are the same distance away, which is 1, so this is our answer.

    分析:题意:给定一个森林,在建森林的过程中有一些询问,距离某个点最远的点的距离;

       考虑分治,对于每个点的祖先,要么经过祖先,要么不经过;

       如果经过,考虑维护这个祖先的最大的两个点的深度,这两个点不在同一棵子树且已经被标记;更新答案分在不在同一子树里即可;

       如果不经过,则可以递归到子树,对于分治来说,维护重心,每个点有log个祖先;

       注意如果两个点被标记则lca也被标记过;

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10,mod=1e9+7,inf=0x3f3f3f3f;
    int n,m,k,t,rt,a[maxn],sz[maxn],all;
    bool vis[maxn];
    vector<int>e[maxn];
    struct node
    {
        int mx1,mx2,mxid;
        vector<pair<int,int> >anc;
    }p[maxn];
    int getroot(int x,int y=0)
    {
        for(int i=0;i<e[x].size();i++)
        {
            int z=e[x][i];
            if(z==y||vis[z])continue;
            if(sz[z]*2>=all)return getroot(z,x);
        }
        return x;
    }
    void getdep(int x,int y,int dep)
    {
        p[x].anc.push_back({rt,dep});
        for(int i=0;i<e[x].size();i++)
        {
            int z=e[x][i];
            if(z==y||vis[z])continue;
            getdep(z,x,dep+1);
        }
    }
    void getsz(int x,int y=0)
    {
        sz[x]=1;
        for(int i=0;i<e[x].size();i++)
        {
            int z=e[x][i];
            if(z==y||vis[z])continue;
            getsz(z,x);
            sz[x]+=sz[z];
        }
    }
    void dfs(int x)
    {
        getsz(x);
        all=sz[x];
        rt=getroot(x);
        getdep(rt,0,0);
        vis[rt]=true;
        for(int i=0;i<e[rt].size();i++)
        {
            int z=e[rt][i];
            if(!vis[z])dfs(z);
        }
    }
    int main()
    {
        int i,j;
        scanf("%d",&m);
        int pos=0;
        for(i=1;i<=m;i++)
        {
            char str[2];
            scanf("%s%d",str,&n);
            if(str[0]=='B')
            {
                a[i]=++pos;
                if(n==-1)continue;
                else e[n].push_back(pos),e[pos].push_back(n);
            }
            else a[i]=-n;
        }
        for(i=1;i<=pos;i++)if(!vis[i])dfs(i);
        pos=0;
        for(i=1;i<=m;i++)
        {
            int last=-1;
            if(a[i]>0)
            {
                ++pos;
                for(j=p[pos].anc.size()-1;j>=0;j--)
                {
                    int fa=p[pos].anc[j].first,w=p[pos].anc[j].second;
                    if(p[fa].mx1<=w)
                    {
                        if(p[fa].mxid!=last)p[fa].mx2=p[fa].mx1;
                        p[fa].mx1=w;
                        p[fa].mxid=last;
                    }
                    else if(p[fa].mx2<=w)
                    {
                        if(last!=p[fa].mxid)p[fa].mx2=w;
                    }
                    last=fa;
                }
            }
            else
            {
                int now=-a[i];
                int ret=p[now].mx1;
                for(j=p[now].anc.size()-1;j>=0;j--)
                {
                    int fa=p[now].anc[j].first,w=p[now].anc[j].second;
                    if(fa>pos)
                    {
                        last=fa;
                        continue;
                    }
                    if(last==p[fa].mxid)ret=max(ret,w+p[fa].mx2);
                    else ret=max(ret,w+p[fa].mx1);
                    last=fa;
                }
                printf("%d
    ",ret);
            }
        }
        return 0;
    }
  • 相关阅读:
    使用jmail方式在服务器上发送邮件正文乱码
    oracle创建分区表
    oracle 在线重定义
    DDD(领域驱动设计)总结
    聚类算法
    bag-of-words 词袋模型
    ICPC昆明区域赛&#183;赛前挣扎复习题
    2021年寒假训练题目合集
    2019 ICPC 南昌 Regional 部分题解
    使用multus实现管理网和业务网分离——calico和flannel共存
  • 原文地址:https://www.cnblogs.com/dyzll/p/8694378.html
Copyright © 2011-2022 走看看