codeforces 613D

原題

原題鏈接

題目大意

給你一顆n100000 個點的樹,然後有q100000 個詢問,每個詢問選定k 個點,問,至少在樹中刪除多少個點,使得n 個點兩兩不連通,無解輸出1
保證k100000

解題思路

首先可以確定的是,無解一定是兩個點相鄰。

其他的情況就要樹形dp。
如果當前點爲非選定點,且子樹中有大於2個選定點,則這個點要刪除。
如果這個點是選定點,且對於其中一個兒子的子樹有選定點,那麼就把這個兒子刪除。

但是每次詢問都把n 個點做樹形dp太耗時間,每次只要把n 個點以及他們的lca 建一顆虛樹,在虛樹上dp就行了。

參考代碼

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define maxn 100005
#define maxsq 20
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

int head[maxn],t[maxn * 2],next[maxn * 2],sum;

int fr[maxn],to[maxn],tim;

int n,m,q;

int fa[maxn][maxsq],deep[maxn];

int a[maxn*2];

bool bz[maxn];

int stack[maxn];

int read(){
    int ret=0,ff=1;
    char ch=getchar();
    while (ch<'0' || ch>'9') {
        if (ch=='-') ff=-1;
        ch=getchar();
    }
    while (ch>='0' && ch<='9') {
        ret=ret*10+ch-'0';
        ch=getchar();
    }
    return ret*ff;
}

bool cmp(int i,int j){
    return fr[i]<fr[j];
}

void insert(int x,int y){
    t[++sum]=y;
    next[sum]=head[x];
    head[x]=sum;
}

void dfs(int x,int father){
    deep[x]=deep[father]+1;
    fr[x]=++tim;
    for(int tmp=head[x];tmp;tmp=next[tmp]) {
        if (t[tmp]==father) continue;
        fa[t[tmp]][0]=x;
        dfs(t[tmp],x);
    }
    to[x]=tim;
}

int getlca(int x,int y){
    if (deep[x]<deep[y]) swap(x,y);
    fd(i,18,0)
        if (deep[fa[x][i]]>=deep[y]) x=fa[x][i];
    if (x==y) return x;
    fd(i,18,0)
        if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}

int last[maxn];

int treedp(int w){
    int ret=0,tot=0;
    for(int tmp=head[w];tmp;tmp=next[tmp]){
        ret+=treedp(t[tmp]);
        tot+=last[t[tmp]];
    }
    if (bz[w]) {
        ret=ret+tot;
        last[w]=1;
    }
    else {
        if (tot>1) ret++;
        if (tot==1) last[w]=1;
        else last[w]=0;
    }
    return ret;
}

int main(){
    n=read();
    fo(i,1,n-1) {
        int x=read(),y=read();
        insert(x,y);
        insert(y,x);
    }
    dfs(1,0);
    fa[1][0]=1;
    fo(i,1,18)
        fo(j,1,n) fa[j][i]=fa[fa[j][i-1]][i-1];
    q=read();
    while (q--) {
        sum=0;
        a[0]=0;
        stack[0]=0;
        m=read();
        fo(i,1,m) {
            int x=read();
            a[++a[0]]=x;
            bz[x]=1;
        }
        bool pd=0;
        fo(i,1,m) {
            int now=a[i];
            if (fa[a[i]][0]!=a[i] && bz[fa[a[i]][0]]) {
                pd=1;
                break;
            }
        }
        if (pd) {
            fo(i,1,a[0]) bz[a[i]]=0;
            puts("-1");
            continue;
        }
        sort(a+1,a+a[0]+1,cmp);
        fo(i,1,m-1) a[++a[0]]=getlca(a[i],a[i+1]);
        sort(a+1,a+a[0]+1,cmp);
        int nowtot=0,last=0;
        fo(i,1,a[0]) {
            if (a[i]==last) continue;
            a[++nowtot]=a[i];
            last=a[i];
        }
        a[0]=nowtot;
        fo(i,1,a[0]) {
            head[a[i]]=0;
            while (stack[0]>0 && !(fr[stack[stack[0]]]<=fr[a[i]] && fr[a[i]]<=to[stack[stack[0]]]))
                stack[0]--;
            if (stack[0]>0) insert(stack[stack[0]],a[i]);
            stack[++stack[0]]=a[i];
        }
        printf("%d\n",treedp(a[1]));
        fo(i,1,a[0]) bz[a[i]]=0;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章