CodeForces 734 E.Anton and Tree

遗传算法详解 附python代码实现 遗传算法 遗传算法是用于解决最优化问题的一种搜索算法。从名字来看,遗传算法借用了生物学里达尔文的进化理论:”适者生存,不适者淘汰“,将该理论以算法的形式表现出来就是遗传算法的过程。 问题引入 上面提到遗传算法是用来解决最优化问题的,下面我将以求二元函数: def F(x, y): return 3*(1-x)**2*np.exp(-(x**2)-(y+1)**2)- 10*(x/5 - x**3...... 阅读详情

E. Anton and Tree

time limit per test 3 seconds
memory limit per test 256 megabytes

Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.

There are n vertices in the tree, each of them is painted black or white. Anton doesn’t like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).

To change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u). For example, consider the tree

p1

and apply operation paint(3) to get the following:

p2

Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n integers colori (0 ≤ colori ≤ 1) — colors of the vertices. colori = 0 means that the i-th vertex is initially painted white, while colori = 1 means it’s initially painted black.
Then follow n - 1 line, each of them contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of vertices connected by the corresponding edge. It’s guaranteed that all pairs (ui, vi) are distinct, i.e. there are no multiple edges.

Output
Print one integer — the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.

Examples
input1

11
0 0 0 1 1 0 1 0 0 1 1
1 2
1 3
2 4
2 5
5 6
5 7
3 8
3 9
3 10
9 11

output1

2

input2

4
0 0 0 0
1 2
2 3
3 4

output2

0

Note
In the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.
In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0.

Solution

我们可以先缩点(把一块同色的联通块看做一个点),容易发现:直径上的点总是变的最慢的,所以最小的次数为 length(dia.)/2 l e n g t h ( d i a . ) / 2

缩点可以有很多种方式,这里介绍一种,每次找最近的与自己颜色不同的后代,然后加边

p3

缩点之后,变成了

这里写图片描述

可以发现树上的任意一条链都是黑白交替的

Code
#include <cstdio>
#define N 4000005
using namespace std;

struct Node{
    int to, nxt;
}e1[N], e2[N];
int po1, po2, lst1[N], lst2[N], cnt1, cnt2, a[N], d[N];

inline void add1(int u, int v) {
    e1[++cnt1].to = v;
    e1[cnt1].nxt = lst1[u];
    lst1[u] = cnt1;
}

inline void add2(int u, int v) {
    e2[++cnt2].to = v;
    e2[cnt2].nxt = lst2[u];
    lst2[u] = cnt2;
}

void dfs(int x, int co, int fa, int fafa) {
    for (int i = lst1[x]; i; i = e1[i].nxt) {
        if (e1[i].to == fa) continue;
        if (a[e1[i].to] == co) {
            add2(e1[i].to, fafa);
            add2(fafa, e1[i].to);
            dfs(e1[i].to, 1 - co, x, e1[i].to);
        }
        else dfs(e1[i].to, co, x, fafa);
    }
}

void dfs1(int x, int fa) {
    d[x] = d[fa] + 1;
    if (d[x] > d[po1]) po1 = x;
    for (int i = lst2[x]; i; i = e2[i].nxt) {
        if (e2[i].to == fa) continue;
        dfs1(e2[i].to, x);
    }
}

void dfs2(int x, int fa) {
    d[x] = d[fa] + 1;
    if (d[x] > d[po2]) po2 = x;
    for (int i = lst2[x]; i; i = e2[i].nxt) {
        if (e2[i].to == fa) continue;
        dfs2(e2[i].to, x);
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i < n; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        add1(x, y);
        add1(y, x);
    }
    dfs(1, 1 - a[1], 1, 1);//缩点
    po1 = 1;
    dfs1(1, 1);//求直径
    po2 = po1;
    d[po1] = 0;
    dfs2(po1, po1);//求直径
    printf("%d\n", d[po2] / 2);
    return 0;
}

如果有不会求直径的小盆友,戳这里_来自qianguch

