Codeforces Round #379 (Div. 2) E. Anton and Tree
题意:
给一颗树 每个节点有黑白2色
可以使一个色块同事变色,问最少的变色次数。
思路:
先缩点 把一样颜色的相邻点 缩成一个
然后新的树 刚好每一层是一个颜色。
最后的答案就是树的直径/2
不过我用的树上的dp,强行求了以每个点为根时树的深度
答案就是最小的深度-1
具体见代码:
const int maxn = + ;
int n;
int color[maxn];
int pa[maxn];
vector<int> G[maxn], G2[maxn];
void init()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", color + i);
}
int u, v;
for (int i = ; i < n; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
} int find(int x)
{
return pa[x] != x ? pa[x] = find(pa[x]) : x;
} int fa[maxn];
void getTree()
{
queue<int> q;
q.push();
color[] = -;
while (!q.empty())
{
int u = q.front(); q.pop();
if (color[fa[u]] == color[u]) pa[u] = find(fa[u]);
else G2[fa[pa[u]]].push_back(u);
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (find(v) == find(fa[u])) continue;
fa[v] = pa[u];
q.push(v);
}
}
swap(G, G2);
} void pg()
{
cout << "Graph:" << endl;
for (int i = ; i <= n; i++)
{
for (int j = ; j < G[i].size(); j++)
{
cout << i << " " << G[i][j] << endl;
}
}
} int son1[maxn], son2[maxn]; //i节点的最大的儿子 和 次大的儿子的下标 int deep[maxn];
int deepFa[maxn];//i的父亲除了i以外的最深深度 int d[maxn];//以i为根时树的深度 d[i] = max(deep[i], deepFa[i] + 1) int dfs(int u) //得到每个节点最深儿子的深度
{
deep[u] = ;
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
fa[v] = u;
int tmp = dfs(v);
if (tmp >= deep[u])
{
son2[u] = son1[u];
son1[u] = v;
deep[u] = tmp;
}
else
{
if (tmp > deep[son2[u]]) son2[u] = v;
}
}
deep[u]++;
return deep[u];
} int bfs()
{
queue<int> q;
for (int i = ; i < G[].size(); i++) q.push(G[][i]);
int ans = d[] = deep[];
while (!q.empty())
{
int u = q.front(); q.pop();
if (son1[fa[u]] == u) deepFa[u] = deep[son2[fa[u]]] + ;
else deepFa[u] = deep[son1[fa[u]]] + ; deepFa[u] = max(deepFa[u], deepFa[fa[u]] + ); d[u] = max(deep[u], deepFa[u] + );
ans = min(ans, d[u]);
for (int i = ; i < G[u].size(); i++)
{
q.push(G[u][i]);
}
}
return ans - ;
} void solve()
{
for (int i = ; i <= n; i++) pa[i] = i;
getTree();
memset(fa, -, sizeof(fa));
for (int i = ; i <= n; i++) son1[i] = son2[i] = n + ;
dfs();
cout << bfs() << endl;
} int main()
{
init();
solve();
return ;
}
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 —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- 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 缩点 树的直径
传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...
- 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 ...
随机推荐
- DotSpatial 删除图层要素
//添加图层后,定义图层,并获取图层 //遍历要素,并进行删除 FeatureSet fs = null; fs = (FeatureSet) map1.Layers[0].DataSet; //要素 ...
- 【python】闭包、@修饰符(装饰器)、
闭包:(返回函数的行为叫闭包??) #函数也是对象,所以可以被传递 def line_conf(a,b): def line(x): return a*x+b return line line1=li ...
- php 错误处理函数
eval() 把子符串当做php 代码执行 // 回调函数function a($b, $c) { echo $b; echo $c; } call_user_func_array('a', ar ...
- 对Spring AOP的理解
AOP(Aspect-Oriented Programming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善. AOP在spring ...
- 谈谈黑客攻防技术的成长规律(aullik5)
黑莓末路 昨晚听FM里谈到了RIM这家公司,有分析师认为它需要很悲催的裁员90%,才能保证活下去.这是一个意料之中,但又有点兔死狐悲的消息.可能在不久的将来,RIM这家公司就会走到尽头,或被收购,或申 ...
- windows下无法创建django工程的问题
环境:python2.7 django1.7 安装好django后,将C:\Python27\Lib\site-packages\Django-1.7.7-py2.7.egg\django\bin; ...
- iOS 服务器回空数据的处理
后端返回一个数组类型的数据,但是数据里面包含"<null>","(null)"等,本地缓存写入数据失败,write to File: 方法限制, 可 ...
- [解决方案] pythonchallenge level 6
查看页面代码,知道找zip www.pythonchallenge.com/pc/def/channel.zip,查看zip下的readme.txt知道从90052,跑一遍知道要收集zip的comme ...
- 重写ajax方法实现异步请求session过期时跳转登录页面
jQuery(function($){ // 备份jquery的ajax方法 var _ajax=$.ajax; // 重写ajax方法, $.ajax=function(opt){ var _suc ...
- JavaScript HTML CSS外部链接
HTML文件 <!--<html> <head><link rel="stylesheet" type="text/css" ...