Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E
3 seconds
256 megabytes
standard input
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.
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.
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.
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
2
4
0 0 0 0
1 2
2 3
3 4
0
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.
题解:
1.缩点:将颜色相同的连通块缩成一个点。当然并不是实际将其缩成一点,而是当前点与上一个点的颜色相同,则“不作为”, 即无视这个点。经过缩点之后,这棵树的结点颜色为黑白交替,如下图。
2.无环无向图的最长路:首先取随便一个点作为起点,然后dfs跑一遍。取得路径最长的那条路的终点,并以此作为起点,再跑一遍dfs,取最长的路径len,最终答案即为len/2,向下取整。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e5+; int n, c[maxn];
vector<int>son[maxn]; pair<int,int> dfs(int u, int fa, int cnt)
{
pair<int,int> tmp = make_pair(cnt, u);
for(int i = ; i<son[u].size(); i++)
{
int v = son[u][i];
if(v==fa) continue; //由于无环,所以只要避免其走“回头路”即可,无需vis数组
tmp = max( tmp, dfs(v,u,cnt+(c[u]!=c[v])) ); //如果颜色相同,则cnt+0,就跳过了当前点
}
return tmp;
} int main()
{
scanf("%d",&n);
for(int i = ; i<=n; i++)
scanf("%d",&c[i]); for(int i = ; i<n; i++)
{
int u, v;
scanf("%d%d",&u,&v);
son[u].push_back(v);
son[v].push_back(u);
} pair<int,int> tmp;
tmp = dfs(,-,);
tmp = dfs(tmp.second, -, );
cout<< tmp.first/ <<endl;
}
Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路的更多相关文章
- 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 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径
传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...
- 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 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree
题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...
- 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 ...
- 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 ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- 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 ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟
题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...
随机推荐
- Loj #125. 除数函数求和(2)
link : https://loj.ac/problem/125 分块calc即可. #include<bits/stdc++.h> #define ll long long using ...
- idea没有subversion问题
问题描述:idea导入svn项目,但点击项目文件右键没有找到subversion选项.同时,idea界面底部没有version control菜单. 解决方法:原因是idea没有启动版本控制.点击id ...
- springboot 启动类启动跳转到前端网页404问题的两个解决方案
前段时间研究springboot 发现使用Application类启动的话, 可以进入Controller方法并且返回数据,但是不能跳转到WEB-INF目录下网页, 前置配置 server: port ...
- android 根据图片名字获取图片id
public int getResource(String imageName){ Context ctx=getBaseContext(); int resId = getResources().g ...
- android权限大全转http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html
android权限大全转http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html 访问登记属性 android.permission ...
- 安装mongoDB遇见的一个路径问题
如果安装路径不存在,则不会解压EXE软件! 安装monogoDB后,它不会自动添加执行路径! 意思就是安装路径是D盘下面的mongoDB文件夹,假如不存在这个文件夹,则不会安装成功 你需要添加路径: ...
- 如何防范SQL注入式攻击
一.什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者 ...
- python(21)- python内置函数练习
题目一:用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao'] name=['alex','wupeiqi','yu ...
- AVL平衡树的插入例程
/* **AVL平衡树插入例程 **2014-5-30 11:44:50 */ avlTree insert(elementType X, avlTree T){ if(T == NULL){ T = ...
- 【每日Scrum】第六天(4.27) TD学生助手Sprint2站立会议
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 今天和楠哥做了课程事件和日历表操作的例子,并尝试做时间表和日历表的数据库设计 Y 刘静 今天开始编辑自己项目中的日历管理 编辑程序,能够在日历 ...