Wannafly挑战赛1 C MMSet2 虚树

题目链接:https://www.nowcoder.com/acm/contest/15/C

思路:虚树,取两点间的lca,构造成一颗新的树;求(直径+1)/2即可

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=3e5+10,M=1e6+10,inf=1e9+7,MOD=1e9+7;
const LL INF=1e18+10,mod=1e9+7;

struct edge
{
    int v,next;
}edge[N<<1];
int head[N],fa[N][20],edg,deep[N];
int in[N],out[N],tot;
void init()
{
    memset(head,-1,sizeof(head));
    edg=0;tot=0;
}
void add(int u,int v)
{
    edg++;
    edge[edg].v=v;
    edge[edg].next=head[u];
    head[u]=edg;
}
void dfs(int u,int fat)
{
    tot++;
    in[u]=tot;
    for (int i=1; i<=19 ;i++)
        fa[u][i] = fa[fa[u][i-1]][i-1];
    for (int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fat) continue;
        deep[v]=deep[u]+1;
        fa[v][0]=u;
        dfs(v,u);
    }
    out[u]=tot;
}
int RMQ_LCA(int x,int y) {
    if(deep[x]<deep[y]) swap(x,y);
    int d=deep[x]-deep[y];
    for (int i=0; i<=19 ;i++)
        if((1<<i)&d) x=fa[x][i];
    for (int i=19; i>=0 ;i--) {
        if(fa[x][i]!=fa[y][i]) {
            x=fa[x][i];y=fa[y][i];
        }
    }
    if(x==y) return x;
    else return fa[x][0];
}


int h[M],ans,far,pos;
struct f
{
    int v,w,nex;
}edge2[N<<1];
int head2[N],edg2;
void add2(int u,int v,int w)
{
    edge2[++edg2]=(f){v,w,head2[u]};
    head2[u]=edg2;
}
bool cmp(int u, int v) {
    return in[u] < in[v];
}
bool check(int u, int v) {
    return in[u] <= in[v] && in[v] <= out[u];
}
int build(int A[], int tot) {
    std::sort(A + 1, A + 1 + tot, cmp);
    for(int i = 2, old_tot = tot; i <= old_tot; i++) {
        A[++tot] = RMQ_LCA(A[i - 1], A[i]);
    }
    std::sort(A + 1, A + 1 + tot, cmp);
    tot = std::unique(A + 1, A + 1 + tot) - A - 1;

    std::stack<int> S; S.push(A[1]);
    for(int i = 2; i <= tot; i++) {
        while(!S.empty() && !check(S.top(), A[i])) S.pop();
        int u = S.top(), v = A[i];
        add2(u,v,deep[v]-deep[u]);
        add2(v,u,deep[v]-deep[u]);//u, v, deep[v] - deep[u]);
        S.push(v);
    }
    return tot;
}
void dfs1(int u,int fa,int val)
{
    if(val>far)far=val,pos=u;
    for(int i=head2[u];i!=-1;i=edge2[i].nex)
    {
        int v=edge2[i].v;
        int w=edge2[i].w;
        if(v==fa)continue;
        dfs1(v,u,val+w);
    }
}
void dfs2(int u,int fa,int val)
{
    ans=max(ans,val);
    for(int i=head2[u];i!=-1;i=edge2[i].nex)
    {
        int v=edge2[i].v;
        int w=edge2[i].w;
        if(v==fa)continue;
        dfs2(v,u,w+val);
    }
}
int main()
{
    init();
    memset(head2,-1,sizeof(head2));
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);add(v,u);
    }
    dfs(1,0);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        edg2=far=pos=ans=0;
        int t;
        scanf("%d",&t);
        for(int i=1;i<=t;i++)
            scanf("%d",&h[i]);
        t = build(h, t);
        dfs1(h[1],0,0);
        dfs2(pos,0,0);
        printf("%d\n",(ans+1)/2);
        for(int i=1;i<=t;i++)
        head2[h[i]]=-1;
    }
    return 0;
}

 

posted @ 2017-10-16 14:22  jhz033  阅读(338)  评论(0编辑  收藏  举报