UniApp 覆盖安装后首次进入 APP pages.json 不生效的解决方案 通过以上步骤,解决了缓存问题导致的首次加载异常。此方案的关键在于强制更新文件名,从根本上避免旧缓存的干扰。 阅读详情

相关推荐

miniDP与DP管脚定义差异详解及设计避坑指南

本文详细解析了Mini DisplayPort与标准DisplayPort接口在管脚定义上的核心差异,通过对比表格直观展示了两者20针脚的不同排列方式,特别是高速差分信号线的分布。文章结合真实设计案例,为硬件工程师提供了从原理图设计、PCB布局到信号完整性考量及故障排查的完整避坑指南,强调绝不能将两者视为简单的物理缩小版。

weixin_29248653的博客 362

Codeforces 734E Anton and Tree(并查集+树的直径)

传送门 题意:给一棵黑白染色过的树,每次操作可以将一块连通的同色子图(连通块内不含另一颜色)染成另一颜色,问最小需要操作几次才能将树上所有节点统一为同一颜色。 题解:先用并查集缩点,把同一颜色的连通的缩成一个点,得到新树,然后直径上的点数/2即可。(从直径中间那个点开始染色,把同色的区间不断往直径两端扩展,这个过程中非直径上的点的颜色也会被统一) 脑洞还是不够大....树的直径又没看出来诶 #include<cstdio> #include<cstring> #includ

KGV093的博客 274

普通人也能看懂的Transformer源代码解释(PyTorch)

Transformer源代码解释之PyTorch篇 章节 词嵌入 位置编码 多头注意力 搭建Transformer 在这里插入图片描述 词嵌入 Transformer本质上是一种Encoder,以翻译任务为例,原始数据集是以两种语言组成一行的,在应用时,应是Encoder输入源语言序列,Decoder里面输入需要被转换的语言序列(训练时)。一个文本常有许多序列组成,常见操作为将序列进行一些预处理(如词切分等)变成列表,一个序列的列表的元素通常为词表中不可切分的最小词,整个文本就是一个大列表,元素为一个一

qq_50974223的博客 4167

Codeforces 734E 树的直径

题意 传送门 Codeforces 734E Anton and Tree 题解 相同颜色的连通分量会被同时染色,故将其缩点,得到一个满足二分图性质的树。设树的直径为 ddd,在任一个节点染色,至多使树的直径减少 222,故染色数量的下界为 ⌈d/2⌉\lceil d/2\rceil⌈d/2⌉。可以取到这个下界,取节点 uuu 满足 uuu 至树上任一节点距离不超过 ⌈d/2⌉\lceil d/2\rceil⌈d/2⌉,那么进行 ⌈d/2⌉\lceil d/2\rceil⌈d/2⌉ 次 paint(u)pa

neweryyy的博客 289

CAPL自动化测试

通过本文的学习,读者应该对CAPL自动化测试有了初步的了解,并能够编写复杂的CAPL脚本。在实际项目中,CAPL的应用非常广泛,希望读者能够通过不断的学习和实践,掌握更多的CAPL技巧,提高自己的开发能力。

闲人编程的博客 1527

SpringBoot+Vue+kkFileView实现文档管理(文档上传、下载、在线预览)

上面在使用OpenOffice实现doc、excel、ppt等文档的管理和预览。除此之外可用kkFileView实现包括且更多文档的预览。

BADAO_LIUMANG_QIZHI的博客 1万+

CodeForces 734E-Anton and Tree(并查集缩点+树的直径)

题目链接:https://codeforces.com/problemset/problem/734/E 博客园食用链接:https://www.cnblogs.com/lonely-wind-/p/13417220.html Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph. There are n vertices in the tree,

孤风的博客 560

ENVI预处理GF-1号WFV1遥感数据提取NDVI指数

使用ENVI处理GF-1号遥感数据提取NDVI指数

所有专家都曾始于新手 8177

CodeForces734E - Anton and Tree(缩点+树的直径)

