首页 > 其他分享 >[AGC034E] Complete Compress

[AGC034E] Complete Compress

时间:2023-06-19 09:00:53浏览次数:43  
标签:Complete int siz AGC034E Compress maxson 棋子 root sum

[AGC034E] Complete Compress

考虑这道题之前,我们先想一个经典问题:

对于一颗有根树,每个节点上可能放一颗棋子,且不同子树上的棋子可以相互抵消。那么,我们设maxson为最大子树包含的棋子数,sun【root】为root的所有子树的棋子总数,很容易得到,如果sum【root】-maxson>=maxson,那么它们一定可以相互抵消,否则不能。(所以sum【root】必需为奇数)

回到本题,看到N的范围不大,我们可以考虑换根,即枚举每一个节点为根。

可以证明让含有祖先关系的两个节点相互靠近是无用功,因此我们只需考虑将不同子树上的节点抵消。

而对于u节点,若有棋子,则可以将一个棋子拆为dis(u,root)个棋子,那么原题就转化为了上面的经典问题。(下文的棋子均指拆开后的棋子)

设f【x】表示消去以x的子树内的棋子,所需的最小步数。设maxp为最大儿子节点编号,max为最大儿子节点上的棋子个数。和经典问题一样,分两种情况讨论。

 

记得每次换根的时候清零sum

如果f【root】==sum【root】/2,则答案合法。(代码实现的时候,还要判一下sum【root】的奇偶性)

 

#include<bits/stdc++.h>
using namespace std;
const int N=4e3+33;
const int inf=2e9+555;
typedef long long ll;
char c;
int n,cnt,head[N],a[N],siz[N],sum[N],f[N],root,ans;
struct edge{
    int to,nex;
}e[N];
void adda(int u,int v){
    e[++cnt].to=v;e[cnt].nex=head[u];head[u]=cnt;
}
void dfs(int u,int fa){
    if(a[u]) siz[u]=1;
    else siz[u]=0;
    int maxp=0;
    for(int i=head[u];i;i=e[i].nex){
        int v=e[i].to;
        if(v==fa) continue;
        dfs(v,u);
        siz[u]+=siz[v];
        sum[u]+=sum[v]+siz[v];
        if(sum[v]+siz[v]>sum[maxp]+siz[v]) maxp=v;
    }
    int maxson=sum[maxp]+siz[maxp];
    if(sum[u]-maxson>=maxson) f[u]=sum[u]/2;
    else f[u]=sum[u]-maxson+min(f[maxp],maxson-sum[u]/2);
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>c;
        if(c=='1') a[i]=1;
    }
    for(int i=1,u,v;i<n;i++){
        cin>>u>>v;
        adda(u,v);adda(v,u);
    }
    ans=inf;
    for(int i=1;i<=n;i++){
        memset(sum,0,sizeof(sum));
        root=i;
        dfs(root,0);
        if(f[root]==sum[root]/2&&sum[root]%2==0) ans=min(ans,f[root]);
        
    }
    if(ans==inf) cout<<-1;
    else cout<<ans;
    return 0;
}

 

标签:Complete,int,siz,AGC034E,Compress,maxson,棋子,root,sum
From: https://www.cnblogs.com/DongPD/p/17490231.html

相关文章

  • 27) yuicompressor-maven-plugin 合并压缩 js css文件
    http://davidb.github.io/yuicompressor-maven-plugin/compress-mojo.html<plugin><groupId>net.alchim31.maven</groupId><artifactId>yuicompressor-maven-plugin</artifactId><version>1.5.1</......
  • YouCompleteMe插件安装
    Vundle安装YouCompleteMe安装YouCompleteMe对软件版本要求,编译python默认都是静态库,YouCompleteMe需要动态库ProblemsinstallingPython3with--enable-sharedexportLD_RUN_PATH=~/opt/python3.10/lib;./configure--prefix=~/opt/python3.10/--enable-sharedexpo......
  • .net压缩文件(System.IO.Compression.ZipFile)
    NuGet安装System.IO.Compression.ZipFile,注意不是System.IO.Compression优点:不同于ICSharpCode.SharpZipLib.dll的地方是,这个插件可以直接压缩文件夹,文件夹内的文件自动压缩进去了,ICSharpCode.SharpZipLib.dll需要一个一个将文件添加进压缩包,不能直接压缩文件夹1ZipFile.Creat......
  • Complete the Sequence
        #include<iostream>usingnamespacestd;constintN=110;inta[N][N];intmain(){intt;scanf("%d",&t);ints,c;while(t--)//t次测试用例{scanf("%d%d",&s,&c);//s是每次的长度c......
  • Task.CompleteTask和Task.FromResult
    问题描述实现接口中的异步方法时,因为返回值类型是Task或Task<T>,所以即使方法的具体实现逻辑极简执行极快(比如直接返回一个常量字符串),我们可能也需要被迫新建一个Task去执行,如下:publicinterfaceIComputer{TaskDo();Task<string>DoString();}publicclass......
  • CF1819C The Fox and the Complete Tree Traversal
    \(\color{purple}\text{TheFoxandtheCompleteTreeTraversal}\)比较有意思的一题。先考虑一个序列的权值。对长度为\(len\)的序列排序,价值为\(len-1\),那么有时候如果后面的元素很大,前面的很小:321300200100我们可以将序列切为\([1,3]\),和\([4,6]\)两部分分别......
  • 1110 Complete Binary Tree(附测试点2,3,4,6分析)
    题目:Givenatree,youaresupposedtotellifitisacompletebinarytree.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveinteger N (≤20)whichisthetotalnumberofnodesinthetree--andh......
  • 如何在gdb中打印<incomplete type>变量 gdb
    linkvimain1.cpp#include<iostream>#include<fstream>#include<stdio.h>usingnamespacestd;intmain(){ifstreaminFile("test.txt",ios::in);//printf("%p",inFile);cout<<"inFile="&l......
  • Waves 14 Complete Mac (Waves混音效果全套插件)
    Waves14Complete是一款全功能的音频处理软件套装,包含超过140个插件,可用于各种音频处理和音乐制作任务。这个套装包含了多种不同类型的插件,包括均衡器、压缩器、混响、延迟、合成器、调制器等等。Waves14Complete还提供了许多专业级功能,如自适应限制、自动启动时间校准、360度......
  • CF1228D Complete Tripartite
    有些题解够了,这题和三分图的判定没有什么关系……这里主要是一个转化,一个点会和所以不与自己相连的点处于相同的集合中。换句话说,如果两个点在同一个集合内,那与这两个点相连的点的集合是完全相同的。这里使用了哈希来判定,另外,如果有孤立的点存在,则要特判。constintmaxN=1e5+......