题解 CF613D 【Kingdom and its Cities】

考虑树形\(DP\),设\(num_x\)记录的为当\(1\)为根时,以\(x\)为子树中重要城市的个数。

那么进行分类讨论:

① 当\(num_x≠0\)时,则需将其所有满足\(num_y≠0\)的儿子\(y\)删去。

② 当\(num_x=0\)时,若满足\(num_y≠0\)的儿子\(y\)个数\(cnt=1\),则直接让\(num\)进行向上传递,若满足\(num_y≠0\)的儿子\(y\)个数\(cnt>1\),则需删去\(x\)本身。

不合法的情况特判掉。

考虑到多次询问和树上点集的特性,考虑用虚树来优化\(DP\)。

多次建虚树时记得清空\(num\),但不要用\(memset\),无法保证复杂度,应记录虚树上的点,只清零这些点。

其他实现细节就看代码吧。

\(code:\)

#include<bits/stdc++.h>
#define maxn 200010
using namespace std;
template<typename T> inline void read(T &x)
{
    x=0;char c=getchar();bool flag=false;
    while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    if(flag) x=-x;
}
int n,q,ans,tmp_cnt;
bool flag;
int query[maxn],tmp[maxn],num[maxn];
struct edge
{
    int to,nxt;
}e[maxn];
int head[maxn],edge_cnt;
void add(int from,int to)
{
    e[++edge_cnt]=(edge){to,head[from]};
    head[from]=edge_cnt;
}
int dfn_cnt;
int de[maxn],dfn[maxn],top_fa[maxn],fa[maxn],son[maxn],siz[maxn];
void dfs_son(int x,int fath)
{
    siz[x]=1;
    fa[x]=fath;
    de[x]=de[fath]+1;
    for(int i=head[x];i;i=e[i].nxt)
    {
        int y=e[i].to;
        if(y==fath) continue;
        dfs_son(y,x);
        siz[x]+=siz[y];
        if(siz[son[x]]<siz[y]) son[x]=y;
    }
}
void dfs_chain(int x,int tp)
{
    dfn[x]=++dfn_cnt,top_fa[x]=tp;
    if(son[x]) dfs_chain(son[x],tp);
    for(int i=head[x];i;i=e[i].nxt)
    {
        int y=e[i].to;
        if(dfn[y]) continue;
        dfs_chain(y,y);
    }
}
int lca(int x,int y)
{
    while(top_fa[x]!=top_fa[y])
    {
        if(de[top_fa[x]]<de[top_fa[y]]) swap(x,y);
        x=fa[top_fa[x]];
    }
    if(dfn[x]>dfn[y]) swap(x,y);
    return x;
}
bool cmp(const int &a,const int &b)
{
    return dfn[a]<dfn[b];
}
int st[maxn],top;
void insert(int x)
{
    if(x==1) return;
    if(top==1)
    {
        st[++top]=x;
        return;
    }
    int anc=lca(x,st[top]);
    if(anc==st[top])
    {
        st[++top]=x;
        return;
    }
    while(top>1&&dfn[anc]<=dfn[st[top-1]]) add(st[top-1],st[top]),top--,tmp[++tmp_cnt]=st[top];
    if(anc!=st[top]) add(anc,st[top]),st[top]=anc,tmp[++tmp_cnt]=st[top];
    st[++top]=x;
}
void dp(int x)
{
    int cnt=0,sum=0;
    for(int i=head[x];i;i=e[i].nxt)
    {
        int y=e[i].to;
        dp(y);
        if(num[x]&&num[y]) ans++;
        if(!num[x]&&num[y])
        {
            cnt++;
            sum+=num[y];
        }
    }
    if(!num[x])
    {
        if(cnt==1) num[x]+=sum;
        if(cnt>1) ans++;
    }
    head[x]=0;
}
void clear()
{
    edge_cnt=0;
    memset(head,0,sizeof(head));
}
int main()
{
    read(n);
    for(int i=1;i<n;++i)
    {
        int a,b;
        read(a),read(b);
        add(a,b),add(b,a);
    }
    dfs_son(1,0);
    dfs_chain(1,1);
    clear();
    read(q);
    while(q--)
    {
        int k;
        read(k);
        edge_cnt=tmp_cnt=flag=ans=0;
        for(int i=1;i<=k;++i)
        {
            read(query[i]);
            num[query[i]]++;
        }
        for(int i=1;i<=k;++i)
        {
            if(num[fa[query[i]]])
            {
                puts("-1");
                flag=true;
                break;
            }
        }
        if(flag)
        {
            for(int i=1;i<=k;++i) num[query[i]]=0;
            continue;
        }
        sort(query+1,query+k+1,cmp);
        st[top=1]=1,tmp[tmp_cnt=1]=1;
        for(int i=1;i<=k;++i) insert(query[i]);
        while(top) add(st[top-1],st[top]),top--,tmp[++tmp_cnt]=st[top];
        dp(1);
        for(int i=1;i<=tmp_cnt;++i) num[tmp[i]]=0;
        for(int i=1;i<=k;++i) num[query[i]]=0;
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lhm-/p/12229858.html

时间: 2024-07-30 00:23:33

题解 CF613D 【Kingdom and its Cities】的相关文章

CF613D Kingdom and its Cities 虚树

传送门 $\sum k \leq 100000$虚树套路题 设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$点存在$0/1$个没有被封住的关键点时的最小代价,转移考虑$i$是否是关键点,随便转就行了 1 #include<bits/stdc++.h> 2 //This code is written by Itst 3 using namespace std; 4 5 inline int read(){ 6 int a = 0; 7 bool f = 0; 8

CF613D Kingdom and its Cities

题目链接 问题分析 首先看数据范围不难发现是虚树. 但是这个DP怎么写的我这么难受-- 应该是不难的DP,\(F[i][0]\)表示\(i\)不占领,\(F[i][1]\)表示\(i\)占领,然后分类讨论--具体的见代码吧-- 参考程序 #include <bits/stdc++.h> using namespace std; const int Maxn = 100010; const int INF = 1000010; const int MaxLog = 20; struct edge

Kingdom and its Cities - CF613D

Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marri

【CF613D】Kingdom and its Cities

题目 题目链接:https://codeforces.com/problemset/problem/613/D 一个王国有 \(n\) 座城市,城市之间由 \(n-1\) 条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重要城市一旦被占领,这座城市就不能通行. 国王定了若干选择重要城市的计划,他想知道,对于每个计划,外敌至少要占领多少个非重要城市,才会导致重要城市之间两两不连通.如果外敌无论如何都不可能导致

codeforces 613D:Kingdom and its Cities

Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daug

CodeForces - 613D:Kingdom and its Cities(虚树+DP)

Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marri

【模板】虚树

核心思想: (听名字高大上,实际上没什么东西……虚树的题主要难在如何操作虚树) 给出$k$个关键点,我们要建出一棵只包含这些关键点和他们$lca$的点数最少的树,以实现$dp$等操作. 标志性的数据范围是$\sum{k}\leq 10^{5}$之类的. 建树方法: 1.将所有关键点按$dfs$序排序. 2.开一个栈表示根到当前点的虚树路径,并把根丢进去. 3.对于每一个关键点$u$: 若栈中只有根这一个元素,则把$u$丢进去. 否则,我们需要弹出栈中所有不在根到$u$路径上的点. 我们用$lca

hdu 5441 Travel 离线带权并查集

Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m

2017 ICPC/ACM 沈阳区域赛HDU6223

Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1262    Accepted Submission(s): 224 Problem Description The ant Welly now dedicates himself to urban infrastructure. He c