VK Cup 2017 - Round 1

【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 简略的解题报告。 阅读详情

感觉自己代码能力太差了,这场比赛的题都调得很痛苦。。

C. Bear and Tree Jumps

  对于一棵有根树来说,dfs可以得到每个节点到树根的距离。注意到跳跃距离最大为5,我们可以把距离最多分为五类,同样地,dfs出所有节点到根的距离信息。
  我的解法分为两次dfs。第一次dfs可以得到节点1为树根时的距离信息。第二次dfs的目的是“换根”,也就是让每个节点以dfs序作为树根,统计与当前根节点有关的所有pair的总距离。

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int maxn = 200010;

vector<int> E[maxn];

ll dis[maxn][5];
int p[maxn];

ll n,k;

ll ans[maxn];

ll res = 0;

void dfs1(int u,int pre){
    dis[u][0] = 1;

    int len = E[u].size();
    for(int i=0;i<len;i++){
        int v = E[u][i];
        if(v==pre)continue;
        p[v] = u;
        dfs1(v,u);

        ans[u] += dis[v][0]-1;
        ans[u] += ans[v];

        for(int i=0;i<k;i++){
            dis[u][i] += dis[v][(i+k-1)%k];
        }
    }
}

void dfs2(int u,int pre){
    int len = E[u].size();
    ll tmp = ans[u];
    if(u!=1)ans[u] += ans[p[u]] + dis[p[u]][0]-1;
    res += ans[u];
    for(int i=0;i<len;i++){
        int v = E[u][i];
        if(v==pre)continue;

        ans[u] -= dis[v][0]-1;
        ans[u] -= ans[v];

        for(int i=0;i<k;i++){
            dis[u][i] -= dis[v][(i+k-1)%k];
        }
        for(int i=0;i<k;i++){
            dis[v][i] += dis[u][(i+k-1)%k];
        }

        dfs2(v,u);

        for(int i=k-1;i>=0;i--){
            dis[v][i] -= dis[u][(i+k-1)%k];
        }
        for(int i=k-1;i>=0;i--){
            dis[u][i] += dis[v][(i+k-1)%k];
        }

        ans[u] += ans[v];
        ans[u] += dis[v][0]-1;
    }

    ans[u] = tmp;
}

int main(){
    cin>>n>>k;

    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        E[u].push_back(v);
        E[v].push_back(u);
    }

    dfs1(1,0);
    dfs2(1,0);

    res += n*(n-1);

    cout<<res/2<<endl;

    return 0;
}

D. Bear and Company

  做这题的时候,如果只提示我是dp的话,我也会往区间分割方面想,之前没写过这种类型的dp,确实比较巧妙。因为每个位置的字符都可以往任意地方跑,于是分区间是不管用的。
  容易发现除了VK以外的字符都是等效的,定义状态 dp(i,j,k) 表示组成前 i+j+k 个字符有 i 个’V’,j个’K’和 k 个其他字符需要的最小次数,再加一维表示最后一个字符是否是’V’。然后枚举添加三种类型的字符。

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int maxn = 200010;

vector<int> E[maxn];

ll dis[maxn][5];
int p[maxn];

ll n,k;

ll ans[maxn];

ll res = 0;

void dfs1(int u,int pre){
    dis[u][0] = 1;

    int len = E[u].size();
    for(int i=0;i<len;i++){
        int v = E[u][i];
        if(v==pre)continue;
        p[v] = u;
        dfs1(v,u);

        ans[u] += dis[v][0]-1;
        ans[u] += ans[v];

        for(int i=0;i<k;i++){
            dis[u][i] += dis[v][(i+k-1)%k];
        }
    }
}

void dfs2(int u,int pre){
    int len = E[u].size();
    ll tmp = ans[u];
    if(u!=1)ans[u] += ans[p[u]] + dis[p[u]][0]-1;
    res += ans[u];
    for(int i=0;i<len;i++){
        int v = E[u][i];
        if(v==pre)continue;

        ans[u] -= dis[v][0]-1;
        ans[u] -= ans[v];

        for(int i=0;i<k;i++){
            dis[u][i] -= dis[v][(i+k-1)%k];
        }
        for(int i=0;i<k;i++){
            dis[v][i] += dis[u][(i+k-1)%k];
        }

        dfs2(v,u);

        for(int i=k-1;i>=0;i--){
            dis[v][i] -= dis[u][(i+k-1)%k];
        }
        for(int i=k-1;i>=0;i--){
            dis[u][i] += dis[v][(i+k-1)%k];
        }

        ans[u] += ans[v];
        ans[u] += dis[v][0]-1;
    }

    ans[u] = tmp;
}

int main(){
    cin>>n>>k;

    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        E[u].push_back(v);
        E[v].push_back(u);
    }

    dfs1(1,0);
    dfs2(1,0);

    res += n*(n-1);

    cout<<res/2<<endl;

    return 0;
}
Dify如何实现自主学习 摘要:Dify通过构建感知-决策-执行-反馈的闭环实现"自主学习",本质是打造能自动获取信息、更新知识库并调整策略的Agent工作流。核心包括:1)建立知识库作为记忆体;2)搭建感知与执行工具获取实时信息;3)通过工作流编排决策"大脑",包含LLM决策节点、行动执行节点和反馈反思节点。典型应用如根据用户反馈优化回答风格,通过存储反思日志到知识库实现持续优化。这种机制使智能体能基于新数据和经验不断自我改进。 阅读详情