题目链接: http://codeforces.com/problemset/problem/734/E 题目大意: 给定一棵无根数,每个节点有黑白两种颜色,现在有一种涂色操作,一次可以将一块连通的同色区域涂为一个颜色,现在求最少操作几次可以将整棵树涂成黑色或白色。 解题过程: 因为之前做过一个涂板子的题,进行的操作和这个一样,也是先缩点。不过...

weixin_30556161的博客 163

内容审核与文本安全技术综述:检测、过滤与对齐

检测质量高度依赖数据。毒性/仇恨/冒犯语言检测领域形成了一批经典数据集,但它们的标签体系差异很大:例如 OLID(OffensEval)强调“是否冒犯—冒犯类型—目标对象”三层结构;HateXplain 在“hate/offensive/normal”三分类之外,还提供目标群体与人类标注的 rationale span;RealToxicityPrompts 则不做离散标签,而是用毒性分数来研究“模型续写时的毒性退化”。ACL 汇编。

nmdbbzcl的博客 1371

基于YOLOv8的MVTec AD缺陷检测系统设计与实现

速度快,精度高;支持 anchor-free 架构;支持实例分割和图像分类;模块化代码,易于部署与改进;完美兼容 ONNX、TensorRT 等推理框架;官方支持多平台训练和推理(CPU/GPU)。YOLOv8 是工业视觉检测落地非常理想的选择。MVTec AD(Anomaly Detection)数据集包括15类工业对象,每个对象包括多个子类缺陷(例如 “scratch”, “crack”, “hole”)及正常图像。类别缺陷子类bottlecablecapsule。

YOLO 931

codeforces 734 E. Anton and Tree(连通分量缩点+树的直径)

题目链接:http://codeforces.com/contest/734/problem/E思路:先缩点,变成黑白相间的一棵树,然后找出树的直径d,每改变一个节点的颜色,相当于将一整块变成一个新的连通分量,画张图可以看出来,答案就是(d+1)/2;#include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; ...

llmxby 277

【Claude Code】Not logged in 未登录错误解决方案 + /login 认证路径

Claude Code 启动时提示 Not logged in 表示当前会话缺少有效凭证。本文从环境变量、API Key 配置、CI 自动化凭证脚本和系统时钟四个维度,提供完整的认证排查流程与修复方案。

多媒体编程、网络编程、系统编程、网络安全编程、驱动编程 893

[codeforces 734E]Anton and Tree

