题意:
给一颗树 每个节点有黑白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的更多相关文章

  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 缩点 树的直径

    传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...

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

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

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

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

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

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

随机推荐

  1. 在Win7 64位操作系统下安装Oracle 10g

    参见网址http://www.cnblogs.com/newstar/archive/2010/12/01/1878026.html 1.下载安装程序,可以到这个网址去下载 http://www.or ...

  2. shopex 小知识

    产品链接: http://www.--/product-172.html 中间的数字代表  sdb_goods 表中 的 goods_id  ... 表示数据库里的产品 id. 分类链接: http: ...

  3. 2014年6月份第2周51Aspx源码发布详情

    AMX高效自定义分页控件(WinForm)源码  2014-6-9 [VS2008]2014.6.9更新内容:   1. 更改用户自定义分页控件功能布局.大大精简了调用分页自定义控件的代码,和使用系统 ...

  4. Darlington Pair

    今天注意到在PWM输出电路中有个器件叫ULN2003,它是达林顿管阵列,该型号应该是通用型号,因为我看到ST和TI都有对应型号的产品. 达林顿管以前没听过,赶紧补一补,而所谓的阵列也就是八个达林顿管封 ...

  5. linux内核分析——扒开系统调用的三层皮(上)

    20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 系统调用 ...

  6. 从UWP到SWIFT-开始

    hi,all 我呢,是一个win10 uwp的开发者,从wp7.wp8.wp8.1.win8.1 到现在的win10,一直在windows阵营,做过一些大家比较熟悉的东西现在也还是在做win10的uw ...

  7. 用javascript简单封装AJAX

    1.创建一个AJAX引擎对象 var CreateAjax = function () { var xhr = null; if (window.XMLHttpRequest) { //非IE游览器 ...

  8. Why jsp?

    Before the JSP come into the world . The CGI and servlet took the responsibility of generating dynam ...

  9. mysql批量替换单字段

    update 表名 set 字段名 =  replace(字段名,'被替换内容','要替换内容'); 指定有人查这个!!!

  10. nfs文件系统挂载错误及解决方法

    挂载nfs时出现如下错误: 原因: 没有安装nfs客户端相关 解决方法: apt-get install nfs-common 参考资料: http://askubuntu.com/questions ...