相关推荐

codesys【modbusRTU】主站

【代码】modbusRTU【codesys】

cfqq1989的博客 4375

VK Cup 2017 - Round 1 A - Bear and Friendship Condition(并查集维护大小 + dfs 遍历图统计边数)

并查集 dfs

Jacob0824的博客 1105

Unity动态创建Avatar骨骼映射

Unity Avatar骨骼映射动态创建,适配Unity通用动画,无需经过第三方建模软件,可运行时创建

Nbin_Newby的博客 1万+

VK Cup 2017 - Round 1 A. Bear and Friendship Condition

A. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Bear Limak examines a social networ...

weixin_30379973的博客 169

SAP MM 物料描述MAKTX 40个字符不够用的问题处理

SAP物料描述表,长度40个字符一般情况下,40个字符在描述中也够用了,但是要加上规格之类的额外描述就不一定了,特别是对于辅助件-刀具。解决方案一:对maktx增强,换成CHAR80或者CHAR100的,但是风险较大,涉及到好多地方,另外SAP+ 算号器 也不支持。解决方案二:增强字段。内部顾问,建议在 那就在这里加一个字段,具体过程如下:1、创建域2、创建架构3、附加架构4、GUI 设置 5、后台设置6、MASS/MM17字段新增。过程1、创建域 SE112、创建结构 se11 3、MARA附加架构

GAVIN 4730

陷波滤波器设计以及Simulink仿真

离散型陷波滤波器设计以及simulink仿真

hmq252198885的博客 3166

VK Cup 2017 - Round 1 (CDE)

771CBear and Tree Jumps 大意: 给定树,每步能走到距离不超过$k$的任意点,记$f(s,t)$为$s$到$t$的最少步数,求$\sum\limits_{s<t}f(s,t)$ 对于路径$(u,v)$, 假设距离为$d$, 那么贡献为$\lceil\frac{d}{k}\rceil=\frac{d+(-d\text{%}k+k)\text{%}k}{k}...

weixin_30295091的博客 200

多标签分类怎么做?(Python)

首先简单介绍下,多标签分类与多分类、多任务学习的关系: 多分类学习(Multi-class):分类器去划分的类别是多个的,但对于每一个样本只能有一个类别,类别间是互斥的。例如:分类器判断这只动物是猫、狗、猪,每个样本只能有一种类别,就是一个三分类任务。常用的做法是OVR、softmax多分类

y1282037271的博客 1359

VK Cup 2021 - C. Bottom-Tier Reversals(思维,构造)

C. Bottom-Tier Reversals 思路: 首先想一下什么情况下会无解,因为一共有奇数个数字,而且可交换的前缀串长度也是奇数,所以无论如何变换,起初的数字所在位置的奇偶性一定都不会变,然后讨论如何构造,我们只需要以下5步便可以确定最后2个数字,依次搞定所有数字即可 代码: #include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define closeSync ios::sync_with_stdio(0)

人工智能方向,计算机视觉(https://github.com/chenshunpeng) 446

MAX31865三线PT100测温实战:配置、接线与代码全解析

本文详细解析MAX31865三线PT100测温的配置、接线与代码实现。通过实战经验分享,介绍如何利用MAX31865芯片实现高精度温度测量,重点讲解三线制接法的自动误差补偿功能,并提供完整的寄存器配置、SPI通信和温度转换算法,帮助开发者快速解决工业测温中的常见问题。

ss78901的博客 757

python实现纵横向拉开档次法 (附完整源码)

python实现纵横向拉开档次法 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 779

VK CUP2017 ROUND 1

来自FallDream的博客。未经允许,请勿转载,谢谢。 ---------------------------------------------------- 和ditoly组队打vkcup,原来以为是正常div2难度,结果发现.....不说了 AB都被叉了一遍,又掉分 ....... ------------------------- A.给定n个人和m组朋友关系,判断这个图是否满足...

weixin_30757793的博客 147

PID、模糊PID、SkyHook、LQR、H2/H∞、ADRC等悬架控制合集

PID、模糊PID、SkyHook、LQR、H2/H∞等悬架控制合集

Mr.邹的博客 2013

Codeforces VK Cup 2017 - Round 1 C 树形dp 双解法

VK Cup 2017 - Round 1 C

weixin_58424264的博客 196

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E

Description Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is g...

219

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)

题意:给一个顶点和边不超过15W的图,问你这个图的所有连通分量是不是一个完全子图(if the graph is a set of disjoint cliques),对此,我们只需要对所有连通分量跑一下DFS,看看每个连通分量中的边数和顶点数是不是满足完全子图的关系就可以了 cnt_edges != (long long) cnt_vertices * (cnt_vertices - 1)#in

Accepted丶 410

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)                                                            全场题解    菜鸡只会A+B+C,呈上题解:                                    

NYIST_TC_LYQ的博客 825

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D

Description A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them. Limak is a little polar bear. He li...

169

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) A

Description Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak and Bob weighaandbrespectively. It's guaranteed that Limak...

144

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B

Description Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There ...

135
上一篇: Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
下一篇: Codeforces problem 750E New Year and Old Subsequence
squee_spoon
博客等级 码龄14年 61粉丝 455原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值