题目连接:http://codeforces.com/contest/734/problem/E

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 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

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

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.

题目大意:有一棵树,有黑白两种颜色,每个节点为黑色或白色,每次变换使得相连的同色节点全部变成另一种颜色,求使树变成单色(纯黑或纯白)所需的最少操作数。

解题思路:

刚开始忽略了改变颜色后连同块的大小会改变,用并查集做了,结果当然WA,后来想到会改变,所以用dfs。

第一次dfs缩点,把连通块缩为一点,构成一个新图,方便处理。

缩点完成后的图就变成了黑白交替出现的一棵树,改变一个节点就相当于把它和与他直接相连的所有节点缩为一点,最后的结果为该树的最长路径/2。

写一下自己想的弱鸡证明过程;

1.首先证明,操作次数只与最长路径有关。

假设有一棵树(称之为完全树),在它的最长路径上的所有节点都连有在保证该路径是最长路径的前提下可连的最多节点,显然,此种情况下操作次数不会大于只有最长路的操作次数,因为必须保证最长链完成修改。另外显然,经过缩点得到的所有可能的树的操作次数小于等于完全树,所以可得,操作次数只与最长路径有关。

2.证明结果为最长路径的1/2。

只考虑其最长路径,假设最长路径节点数n为奇数,则相当于每次操作减少两节点,直至最后剩下一个节点,设操作数为x,则有:n-2x=1,解得x=(n-1)/2;

假设n为偶数,则最后一次操作相当于只消除一个节点,则有n-2(x-1)-1=1,解得 =n/2

综合可得,x=n/2(整数除法)

代码如下:

#include<bits/stdc++.h>

using namespace std;

vector <],g[];
];
]={};
,maxp;

void dfs1(int n,int s)
{
    if(n>N||vis[n])
        return;
    vis[n]=;
    if(c[n]!=c[s])
    {
        g[n].push_back(s);
        g[s].push_back(n);
        s=n;
    }
    ;i<G[n].size();i++)
    {
        dfs1(G[n][i],s);
    }
}

void dfs2(int n,int dis)
{
    if(vis[n])
        return;
    vis[n]=;
    if(dis>maxx)
    {
        maxx=dis;
        maxp=n;
    }
    ;i<g[n].size();i++)
        dfs2(g[n][i],dis+);
}

int main()
{
    int x,y;
    cin>>N;
    ;i<=N;i++)
        scanf("%d",&c[i]);
    ;i<N;i++)
    {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs1(,);
    memset(vis,,sizeof(vis));
    dfs2(,);
    memset(vis,,sizeof(vis));
    dfs2(maxp,);
    cout<<maxx/<<endl;
}

codeforces734E的更多相关文章

  1. CodeForces-734E Anton and Tree 树的直径

    题目大意: 给定一棵有n个节点的树,有黑点白点两种节点. 每一次操作可以选择一个同种颜色的联通块将其染成同一种颜色 现在给定一个初始局面问最少多少步可以让树变为纯色. 题解: 首先我们拿到这棵树时先将 ...

随机推荐

  1. 【bzoj2659】[Beijing wc2012]算不出的算式 数论

    题目描述 求,其中p和q是奇质数. 输入 只有一行,两个奇质数,分别表示p,q. 输出 一个数,表示算式结果. 样例输入 5 样例输出 6 题解 数论 神TM数学结论题... 当$p\neq q$时, ...

  2. POJ 3693 Maximum repetition substring(最多重复次数的子串)

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10461   Ac ...

  3. JavaScript的大括号的语义

    Javascript中大括号"{}"有四种语义作用: 语义1. 组织复合语句,这是最常见的: view source   print? 1 if( condition ) { 2 ...

  4. Java 处理 XML 的三种主流技术及介绍

    Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...

  5. Bash 实例,第二部分

    我们先看一下处理命令行自变量的简单技巧,然后再看看 bash 基本编程结构. 接收自变量 在 介绍性文章 中的样本程序中,我们使用环境变量 "$1" 来引用第一个命令行自变量.类似 ...

  6. sql数据库的链接方式

    今天看见了一个数据库的链接方法,给转载了,记得我刚刚学DAO的时候老是要记载这些东西,所以就上博客园上面看了看,就转过来了... MySQL: String Driver="com.mysq ...

  7. python2.7字典转换成json时中文字符串变成unicode的问题:

    参考:http://blog.csdn.net/u014431852/article/details/53058951 编码问题: python2.7字典转换成json时中文字符串变成unicode的 ...

  8. swt 更新主UI线程

    // 将msg送回对应的Applet public void write(String msg) { synchronized (msg) { try { m_out.writeUTF(msg); } ...

  9. Yeelight介绍

    1. 介绍 Yeelight是小米生态链中的WiFi智能灯泡,本文介绍它的接入和控制实现: Yeelight使用的是自定义的私有协议,该协议采用了类似SSDP的发现机制和基于JSON的控制命令 2. ...

  10. 【转】cve2014-3153 漏洞之详细分析与利用

    背景学习: Linux Futex的设计与实现 使用者角度看bionic pthread_mutex和linux futex实现 By kernux TopSec α-lab 一 漏洞概述 这个漏洞是 ...