题意:
给一颗树 每个节点有黑白2色
可以使一个色块同事变色,问最少的变色次数。
思路:
先缩点 把一样颜色的相邻点 缩成一个
然后新的树 刚好每一层是一个颜色。
最后的答案就是树的直径/2

不过我用的树上的dp,强行求了以每个点为根时树的深度
答案就是最小的深度-1

具体见代码:

const int maxn = 200000 + 10;
int n;
int color[maxn];
int pa[maxn];
vector<int> G[maxn], G2[maxn];
void init()
{scanf("%d", &n);for (int i = 1; i <= n; i++){scanf("%d", color + i);}int u, v;for (int i = 1; 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(1);color[0] = -1;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 = 0; 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 = 0; i <= n; i++){for (int j = 0; 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] = 0;for (int i = 0; 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 = 0; i < G[1].size(); i++) q.push(G[1][i]);int ans = d[1] = deep[1];while (!q.empty()){int u = q.front(); q.pop();if (son1[fa[u]] == u) deepFa[u] = deep[son2[fa[u]]] + 1;else deepFa[u] = deep[son1[fa[u]]] + 1;deepFa[u] = max(deepFa[u], deepFa[fa[u]] + 1);d[u] = max(deep[u], deepFa[u] + 1);ans = min(ans, d[u]);for (int i = 0; i < G[u].size(); i++){q.push(G[u][i]);}}return ans - 1;
}void solve()
{for (int i = 1; i <= n; i++) pa[i] = i;getTree();memset(fa, -1, sizeof(fa));for (int i = 0; i <= n; i++) son1[i] = son2[i] = n + 1;dfs(0);cout << bfs() << endl;
}int main()
{init();solve();return 0;
}

转载于:https://www.cnblogs.com/liangyongrui/p/6074008.html

Codeforces Round #379 (Div. 2) E. Anton and Tree相关推荐

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

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

  3. Codeforces Round #453 (Div. 1) D. Weighting a Tree 构造 + dfs树

    传送门 文章目录 题意: 思路: 题意: 给你一颗nnn个点的图,每个点都有一个点权cic_ici​,要求你给每个边赋一个权值kik_iki​,要求对于每个点与他相连的边的权值之和等于这个点的点权ci ...

  4. Codeforces Round #379 (Div. 2) 总结分享

    第一次Virtual participation,contest时只ac了A.B两道,感觉这两道题没什么好说的,直接从C题开始. C. Anton and Making Potions 题意 制作n瓶 ...

  5. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  6. Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心

    E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

  7. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  8. Codeforces Round #324 (Div. 2) E. Anton and Ira(贪心)

    题意: 给定N≤2000的两个序列,通过交换第一个序列变成第二个序列给定N\le 2000的两个序列, 通过交换第一个序列变成第二个序列 如果交换ai和aj,交换的代码为|i−j|,给出交换代价最小的 ...

  9. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

    链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为 ...

最新文章

  1. 聊一聊今年实例分割领域的进展和未来展望
  2. dataAdapter与dataSet和dataTable的填充
  3. 河北省计算机2018单招试题答案,2018年河北省普通高职单招考试十类和高职单招对口电子电工类、计算机类联考命题、考试与评卷...
  4. DDoS booter滥用 DTLS 服务器放大攻击
  5. Java线程之Callable和Future
  6. java web消息机制_JavaWeb BeanUtils信息类原理详解
  7. C#效率优化(2)-- 方法内联
  8. 如何运用创客匠人微信小程序实现引流拓客?
  9. 【目标流畅阅读文献】kick off
  10. 伸展树(一) - 图文解析与C语言实现
  11. 如何将iPhone中的手机通讯录同步至安卓手机(教学篇)
  12. 改变exe文件图标的方法
  13. Anaconda下载simpleITK包和pytorch包
  14. 处理器协同机制其二内存屏障与内存顺序(及Store Buffer与Invalidate Queue)
  15. Scheduling
  16. 关于wiretap库
  17. OpenCV Using Python——应用统计肤色模型和相对于块原点能量的肤色分割
  18. iphone UINavigationController使用的一些技巧
  19. 「 LaTeX 」写论文,参考文献编译错误,ERROR:Misplaced alignment tab character .
  20. 为什么大多数婆婆和儿媳都处不好呢?

热门文章

  1. 物联网的四种计算类型
  2. 自学编程的八大误区!克服它!
  3. c JAVA 注解,Java元注解作用及使用
  4. Pluto - iOS 上一个高性能的排版渲染引擎
  5. 自动 更新SVN目录文件.bat
  6. 在sqlplus中操作blob和clob
  7. ORACLE包和过程依赖关系测试
  8. Sqoop import导入表时报错java.lang.ClassNotFoundException: org.json.JSONObject
  9. 前端三部曲之Html -- 1(html的基本结构和常见的meta标签的作用)
  10. SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)