【题目大意】 给一棵n( 【解题思路】 先将相同颜色的连通块缩点,然后我们就得到了一棵黑白相间的树。 我们可以发现当确定一个点为根时的最少操作数为树的高度减一。 当以树的直径的中点为根时树的高度最小。 因此答案为(树的直径长度+1)/2。 用dp或者两次dfs求出树的直径。 【呆马】 #include #include #inclu

S_G_G的博客 580

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

CodeForces - 734E Anton and Tree

博主的 BiBi 时间 博主还有 1h+11min\mathtt{1h+11min}1h+11min 就ヾノ≧∀≦)o 放假啦!好鸡冻!!! 然鹅博主这个小蒟蒻在假期还有作业,但是和平时比较假期作业显得极其划算。hihihihihihihi~ Solution 感觉这题非常巧。 我们将同色的连通块缩成一个点,然后就有一个非常神奇的性质:ansansans 就是 ⌊新树的直径+12⌋\lfloor \frac{新树的直径+1}{2} \rfloor⌊2新树的直径+1​⌋。 你可能觉得答案应该是 min⁡(黑点

Oxide‘s Blog 297

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【codeforces 734E】Anton and Tree【缩点+DP】

题目链接:codeforces 734E题目大意:来看题解的都是知道题目是什么意思的,我就不多嘴了。题解cf自题解已经非常清楚了。先缩点,再找出该树一条最长的直径d,答案就是(d+1)/2 为什么呐?? 假设树的直径是0,那么答案就是0吧。然后我们就可以看到这个事实: the tree diameter can’t be decreased more than by two per one pa

cyclization的小栈 408

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

codeforces - 734E - Anton and Tree(图论 树)

codeforces - 734E - Anton and Tree(图论 树)LINK:http://codeforces.com/contest/734/problem/E题意: 给一棵树,点被染成了黑白二色,每次操作可以翻转相邻的同色点的颜色,问最少多少次操作能把整棵树染成一个颜色。解法: 当天没去看..被剧透了,帮人把结论证明了一遍:把相邻同色的点缩点之后树的直径的一半就是答案。 对每

horihori_的博客 833

Codeforces-734E Anton and Tree(树的直径)

E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Anton is growing a tree in his garden. In

十分残念的博客 498

Anton and Tree CodeForces - 734E

题意:给出n个点,n-1条边构成一棵生成树,每个点都有黑白两种颜色,每次改变颜色都能使同颜色的相邻点改变颜色,求最小改变颜色的次数 思路:每次将一个联通块的颜色反转,比较容易想到每次反转后当前这个联通块的区域都会变的更大。 所以我们首先将这棵树进行缩点将相邻的相同颜色的点,缩为一点。缩点之后的树,每个节点都是原来的一个联通快。而将联通块的颜色反转等价于将这个缩点之后的树上的某个节点的全部相连的

曹讽 314

【27.91%】【codeforces 734E】Anton and Tree

time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Anton is growing a tree in his garden. In case you forgot, the tree is a con...

AWCXV 96

CodeForces - 734E - Anton and Tree

题目 给出n个点,n-1条边构成一棵生成树,每个点都有黑白两种颜色,每次改变颜色都能使同颜色的相邻点改变颜色,求最小改变颜色的次数 题解 每次将一个联通块的颜色反转,这个联通块的区域都会变的更大。 所以只要把颜色相同的点缩成一个点 只要在缩完点的树的直径中心改变len / 2个节点即可 len 为直径长度 #include&lt;bits/stdc++.h&gt; usin...

RE:X 240

CodeForces 734E - Anton and Tree

题目大意:对于给定的一颗树,现在已知这棵树的节点只有0,10,1两种颜色,你可以任选一个节点,并将这个节点所在的颜色相通的联通块颜色全部反转。问,最少需要几次操作可以将这棵树上所有的节点变成用一种颜色。思路: 每次将一个联通块的颜色反转,比较容易想到每次反转后当前这个联通块的区域都会变的更大。 所以我们首先将这棵树进行缩点。缩点之后的树,每个节点都是原来的一个联通快。而将联通块的颜色反转等价于将这

宝树啊宝树 651

Codeforces 734E Anton and Tree(缩点+树的直径)

题目链接:Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字的一棵数。 题解:首先一次可以改变一片数字,那么进行缩点后就变成了每次改变一个点。缩完点后这棵数变成了一棵相邻节点不同,0和1相交叉的一棵树。然后一棵树的直径上就是 0 1 0 1...

160

CodeForces 734E Anton and Tree 【连通缩点】

题目:点击打开链接 题意:给出n个点,n-1条边构成一棵生成树,每个点都有黑白两种颜色,每次改变颜色都能使同颜色的相邻点改变颜色,求最小改变颜色的次数 分析:因为每次改变颜色都能使同颜色的相邻点改变颜色,所以相邻且颜色相同的点可以缩成一点,重新建图,构成黑白相间的点的树,设树的直径(任意两点的最远距离)为d,因为每次改变颜色都会使它与相邻的点的颜色相同,所以取树的直径上的点操作,共有d+1个点

代码中转站 866

Codeforces 734E Anton and Tree (dfs缩点+树的直径)

E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Anton is growing a tree in his garden. In case yo

K键盘里的青春K 843
上一篇: XJOI提高组训练(1)T5方程的解数
下一篇: vjudge_XJ_Contest0@8/4/2018(updated)
北路人
博客等级 码龄9年 21粉丝 62原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值