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 ...
随机推荐
- redis缓存技术学习
1 什么是redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合)和zset ...
- 设计模式之observer and visitor
很长时间一直对observer(观察者)与visitor(访问者)有些分不清晰. 今天有时间进行一下梳理: 1.observer模式 这基本就是一个通知模式,当被观察者发生改变时,通知所有监听此变化的 ...
- java install
http://www.cnblogs.com/a2211009/p/4265225.html
- maven 问题解决 tools以及jconsole两个jar包 无效
在SVN上下载项目,结果完成后出现两个jar包不存在的情况 如下图: 然后,第一步就是去查看POM.xml文件是否配置了这两个文件,结果并没有,于是就更加奇怪了 所以怀疑是不是其他maven下载的ja ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- c++继承,多态,重载的作用
继承:用户通过继承,可以从一个类派生出多个子类,减少了重新定义类的次数,并且在不影响子类的相应功能的情况下,保护了基类中的数据安全性.用户还可以在子类中,使用与基类相同的行为实现不同的功能,因此,使用 ...
- GUI
容器:1.JWindow 2.JFrame 3.JDialogo 4.JApplet 边界布局管理: 布局方式:把整个容器划分为五个部分:东西南北中,南北要贯通,中间最大(不仅是范围,权利也最大), ...
- JAVA深入研究——Method的Invoke方法。
在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...
- tcp传送xml
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.i ...
- MSSQL—字符串分离(Split函数)
前面提到了记录合并,有了合并需求肯定也会有分离需求,说到字符串分离,大家肯定会想到SPLIT函数,这个在.NET,Java和JS中都有函数,很可惜在SQL SERVER中没有,我们只能自己来写这么一个 ...