传送门

题意:

这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色。问最少操作次数,使得树上颜色相同。

思路:

先缩点,把相同的颜色的相邻节点缩在一起。再求出树的最长直径S(边的个数),答案就是(S + 1)/ 2;

因为对于一条链,我们可以从中间向两边交换改变。

#include <bits/stdc++.h>
#define pb push_back
using namespace std; const int MAXN = 2e5+;
vector<int>mp[MAXN],newmap[MAXN];
int n,a[MAXN],col[MAXN],h[MAXN],fa[MAXN];
bool vis[MAXN];
int ans = ;
void dfs(int u, int c){
vis[u] = true;
col[u] = c;
for(int i=; i < mp[u].size(); i++){
int v = mp[u][i];
if(a[v]!=a[u])continue;
if(vis[v])continue;
dfs(v,c);
}
}
void dfs2(int u, int o){ int mx = ,mxx = ;
for(int i=; i<newmap[u].size(); i++){
int v = newmap[u][i];
if(v==o)continue;
dfs2(v,u);
mx = max(mx,h[v] + );
if(mx > mxx)swap(mx,mxx);
}
h[u] = mxx;
ans = max (ans, mxx + mx);
}
int main(){
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%d", &a[i]);
}
for(int i=; i<n; i++){
int u,v;
scanf("%d%d", &u, &v);
mp[u].pb(v);
mp[v].pb(u);
}
int cnt = ;
for(int i=; i<=n; i++){
if(!vis[i]){
dfs(i , cnt++);
}
}
for(int i=; i<=n; i++){
for(int k = ; k < mp[i].size(); k++){
if(col[i] != col[mp[i][k]]){
newmap[col[i]].pb(col[mp[i][k]]);
}
}
}
dfs2(, -);
printf("%d\n", (ans + )/);
return ;
}

CF734E

Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径的更多相关文章

  1. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  2. Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

    题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...

  3. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #379 (Div. 2) E. Anton and Tree

    题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...

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

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  6. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  7. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  8. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  9. Codeforces Round #379 (Div. 2) A. Anton and Danik 水题

    A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...

随机推荐

  1. SparkStreaming对接rabbitMQ

    /** * SparkStreaming对接rabbitmq java代码 */public class SparkConsumerRabbit { public static void main(S ...

  2. Codeforces Round #192 (Div. 2) (330A) A. Cakeminator

    题意: 如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕. //cf 192 div2 #include <stdio.h> #include & ...

  3. powershell小脚本--批量添加用户属性----导出登录时间

    需求1:某公司所有员工少了MAIL属性,需要批量添加.例如,用户chenyy  添加邮件属性chenyy@xxxx.com 先导出(只导出名字)备用: Get-ADUser -Filter * -Pr ...

  4. 大数阶乘(c++实现)

    #include <iostream>using namespace std;#define N 1000int BigNumFactorial(int Num[], int n);voi ...

  5. python创建虚拟环境(Windows)

    >>>构建Python虚拟环境的目的是为了防止真实环境被破坏!!! >>>每一个项目建议用一个虚拟环境为了防止软件版本号冲突!!! 1.在终端切换到一个新的磁盘 如 ...

  6. Wpf窗口设置屏幕居中最前显示

    public Window()         {             InitializeComponent();             WindowStartupLocation = Win ...

  7. c#小灶——数据类型

    C#中有许多数据类型,存储不同的数据要用不同的数据类型.我们这里面向初学只介绍值类型,引用类型和指针类型在后续的学习中会有接触. 整型 int是最常用的整型,用来存储整数.除了int之外,还有其他不常 ...

  8. Spring系列(二):Spring IoC应用

    一.Spring IoC的核心概念 IoC(Inversion of Control  控制反转),详细的概念见Spring系列(一):Spring核心概念 二.Spring IoC的应用 1.定义B ...

  9. 认识Linux工具

    Centos7镜像网站:清华,阿里,网易 软件安装:lamp   httpd (认识) yum: 安装工具    需要选版本和特性,所以生产不用yum rpm:安装依赖 源码编译 shell脚本:yu ...

  10. 手写Struts,带你深入源码中心解析

    个人剖析,不喜勿喷 扫码关注公众号,不定期更新干活 在此申明本博文并非原创,原文:http://blog.csdn.net/lenotang/article/details/3336623,本文章是在 ...