E. Anton and Tree(缩点+树直径模板)
来源:互联网 发布:php运行环境配置 编辑:程序博客网 时间:2024/05/16 15:20
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.
110 0 0 1 1 0 1 0 0 1 11 21 32 42 55 65 73 83 93 109 11
2
40 0 0 01 22 33 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
题意:求得最小操作次数使得,一个只有黑白两种颜色的无向图变成一种颜色
解题思路:用了缩点和求树的直径,将相同颜色临近的点缩成一个颜色,然后关于求树的直径,先任意一点求BFS获得最长的那个点,从他再BFS一边获得的最长点就是树的直径,求出来后,可以近似的处理成一个树,然后就是树的直径向上取整
#include<iostream> #include<cstdio>#include<stdio.h>#include<cstring> #include<cstdio> #include<climits> #include<cmath> #include<vector>#include <bitset>#include<algorithm> #include <queue>#include<map>using namespace std;int a[200005], n;vector<int> tree[200005];pair<int, int> temp;void dfs(int root, int son, int deep){int i;pair<int, int> t;t = make_pair(deep, son);temp = max(temp, t);for (i = 0; i < tree[son].size(); i++){int u = tree[son][i];if (root ==u){continue;}if (a[u] == a[son]){dfs(son, u, deep);}else{dfs(son, u, deep + 1);}}}int main(){int i, x, y;pair<int, int> s;cin >> n;for (i = 1; i <= n; i++){cin >> a[i];}for (i = 1; i < n; i++){cin >> x >> y;tree[x].push_back(y);tree[y].push_back(x);}temp = make_pair(0, 0);dfs(-1, 1, 0);s = temp;temp = make_pair(0, 0);dfs(-1, s.second, 0);cout << (temp.first + 1) / 2 << endl;}
- E. Anton and Tree(缩点+树直径模板)
- Codeforces 734E. Anton and Tree By Assassin 缩点+树的最大直径
- Codeforces 734E Anton and Tree (dfs缩点+树的直径)
- Codeforces 734E Anton and Tree【并查集缩点||DFS缩点+树的直径】
- Codeforces-734E Anton and Tree(树的直径)
- CF734E:Anton and Tree(缩点 & 直径)
- [Codeforces734E]Anton and Tree 树的直径
- 【codeforces 734E】Anton and Tree【缩点+DP】
- 思路题,树的直径(Anton and Tree,cf 734E)
- Codeforces 734E Anton and Tree【并查集缩点+树的直径】好题~
- 【CodeForces734E】【缩点】Anton and Tree 题解
- codeforces 734 E. Anton and Tree (黑红树,缩点+dfs)
- Codeforces Round #379 (Div. 2)E. Anton and Tree(dfs缩点,想法题)
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
- CodeForces 734E - Anton and Tree
- 【27.91%】【codeforces 734E】Anton and Tree
- CodeForces 734 E.Anton and Tree(dfs)
- Anton and Tree CodeForces
- 位操作
- Spring bean配置和自动注入
- POJ-2823Sliding Window(单调队列模板)
- UNIX网络编程(一)-unp.h文件的编译
- 疯狂Java笔记:3.6 直接量
- E. Anton and Tree(缩点+树直径模板)
- uva 674 && hdu 2069 coin change(水完全背包)
- Oracle创建序列和触发器
- 通过生成 llvm IR,提高程序的执行性能
- Java NIO笔记之内存映射文件
- jprofiler 安装和连接linux(tomcat)
- jsp引用css和js文件方法 以及 java web应用程序文件结构
- 在(U)EFI环境下重装Grub2
- Minimum Absolute Difference in BST